<?php

namespace WPUltimateSecurity\UltimateSecurity\Presentation\API\Routes;

/**
 * Route Manager
 *
 * Central orchestrator for REST API route registration.
 * Manages all route registrars and provides a clean API for registering routes.
 *
 * Usage:
 * ```php
 * // In ApiServiceProvider or RestAPI::register()
 * add_action('rest_api_init', function () {
 *     RouteManager::getInstance()->registerAll();
 * });
 *
 * // Or register specific route groups
 * RouteManager::getInstance()
 *     ->add(new AuthenticationRoutes())
 *     ->add(new SettingsRoutes())
 *     ->registerAll();
 * ```
 *
 * @package WPUltimateSecurity\UltimateSecurity\Presentation\API\Routes
 * @since 2.0.0
 */
class RouteManager
{
    /**
     * Singleton instance.
     *
     * @var self|null
     */
    private static ?self $instance = null;

    /**
     * Registered route registrars.
     *
     * @var array<RouteRegistrarInterface>
     */
    private array $registrars = [];

    /**
     * Whether routes have been registered.
     *
     * @var bool
     */
    private bool $registered = false;

    /**
     * Private constructor.
     */
    private function __construct()
    {
        // Load default registrars
        $this->loadDefaults();
    }

    /**
     * Get the singleton instance.
     *
     * @return self
     */
    public static function getInstance(): self
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /**
     * Load default route registrars.
     *
     * @return void
     */
    private function loadDefaults(): void
    {
        // Add core route registrars
        // Note: These are new modular registrars that can be used alongside
        // the existing RestAPI.php routes for gradual migration

        // These will be uncommented as routes are migrated:
        $this->add(new AuthenticationRoutes());
        // $this->add(new SettingsRoutes());
        // $this->add(new SecurityRoutes());

        // Allow extensions to add registrars
        do_action('ultimate_security_register_routes', $this);
    }

    /**
     * Add a route registrar.
     *
     * @param RouteRegistrarInterface $registrar The registrar to add.
     *
     * @return self
     */
    public function add(RouteRegistrarInterface $registrar): self
    {
        $this->registrars[] = $registrar;

        return $this;
    }

    /**
     * Register all routes from all registrars.
     *
     * @return void
     */
    public function registerAll(): void
    {
        if ($this->registered) {
            return;
        }

        foreach ($this->registrars as $registrar) {
            $registrar->register();
        }

        $this->registered = true;

        do_action('ultimate_security_routes_registered', $this);
    }

    /**
     * Get all registered registrars.
     *
     * @return array<RouteRegistrarInterface>
     */
    public function getRegistrars(): array
    {
        return $this->registrars;
    }

    /**
     * Check if routes have been registered.
     *
     * @return bool
     */
    public function isRegistered(): bool
    {
        return $this->registered;
    }

    /**
     * Reset the manager (primarily for testing).
     *
     * @return void
     */
    public static function reset(): void
    {
        self::$instance = null;
    }

    /**
     * Get the REST API namespace.
     *
     * @return string
     */
    public static function getNamespace(): string
    {
        return 'ultimate-security/v1';
    }
}
