<?php
/**
 * File Path: includes/class-mbs-autoloader.php
 *
 * My Book Showroom — Autoloader
 *
 * Handles two class naming conventions used in this codebase:
 *
 *   1. Global MBS_* classes (all files except the license child classes).
 *      Example: MBS_License_Client → includes/api/class-mbs-license-client.php
 *
 *   2. Namespaced DMRS\MBS\Api\License\* child classes (license/ subdirectory).
 *      Example: DMRS\MBS\Api\License\MBS_License_Credentials
 *               → includes/api/license/class-mbs-license-credentials.php
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class MBS_Autoloader {

	/** Register the autoloader. */
	public static function register(): void {
		spl_autoload_register( array( __CLASS__, 'autoload' ) );
	}

	/** Autoload MBS classes — both global and namespaced. */
	public static function autoload( string $class_name ): void {

		// ── Branch 1: Namespaced license child classes ────────────────────────
		// Handles: DMRS\MBS\Api\MBS_License_Client
		//          DMRS\MBS\Api\License\MBS_License_Credentials  (etc.)
		if ( 0 === strpos( $class_name, 'DMRS\\MBS\\Api\\' ) ) {
			// Strip the namespace prefix to get the bare class name.
			// e.g. 'DMRS\MBS\Api\License\MBS_License_Credentials' → 'MBS_License_Credentials'
			$parts      = explode( '\\', $class_name );
			$bare_class = end( $parts );
			$filename   = 'class-' . strtolower( str_replace( '_', '-', $bare_class ) ) . '.php';

			// Check the license child directory first, then the api directory.
			$candidates = array(
				MBS_INCLUDES_DIR . 'api/license/' . $filename,
				MBS_INCLUDES_DIR . 'api/'         . $filename,
			);

			foreach ( $candidates as $file ) {
				if ( file_exists( $file ) ) {
					require_once $file;
					return;
				}
			}

			// Not found — do not fall through to Branch 2.
			return;
		}

		// ── Branch 2: Global MBS_* classes ───────────────────────────────────
		if ( 0 !== strpos( $class_name, 'MBS_' ) ) {
			return;
		}

		$bare_name = strtolower( str_replace( '_', '-', $class_name ) );
		$filename  = 'class-' . $bare_name . '.php';
		// Also resolve interface-*.php files (e.g. interface-mbs-payment-processor.php).
		$iface_filename = 'interface-' . $bare_name . '.php';

		// API directories searched first — critical for license client and update checker.
		$search_dirs = array(
			MBS_INCLUDES_DIR . 'api/',
			MBS_INCLUDES_DIR . 'api/license/',
			MBS_INCLUDES_DIR . 'api/esp/',
			MBS_INCLUDES_DIR,
			MBS_INCLUDES_DIR . 'admin/',
			MBS_INCLUDES_DIR . 'admin/tabs/',
			MBS_INCLUDES_DIR . 'admin/tabs/imp-exp/',
			MBS_INCLUDES_DIR . 'admin/authors/',
			MBS_INCLUDES_DIR . 'admin/tabs/authors-series/',
			MBS_INCLUDES_DIR . 'admin/tabs/license/',
			MBS_INCLUDES_DIR . 'admin/tabs/general/',
			MBS_INCLUDES_DIR . 'admin/tabs/appearance/',
			MBS_INCLUDES_DIR . 'data/',
			MBS_INCLUDES_DIR . 'helpers/',
			MBS_INCLUDES_DIR . 'helpers/cover-strategies/',
			MBS_INCLUDES_DIR . 'import/',
			MBS_INCLUDES_DIR . 'import/csv/',
			MBS_INCLUDES_DIR . 'export/',
			MBS_INCLUDES_DIR . 'metaboxes/',
			MBS_INCLUDES_DIR . 'metaboxes/book-header/',
			MBS_INCLUDES_DIR . 'payment/',
			MBS_INCLUDES_DIR . 'post-types/',
			MBS_INCLUDES_DIR . 'shortcodes/',
			MBS_INCLUDES_DIR . 'blocks/',
			MBS_INCLUDES_DIR . 'taxonomies/',
			MBS_INCLUDES_DIR . 'widgets/',
			MBS_LAYOUTS_DIR,
			MBS_INCLUDES_DIR . 'compat/',
			MBS_INCLUDES_DIR . 'migrations/',
		);

		foreach ( $search_dirs as $dir ) {
			$file = $dir . $filename;
			if ( file_exists( $file ) ) {
				require_once $file;
				return;
			}
			// Try interface-*.php variant.
			$ifile = $dir . $iface_filename;
			if ( file_exists( $ifile ) ) {
				require_once $ifile;
				return;
			}
		}
	}
}
