<?php

namespace FleekDash\Core;

use FleekDash\Controllers\DebugController;

/**
 * Registers FleekDash debug REST routes.
 * Extracted from DebugController for S1448 compliance.
 */
class DebugRouteRegistrar
{
    /**
     * @param callable $permissionCallback
     */
    public function registerAll($permissionCallback, DebugRouteHandlers $handlers)
    {
        $this->registerStatusRoutes($permissionCallback, $handlers->statusHandler, $handlers->logService);
        $this->registerLogRoutes($permissionCallback, $handlers->logHandler);
        $this->registerAutoClearRoutes($permissionCallback, $handlers->autoClearHandler);
        $this->registerCacheRoutes($permissionCallback, $handlers->cacheHandler);
        $this->registerMuCleanupRoutes($permissionCallback, $handlers->utilityHandler);
        $this->registerActivationRoutes($permissionCallback, $handlers->utilityHandler);
        $this->registerTestMigrationRoute($permissionCallback, $handlers->migrationTester);
        $this->registerPhpNoticesRoutes($permissionCallback, $handlers->utilityHandler);
    }

    /**
     * @param callable $permissionCallback
     */
    private function registerStatusRoutes($permissionCallback, DebugStatusHandler $statusHandler, DebugLogService $logService)
    {
        \register_rest_route(DebugController::API_NAMESPACE, '/debug/status', [
            'methods' => 'GET',
            'callback' => [$statusHandler, 'getDebugStatus'],
            'permission_callback' => $permissionCallback,
        ]);

        \register_rest_route(DebugController::API_NAMESPACE, '/debug/status', [
            'methods' => 'POST',
            'callback' => [$statusHandler, 'updateDebugStatus'],
            'permission_callback' => $permissionCallback,
            'args' => [
                'enabled' => ['required' => true, 'type' => 'boolean'],
                'clear_log' => ['required' => false, 'type' => 'boolean', 'default' => true],
            ],
        ]);

        \register_rest_route(DebugController::API_NAMESPACE, '/debug/clear-log', [
            'methods' => 'POST',
            'callback' => [$logService, 'clearDebugLogEndpoint'],
            'permission_callback' => $permissionCallback,
            'args' => [
                'delete_file' => ['required' => false, 'type' => 'boolean', 'default' => false],
                'create_backup' => ['required' => false, 'type' => 'boolean', 'default' => false],
            ],
        ]);
    }

    /**
     * @param callable $permissionCallback
     */
    private function registerLogRoutes($permissionCallback, DebugLogHandler $logHandler)
    {
        \register_rest_route(DebugController::API_NAMESPACE, '/debug/copy-to-uploads', [
            'methods' => 'POST',
            'callback' => [$logHandler, 'copyDebugLogToUploads'],
            'permission_callback' => $permissionCallback,
        ]);

        \register_rest_route(DebugController::API_NAMESPACE, '/debug/download-url', [
            'methods' => 'GET',
            'callback' => [$logHandler, 'getDebugLogDownloadUrl'],
            'permission_callback' => $permissionCallback,
        ]);

        \register_rest_route(DebugController::API_NAMESPACE, '/debug/download-latest', [
            'methods' => 'GET',
            'callback' => [$logHandler, 'downloadLatestDebugLog'],
            'permission_callback' => $permissionCallback,
        ]);
    }

    /**
     * @param callable $permissionCallback
     */
    private function registerAutoClearRoutes($permissionCallback, DebugAutoClearHandler $autoClearHandler)
    {
        \register_rest_route(DebugController::API_NAMESPACE, '/debug/auto-clear-settings', [
            'methods' => 'GET',
            'callback' => [$autoClearHandler, 'getAutoClearSettings'],
            'permission_callback' => $permissionCallback,
        ]);

        \register_rest_route(DebugController::API_NAMESPACE, '/debug/auto-clear-settings', [
            'methods' => 'POST',
            'callback' => [$autoClearHandler, 'updateAutoClearSettings'],
            'permission_callback' => $permissionCallback,
            'args' => [
                'auto_clear_enabled' => ['required' => false, 'type' => 'boolean'],
                'auto_clear_on_toggle' => ['required' => false, 'type' => 'boolean'],
                'auto_clear_max_size_mb' => [
                    'required' => false,
                    'type' => 'number',
                    'minimum' => 1,
                    'maximum' => 100,
                ],
                'auto_clear_max_age_hours' => [
                    'required' => false,
                    'type' => 'number',
                    'minimum' => 1,
                    'maximum' => 168,
                ],
                'auto_clear_backup' => ['required' => false, 'type' => 'boolean'],
            ],
        ]);

        \register_rest_route(DebugController::API_NAMESPACE, '/debug/auto-clear-check', [
            'methods' => 'POST',
            'callback' => [$autoClearHandler, 'triggerAutoClearCheck'],
            'permission_callback' => $permissionCallback,
        ]);
    }

    /**
     * @param callable $permissionCallback
     */
    private function registerCacheRoutes($permissionCallback, DebugCacheHandler $cacheHandler)
    {
        \register_rest_route(DebugController::API_NAMESPACE, '/debug/rest-cache-info', [
            'methods' => 'GET',
            'callback' => [$cacheHandler, 'getRestCacheInfo'],
            'permission_callback' => $permissionCallback,
        ]);

        \register_rest_route(DebugController::API_NAMESPACE, '/debug/clear-etags', [
            'methods' => 'POST',
            'callback' => [$cacheHandler, 'clearEtags'],
            'permission_callback' => $permissionCallback,
        ]);

        \register_rest_route(DebugController::API_NAMESPACE, '/debug/clear-all-cache', [
            'methods' => 'POST',
            'callback' => [$cacheHandler, 'clearAllCache'],
            'permission_callback' => $permissionCallback,
        ]);
    }

    /**
     * @param callable $permissionCallback
     */
    private function registerMuCleanupRoutes($permissionCallback, DebugUtilityHandler $utilityHandler)
    {
        \register_rest_route(DebugController::API_NAMESPACE, '/debug/mu-cleanup/status', [
            'methods' => 'GET',
            'callback' => [$utilityHandler, 'getMuCleanupStatus'],
            'permission_callback' => $permissionCallback,
        ]);

        \register_rest_route(DebugController::API_NAMESPACE, '/debug/mu-cleanup/run', [
            'methods' => 'POST',
            'callback' => [$utilityHandler, 'runMuCleanup'],
            'permission_callback' => $permissionCallback,
        ]);
    }

    /**
     * @param callable $permissionCallback
     */
    private function registerActivationRoutes($permissionCallback, DebugUtilityHandler $utilityHandler)
    {
        \register_rest_route(DebugController::API_NAMESPACE, '/debug/activation-status', [
            'methods' => 'GET',
            'callback' => [$utilityHandler, 'getActivationStatus'],
            'permission_callback' => $permissionCallback,
        ]);

        \register_rest_route(DebugController::API_NAMESPACE, '/debug/activation-trigger', [
            'methods' => 'POST',
            'callback' => [$utilityHandler, 'triggerActivationRunner'],
            'permission_callback' => $permissionCallback,
        ]);
    }

    /**
     * @param callable $permissionCallback
     */
    private function registerTestMigrationRoute($permissionCallback, GrowthMigrationTester $migrationTester)
    {
        \register_rest_route(DebugController::API_NAMESPACE, '/debug/test-growth-migration', [
            'methods' => 'GET',
            'callback' => [$migrationTester, 'runTests'],
            'permission_callback' => $permissionCallback,
        ]);
    }

    /**
     * @param callable $permissionCallback
     */
    private function registerPhpNoticesRoutes($permissionCallback, DebugUtilityHandler $utilityHandler)
    {
        \register_rest_route(DebugController::API_NAMESPACE, '/debug/php-notices-settings', [
            'methods' => 'GET',
            'callback' => [$utilityHandler, 'getPhpNoticesSettings'],
            'permission_callback' => $permissionCallback,
        ]);

        \register_rest_route(DebugController::API_NAMESPACE, '/debug/php-notices-settings', [
            'methods' => 'POST',
            'callback' => [$utilityHandler, 'updatePhpNoticesSettings'],
            'permission_callback' => $permissionCallback,
            'args' => [
                'hide_php_notices' => [
                    'required' => true,
                    'type' => 'boolean',
                    'description' => 'Whether to filter routine diagnostic noise from debug.log',
                ],
            ],
        ]);
    }
}
