<?php

namespace WPUltimateSecurity\UltimateSecurity\Presentation\API\Routes;

use WPUltimateSecurity\UltimateSecurity\Infrastructure\Security\Capabilities;
use WPUltimateSecurity\UltimateSecurity\Presentation\API\Controllers\OptionsController;
use WPUltimateSecurity\UltimateSecurity\Presentation\API\Controllers\SettingsController;

/**
 * Settings Routes
 *
 * Registers REST API routes for settings management:
 * - Plugin options CRUD
 * - Recovery URL
 * - Encryption keys
 * - Cache management
 *
 * @package WPUltimateSecurity\UltimateSecurity\Presentation\API\Routes
 * @since 2.0.0
 */
class SettingsRoutes extends AbstractRouteRegistrar
{
    /**
     * {@inheritDoc}
     */
    public function register(): void
    {
        $this->registerOptionsRoutes();
        $this->registerCacheRoutes();
    }

    /**
     * Register options routes.
     *
     * @return void
     */
    private function registerOptionsRoutes(): void
    {
        // GET options
        $this->get(
            '/options',
            [OptionsController::class, 'get_options'],
            Capabilities::MANAGE_SETTINGS
        );

        // POST/UPDATE options
        $this->post(
            '/options',
            [OptionsController::class, 'update_options'],
            Capabilities::MANAGE_SETTINGS
        );

        // Save recovery URL
        $this->post(
            '/save-recovery-url',
            [OptionsController::class, 'save_recovery_url'],
            Capabilities::MANAGE_SETTINGS
        );

        // Get encryption database keys
        $this->get(
            '/get-enable-encryption-database-keys',
            [OptionsController::class, 'get_encryption_database_keys'],
            Capabilities::MANAGE_SETTINGS
        );
    }

    /**
     * Register cache management routes.
     *
     * @return void
     */
    private function registerCacheRoutes(): void
    {
        // Purge all caches
        $this->post(
            '/settings/purge-cache',
            function () {
                $controller = new SettingsController();
                $controller->purge_caches();

                return new \WP_REST_Response([
                    'success' => true,
                    'message' => __('All caches have been purged successfully.', 'ultimate-security'),
                ], 200);
            },
            Capabilities::MANAGE_SETTINGS
        );
    }
}
