<?php
/**
 * Features Routes
 * Feature flags and license management
 */

namespace FleekDash\Routes\Groups;

use FleekDash\Libs\API\Route;
use FleekDash\Libs\API\RouteAuthMiddleware;
use FleekDash\Controllers\License\LicenseRestController;

class FeaturesRoutes
{
    public static function register()
    {
        \FleekDash\Services\LabsFeaturesService::init();

        // Load FeatureController if not loaded
        if (!class_exists('\FleekDash\API\FeatureController')) {
            require_once FLEEKDASH_PLUGIN_DIR . 'includes/API/FeatureController.php';
        }

        // Features - use callbacks that call getInstance() (singleton pattern)
        Route::get('features/user', function($request) {
            return \FleekDash\API\FeatureController::getInstance()->getUserFeatures($request);
        }, RouteAuthMiddleware::authenticated());

        Route::post('features/check', function($request) {
            return \FleekDash\API\FeatureController::getInstance()->checkFeature($request);
        }, RouteAuthMiddleware::authenticated());

        Route::post('features/check-limit', function($request) {
            return \FleekDash\API\FeatureController::getInstance()->checkFeatureLimit($request);
        }, RouteAuthMiddleware::authenticated());

        Route::get('features/usage', function($request) {
            return \FleekDash\API\FeatureController::getInstance()->getFeatureUsage($request);
        }, RouteAuthMiddleware::authenticated());

        // License — REST layer in LicenseRestController (S1448); core logic in LicenseController.
        Route::get('license/status', function ($request) {
            return LicenseRestController::getInstance()->restGetLicenseStatus($request);
        }, RouteAuthMiddleware::admin(), [
            'force_check' => [
                'required' => false,
                'default' => false,
                'type' => 'boolean',
                'sanitize_callback' => static function ($value) {
                    if ($value === null || $value === '') {
                        return false;
                    }

                    return filter_var($value, FILTER_VALIDATE_BOOLEAN);
                },
            ],
            'license_key' => [
                'required' => false,
                'type' => 'string',
            ],
        ]);

        Route::post('license/verify', function($request) {
            return LicenseRestController::getInstance()->restVerifyLicense($request);
        }, RouteAuthMiddleware::admin(), [
            'force_check' => [
                'required' => false,
                'default' => false,
                'type' => 'boolean',
                'sanitize_callback' => static function ($value) {
                    if ($value === null || $value === '') {
                        return false;
                    }

                    return filter_var($value, FILTER_VALIDATE_BOOLEAN);
                },
            ],
            'license_key' => [
                'required' => false,
                'type' => 'string',
            ],
        ]);

        Route::post('license/activate', function($request) {
            return LicenseRestController::getInstance()->restActivateLicense($request);
        }, RouteAuthMiddleware::admin());

        Route::post('license/deactivate', function($request) {
            return LicenseRestController::getInstance()->restDeactivateLicense($request);
        }, RouteAuthMiddleware::admin());

        Route::post('license/clear-cache', function($request) {
            return LicenseRestController::getInstance()->restClearCache($request);
        }, RouteAuthMiddleware::admin());

        // Service Worker
        Route::get('service-worker', function() {
            return \rest_ensure_response([
                'success' => true,
                'data' => ['service_worker' => 'active']
            ]);
        }, RouteAuthMiddleware::public());

        // Core update/reinstall: FleekDashCoreUpdate (includes/API/class-core-update.php) on rest_api_init.
    }
}
