<?php

namespace XZD\AI\V5\REST;

use WP_REST_Server;

/**
 * REST API 路由注册器
 */
class Router
{
    private static array $controllers = [];

    /**
     * 注册一个控制器
     */
    public static function register(string $controllerClass): void
    {
        if (!in_array($controllerClass, self::$controllers, true)) {
            self::$controllers[] = $controllerClass;
        }
    }

    /**
     * 初始化所有路由
     */
    public static function init(): void
    {
        add_action('rest_api_init', function () {
            foreach (self::$controllers as $controllerClass) {
                if (class_exists($controllerClass)) {
                    $controller = new $controllerClass();
                    if (method_exists($controller, 'registerRoutes')) {
                        $controller->registerRoutes();
                    }
                }
            }
        });
    }
}
