<?php

namespace WPSN\Modules\Diagnostics;

defined('ABSPATH') || exit;

final class QueueLiveRunnerProfiles
{
    public const DEFAULT_PROFILE = 'medium';

    /**
     * @return array<string,array<string,mixed>>
     */
    public static function definitions(): array
    {
        return [
            'watch' => [
                'key' => 'watch',
                'label' => __('Watch only', 'wp-seo-next'),
                'description' => __('Refresh progress without running new queue batches.', 'wp-seo-next'),
                'interval_seconds' => 15,
                'max_jobs' => 0,
                'max_seconds' => 0,
                'auto_run' => false,
            ],
            'minute' => [
                'key' => 'minute',
                'label' => __('Every 1 min', 'wp-seo-next'),
                'description' => __('Every 60 seconds · safest option when admin-ajax is unstable.', 'wp-seo-next'),
                'interval_seconds' => 60,
                'max_jobs' => 8,
                'max_seconds' => 6,
                'auto_run' => true,
            ],
            'relaxed' => [
                'key' => 'relaxed',
                'label' => __('Every 30 sec', 'wp-seo-next'),
                'description' => __('Every 30 seconds · lighter batches for slower shared hosting.', 'wp-seo-next'),
                'interval_seconds' => 30,
                'max_jobs' => 10,
                'max_seconds' => 8,
                'auto_run' => true,
            ],
            'slow' => [
                'key' => 'slow',
                'label' => __('Every 15 sec', 'wp-seo-next'),
                'description' => __('Every 15 seconds · smaller batches for safer shared hosting.', 'wp-seo-next'),
                'interval_seconds' => 15,
                'max_jobs' => 15,
                'max_seconds' => 8,
                'auto_run' => true,
            ],
            'medium' => [
                'key' => 'medium',
                'label' => __('Every 12 sec', 'wp-seo-next'),
                'description' => __('Every 12 seconds · balanced default with moderate batches.', 'wp-seo-next'),
                'interval_seconds' => 12,
                'max_jobs' => 25,
                'max_seconds' => 10,
                'auto_run' => true,
            ],
            'fast' => [
                'key' => 'fast',
                'label' => __('Every 10 sec', 'wp-seo-next'),
                'description' => __('Every 10 seconds · larger batches for quicker completion.', 'wp-seo-next'),
                'interval_seconds' => 10,
                'max_jobs' => 40,
                'max_seconds' => 12,
                'auto_run' => true,
            ],
        ];
    }

    public static function normalize(string $profile): string
    {
        $profile = sanitize_key($profile);
        $definitions = self::definitions();
        if (!isset($definitions[$profile])) {
            return self::DEFAULT_PROFILE;
        }

        return $profile;
    }

    /**
     * @return array<string,mixed>
     */
    public static function definition(string $profile): array
    {
        $definitions = self::definitions();
        $profile = self::normalize($profile);

        return $definitions[$profile] ?? $definitions[self::DEFAULT_PROFILE];
    }

    /**
     * @return array<string,array<string,mixed>>
     */
    public static function clientConfig(): array
    {
        $out = [];
        foreach (self::definitions() as $key => $definition) {
            $out[$key] = [
                'key' => (string) $key,
                'label' => (string) ($definition['label'] ?? $key),
                'description' => (string) ($definition['description'] ?? ''),
                'intervalSeconds' => (int) ($definition['interval_seconds'] ?? 12),
                'maxJobs' => (int) ($definition['max_jobs'] ?? 0),
                'maxSeconds' => (int) ($definition['max_seconds'] ?? 0),
                'autoRun' => !empty($definition['auto_run']),
            ];
        }

        return $out;
    }
}
