<?php

namespace WPSN\Core\Queue;

defined('ABSPATH') || exit;

/**
 * Remote Queue Runner configuration.
 *
 * Stored OUTSIDE the main Settings option intentionally:
 * - do not leak token into Config Sync / Snapshots
 * - operational, not SEO output
 */
final class QueueRemoteRunnerConfig
{
    public const OPT_ENABLED = 'wpsn_queue_remote_enabled';
    public const OPT_TOKEN = 'wpsn_queue_remote_token';
    public const OPT_MAX_JOBS = 'wpsn_queue_remote_max_jobs';
    public const OPT_RATE_LIMIT = 'wpsn_queue_remote_rate_limit';
    public const OPT_RATE_WINDOW = 'wpsn_queue_remote_rate_window';

    public static function enabled(): bool
    {
        return (bool) get_option(self::OPT_ENABLED, false);
    }

    public static function token(): string
    {
        $t = (string) get_option(self::OPT_TOKEN, '');
        $t = trim($t);
        return $t !== '' ? $t : '';
    }

    public static function ensureToken(): string
    {
        $t = self::token();
        if ($t !== '') {
            return $t;
        }

        $t = function_exists('wp_generate_password')
            ? (string) wp_generate_password(32, false, false)
            : bin2hex(random_bytes(16));

        update_option(self::OPT_TOKEN, $t, false);
        return $t;
    }

    public static function regenerateToken(): string
    {
        $t = function_exists('wp_generate_password')
            ? (string) wp_generate_password(32, false, false)
            : bin2hex(random_bytes(16));
        update_option(self::OPT_TOKEN, $t, false);
        return $t;
    }

    public static function maxJobs(): int
    {
        $v = (int) get_option(self::OPT_MAX_JOBS, 3);
        if ($v < 1) {
            $v = 1;
        }
        if ($v > 20) {
            $v = 20;
        }
        return $v;
    }

    public static function rateLimit(): int
    {
        $v = (int) get_option(self::OPT_RATE_LIMIT, 120);
        if ($v < 10) {
            $v = 10;
        }
        if ($v > 5000) {
            $v = 5000;
        }
        return $v;
    }

    public static function rateWindowSeconds(): int
    {
        $v = (int) get_option(self::OPT_RATE_WINDOW, 3600);
        if ($v < 60) {
            $v = 60;
        }
        if ($v > 7 * DAY_IN_SECONDS) {
            $v = 7 * DAY_IN_SECONDS;
        }
        return $v;
    }

    public static function enable(int $maxJobs = 3, int $rateLimit = 120, int $rateWindowSeconds = 3600, bool $regenerateToken = false): void
    {
        update_option(self::OPT_ENABLED, 1, false);

        $maxJobs = max(1, min(20, $maxJobs));
        update_option(self::OPT_MAX_JOBS, $maxJobs, false);

        $rateLimit = max(10, min(5000, $rateLimit));
        update_option(self::OPT_RATE_LIMIT, $rateLimit, false);

        $rateWindowSeconds = max(60, min(7 * DAY_IN_SECONDS, $rateWindowSeconds));
        update_option(self::OPT_RATE_WINDOW, $rateWindowSeconds, false);

        if ($regenerateToken) {
            self::regenerateToken();
        } else {
            self::ensureToken();
        }
    }

    public static function disable(): void
    {
        update_option(self::OPT_ENABLED, 0, false);
    }
}
