<?php
/**
 * API Registry
 *
 * Loads and registers all API controllers
 *
* PHASE 5.6 CLEANUP COMPLETE!
 * All legacy APIs removed - Controllers are Pure V2.2!
 *
 * Uses Composer autoloader + namespace imports (use) instead of require_once.
 * Class references trigger autoloading on demand.
 *
 * @package FleekDash\API
 */

namespace FleekDash\API;

use FleekDash\Core\AuthMiddleware;
use FleekDash\Media\MediaFolderNativeIntegration;
use FleekDash\Services\MediaFolderService;
use FleekDash\Taxonomies\MediaFolderTaxonomy;

class ApiRegistry
{
    /** @var \FleekdashMenuSyncAPI|null Kept for hook lifecycle; passed to extensions via action */
    private static ?\FleekdashMenuSyncAPI $menuSyncApi = null;

    /**
     * Register all API controllers
     *
     * Centralized API initialization for all FleekDash endpoints
     *
     * Uses Composer autoloader + namespace imports (use) instead of require_once.
     * Class references trigger autoloading on demand.
     */
    public static function registerAll()
    {
        // Load core dependencies via class reference (triggers Composer autoloader)
        class_exists(AuthMiddleware::class);
        class_exists(BaseController::class);

        // PHASE 5.6 CLEANUP: 13 Legacy APIs deleted after Pure V2.2 migration
        //
        // Deleted files (logic moved to Pure V2.2 Controllers):
        // class-fleekdash-dashboard-unified.php → DashboardUnifiedController
        // DashboardSettings.php → DashboardSettingsController
        // AppearanceSettings.php → AppearanceSettingsController
        // PluginsAPI.php → PluginsController
        // GmailSettings.php → GmailController
        // SimpleMenuAPI.php → MenuController (uses V2.2 Services)
        // MenuFilterAPI.php → MenuController (uses V2.2 Services)
        // MenuCapabilityValidatorAPI.php → MenuController (uses V2.2 Services)
        // MenuCacheAPI.php → MenuController (uses V2.2 Services)
        // RoleManagementAPI.php → RolesController
        // LanguageAPI.php → LanguageController
        // TextOverridesAPI.php → TextOverridesController
        // CacheAPI.php → CacheController

        // Media Folder taxonomy + API (V2.3.2)
        MediaFolderTaxonomy::init();
        class_exists(MediaFolderService::class);
        class_exists(MediaAPI::class);

        // Media Folder native integration - folder sidebar in wp.media modal (V2.4.3)
        class_exists(MediaFolderNativeIntegration::class);
        MediaFolderNativeIntegration::init();

        // Load remaining helper classes via class reference (triggers Composer autoloader)
        class_exists(CapabilityValidationAPI::class);
        class_exists(PluginDirectoryInstaller::class);
        class_exists(ThemeDirectoryInstaller::class);
        class_exists(UpdateJobManager::class);
        class_exists(RollbackJobManager::class);
        class_exists(\FleekdashMenuSyncAPI::class);
        class_exists(AISettingsAPI::class);
        class_exists(FleekDocsSettingsAPI::class);
        class_exists(AdminBarSettingsAPI::class);

        // Register controllers on rest_api_init hook
        // Check if hook has NOT fired yet before scheduling
        // If hook already fired, call directly; otherwise schedule it
        if (!\did_action('rest_api_init')) {
            // Hook not fired yet - schedule it
            \add_action('rest_api_init', [self::class, 'registerAllApis'], 5);
        } else {
            // Hook already fired - call directly
            self::registerAllApis();
        }
    }

    /**
     * Register ALL APIs
     *
     * Called on rest_api_init hook
     *
* PHASE 5.6: Clean implementation - only MenuSyncAPI needed
     */
    public static function registerAllApis(): void
    {
        // Constructor registers admin_menu hook - instance kept and passed to extensions
        self::$menuSyncApi = new \FleekdashMenuSyncAPI();
        \do_action('fleekdash_menu_sync_registered', self::$menuSyncApi);

        // ALL OTHER ROUTES REGISTERED VIA ROUTEGROUPS!
        // See: includes/Routes/Api.php for complete V2.2 route registration
    }
}
