<?php

namespace FleekDash\Services\Theme;

defined('ABSPATH') || exit;

/**
 * PalettePresets
 *
 * Provides light/dark CSS variable maps for bundled palettes.
 */
class PalettePresets
{
    private const COLOR_WHITE = '#FFFFFF';
    private const COLOR_INDIGO_500 = '#6366F1';
    private const COLOR_ZINC_900 = '#18181B';
    private const COLOR_GRAY_200 = '#E5E7EB';
    private const COLOR_SLATE_900 = '#0F172A';
    private const COLOR_SLATE_50 = '#F8FAFC';
    private const COLOR_SKY_100 = '#E0F2FE';
    private const COLOR_STARTER_BORDER_LIGHT = 'rgba(88, 96, 116, 0.12)';
    private const COLOR_STARTER_CARD_FOREGROUND = '#071437';
    private const COLOR_WP7_MODERN_ACCENT = '#FD356E';

    /**
     * Palette preset definitions.
     *
     * @var array<string, array<string, array<string, string>>>
     */
    private const PRESETS = [
        'starter-fresh' => [
            'light' => [
                '--primary' => '#6200EE',
                '--primary-foreground' => self::COLOR_WHITE,
                '--accent' => '#9D00FF',
                '--accent-foreground' => self::COLOR_WHITE,
                '--background' => self::COLOR_WHITE,
                '--foreground' => '#4B5675',
                '--border' => self::COLOR_STARTER_BORDER_LIGHT,
                '--card' => self::COLOR_WHITE,
                '--card-foreground' => self::COLOR_STARTER_CARD_FOREGROUND,
                '--muted-foreground' => 'rgba(75, 86, 117, 0.6)',
                '--input' => '#444D644D',
            ],
            'dark' => [
                '--primary' => '#8C00FF',
                '--primary-foreground' => self::COLOR_WHITE,
                '--accent' => '#D700FF',
                '--accent-foreground' => self::COLOR_WHITE,
                '--background' => '#1A2123',
                '--foreground' => '#ACB7CDCF',
                '--border' => '#815B831F',
                '--card' => '#1A2123',
                '--card-foreground' => self::COLOR_WHITE,
                '--muted-foreground' => 'rgba(172, 183, 205, 0.65)',
                '--input' => '#BA8BC440',
            ],
        ],
        'starter-modern' => [
            'light' => [
                '--primary' => self::COLOR_WP7_MODERN_ACCENT,
                '--primary-foreground' => self::COLOR_WHITE,
                '--accent' => self::COLOR_WP7_MODERN_ACCENT,
                '--accent-foreground' => self::COLOR_WHITE,
                '--background' => self::COLOR_WHITE,
                '--foreground' => '#4B5675',
                '--border' => self::COLOR_STARTER_BORDER_LIGHT,
                '--card' => self::COLOR_WHITE,
                '--card-foreground' => self::COLOR_STARTER_CARD_FOREGROUND,
                '--muted' => '#E6E8F5',
                '--muted-foreground' => 'rgba(75, 86, 117, 0.6)',
                '--input' => '#444D644D',
                '--gradient-orb-opacity' => '0',
            ],
            'dark' => [
                '--primary' => self::COLOR_WP7_MODERN_ACCENT,
                '--primary-foreground' => self::COLOR_WHITE,
                '--accent' => self::COLOR_WP7_MODERN_ACCENT,
                '--accent-foreground' => self::COLOR_WHITE,
                '--background' => '#19191C',
                '--foreground' => '#ACB7CDCF',
                '--border' => '#815B831F',
                '--card' => '#19191C',
                '--card-foreground' => self::COLOR_WHITE,
                '--muted' => 'rgba(172, 183, 205, 0.2)',
                '--muted-foreground' => 'rgba(172, 183, 205, 0.65)',
                '--input' => '#BA8BC440',
                '--gradient-orb-opacity' => '0.06',
            ],
        ],
        'modern-fresh' => [
            'light' => [
                '--primary' => self::COLOR_INDIGO_500,
                '--primary-foreground' => self::COLOR_WHITE,
                '--accent' => '#F4F4F5',
                '--accent-foreground' => self::COLOR_ZINC_900,
                '--background' => self::COLOR_WHITE,
                '--foreground' => self::COLOR_ZINC_900,
                '--border' => '#E4E4E7',
                '--card' => self::COLOR_WHITE,
                '--card-foreground' => self::COLOR_ZINC_900,
                '--muted-foreground' => '#71717A',
                '--input' => '#E4E4E7',
            ],
            'dark' => [
                '--primary' => '#6200EE',
                '--primary-foreground' => self::COLOR_WHITE,
                '--accent' => '#AF00FF',
                '--accent-foreground' => self::COLOR_WHITE,
                '--background' => '#101722',
                '--foreground' => self::COLOR_GRAY_200,
                '--border' => '#FFFFFF09',
                '--card' => '#111827',
                '--card-foreground' => self::COLOR_GRAY_200,
                '--muted-foreground' => 'rgba(229, 231, 235, 0.7)',
                '--input' => '#374151',
            ],
        ],
        'starter-sunrise' => [
            'light' => [
                '--primary' => self::COLOR_INDIGO_500,
                '--accent' => '#F97316',
                '--background' => self::COLOR_WHITE,
                '--foreground' => self::COLOR_SLATE_900,
                '--border' => self::COLOR_STARTER_BORDER_LIGHT,
                '--card' => self::COLOR_WHITE,
                '--card-foreground' => self::COLOR_STARTER_CARD_FOREGROUND,
                '--muted-foreground' => 'rgba(15, 23, 42, 0.55)',
                '--input' => '#FDEAD4',
            ],
            'dark' => [
                '--primary' => '#8B5CF6',
                '--accent' => '#FB923C',
                '--background' => '#111827',
                '--foreground' => '#E5E7EB',
                '--border' => 'rgba(99, 102, 241, 0.28)',
                '--card' => '#121826',
                '--card-foreground' => '#FFFFFF',
                '--muted-foreground' => 'rgba(224, 231, 255, 0.65)',
                '--input' => 'rgba(147, 197, 253, 0.25)',
            ],
        ],
        'starter-coffee' => [
            'light' => [
                '--primary' => '#8B5A2B',
                '--accent' => '#D4A373',
                '--background' => '#FCF5E9',
                '--foreground' => '#2F2414',
                '--border' => '#E8D5BC',
                '--card' => self::COLOR_WHITE,
                '--card-foreground' => '#2F2414',
                '--muted-foreground' => 'rgba(47, 36, 20, 0.65)',
                '--input' => '#E8D5BC',
            ],
            'dark' => [
                '--primary' => '#C68F5C',
                '--accent' => '#E6B98C',
                '--background' => '#1C130D',
                '--foreground' => '#F3E9DD',
                '--border' => 'rgba(208, 160, 112, 0.32)',
                '--card' => '#26180F',
                '--card-foreground' => '#F9EEE1',
                '--muted-foreground' => 'rgba(243, 233, 221, 0.65)',
                '--input' => 'rgba(208, 160, 112, 0.28)',
            ],
        ],
        'starter-midnight' => [
            'light' => [
                '--primary' => '#4338CA',
                '--accent' => '#0047D5',
                '--background' => self::COLOR_SLATE_50,
                '--foreground' => self::COLOR_SLATE_900,
                '--border' => '#CBD5F5',
                '--card' => self::COLOR_WHITE,
                '--card-foreground' => self::COLOR_SLATE_900,
                '--muted-foreground' => 'rgba(15, 23, 42, 0.6)',
                '--input' => '#CBD5F5',
            ],
            'dark' => [
                '--primary' => '#7C3AED',
                '--accent' => '#DF05E6',
                '--background' => '#090A15',
                '--foreground' => self::COLOR_SLATE_50,
                '--border' => 'rgba(124, 58, 237, 0.28)',
                '--card' => '#0F1624',
                '--card-foreground' => self::COLOR_SLATE_50,
                '--muted-foreground' => 'rgba(240, 244, 255, 0.65)',
                '--input' => 'rgba(90, 103, 216, 0.3)',
            ],
        ],
        'starter-ocean' => [
            'light' => [
                '--primary' => '#2563EB',
                '--accent' => '#38BDF8',
                '--background' => '#F0F9FF',
                '--foreground' => '#082F49',
                '--border' => '#BAE6FD',
                '--card' => self::COLOR_WHITE,
                '--card-foreground' => '#082F49',
                '--muted-foreground' => 'rgba(8, 47, 73, 0.6)',
                '--input' => '#BAE6FD',
            ],
            'dark' => [
                '--primary' => '#1E3A8A',
                '--accent' => '#22D3EE',
                '--background' => '#051923',
                '--foreground' => self::COLOR_SKY_100,
                '--border' => 'rgba(34, 211, 238, 0.28)',
                '--card' => '#082231',
                '--card-foreground' => self::COLOR_SKY_100,
                '--muted-foreground' => 'rgba(224, 242, 254, 0.6)',
                '--input' => 'rgba(37, 99, 235, 0.25)',
            ],
        ],
        'modern-neon' => [
            'light' => [
                '--primary' => '#8B5CF6',
                '--accent' => '#EC4899',
                '--background' => self::COLOR_SLATE_900,
                '--foreground' => self::COLOR_SLATE_50,
                '--border' => 'rgba(99, 102, 241, 0.35)',
                '--card' => 'rgba(15, 23, 42, 0.75)',
                '--card-foreground' => self::COLOR_SLATE_50,
                '--muted-foreground' => 'rgba(248, 250, 252, 0.7)',
                '--input' => 'rgba(148, 163, 184, 0.2)',
            ],
            'dark' => [
                '--primary' => '#A855F7',
                '--accent' => '#F472B6',
                '--background' => '#090C1F',
                '--foreground' => self::COLOR_WHITE,
                '--border' => 'rgba(168, 85, 247, 0.35)',
                '--card' => 'rgba(15, 23, 42, 0.9)',
                '--card-foreground' => self::COLOR_WHITE,
                '--muted-foreground' => 'rgba(248, 250, 252, 0.7)',
                '--input' => 'rgba(59, 130, 246, 0.25)',
            ],
        ],
        'modern-coffee' => [
            'light' => [
                '--primary' => '#B08968',
                '--accent' => '#DDB892',
                '--background' => '#2C1810',
                '--foreground' => '#F8F5F0',
                '--border' => 'rgba(221, 184, 146, 0.35)',
                '--card' => 'rgba(44, 24, 16, 0.9)',
                '--card-foreground' => '#F8F5F0',
                '--muted-foreground' => 'rgba(248, 245, 240, 0.7)',
                '--input' => 'rgba(221, 184, 146, 0.25)',
            ],
            'dark' => [
                '--primary' => '#DDB892',
                '--accent' => '#E6C7A6',
                '--background' => '#1B0E08',
                '--foreground' => '#FFF7EF',
                '--border' => 'rgba(221, 184, 146, 0.28)',
                '--card' => '#241109',
                '--card-foreground' => '#FFF7EF',
                '--muted-foreground' => 'rgba(255, 247, 239, 0.65)',
                '--input' => 'rgba(221, 184, 146, 0.22)',
            ],
        ],
        'modern-midnight' => [
            'light' => [
                '--primary' => '#4C1D95',
                '--accent' => '#3730A3',
                '--background' => '#EEF2FF',
                '--foreground' => self::COLOR_SLATE_900,
                '--border' => '#C7D2FE',
                '--card' => self::COLOR_WHITE,
                '--card-foreground' => self::COLOR_SLATE_900,
                '--muted-foreground' => 'rgba(15, 23, 42, 0.6)',
                '--input' => '#C7D2FE',
            ],
            'dark' => [
                '--primary' => self::COLOR_INDIGO_500,
                '--accent' => '#312E81',
                '--background' => '#050816',
                '--foreground' => '#F1F5F9',
                '--border' => 'rgba(99, 102, 241, 0.3)',
                '--card' => '#080C1B',
                '--card-foreground' => '#F1F5F9',
                '--muted-foreground' => 'rgba(241, 245, 249, 0.7)',
                '--input' => 'rgba(99, 102, 241, 0.25)',
            ],
        ],
        'modern-ocean' => [
            'light' => [
                '--primary' => '#0EA5E9',
                '--accent' => '#22D3EE',
                '--background' => '#051923',
                '--foreground' => self::COLOR_SKY_100,
                '--border' => 'rgba(34, 211, 238, 0.32)',
                '--card' => '#082231',
                '--card-foreground' => self::COLOR_SKY_100,
                '--muted-foreground' => 'rgba(224, 242, 254, 0.7)',
                '--input' => 'rgba(14, 165, 233, 0.3)',
            ],
            'dark' => [
                '--primary' => '#0284C7',
                '--accent' => '#06B6D4',
                '--background' => '#03121F',
                '--foreground' => '#CFFAFE',
                '--border' => 'rgba(6, 182, 212, 0.3)',
                '--card' => '#041927',
                '--card-foreground' => '#CFFAFE',
                '--muted-foreground' => 'rgba(207, 250, 254, 0.7)',
                '--input' => 'rgba(6, 182, 212, 0.25)',
            ],
        ],
    ];

    private const MAIN_GRADIENTS = [
        'starter-modern' => [
            'light' => 'linear-gradient(135deg, #441b90, #fd356e)',
            'dark' => 'linear-gradient(135deg, #441b90, #fd356e)',
        ],
        'starter-fresh' => [
            'light' => 'linear-gradient(135deg, #333399, #ff00cc)',
            'dark' => 'linear-gradient(135deg, #333399, #ff00cc)',
        ],
        'modern-fresh' => [
            'light' => 'linear-gradient(135deg, #6366F1, #8B5CF6)',
            'dark' => 'linear-gradient(135deg, #7f00ff, #e100ff)',
        ],
        'starter-sunrise' => [
            'light' => 'linear-gradient(135deg, #f46b45, #eea849)',
            'dark' => 'linear-gradient(135deg, #f37335, #fdc830)',
        ],
        'starter-coffee' => [
            'light' => 'linear-gradient(135deg, #ff7e5f, #feb47b)',
            'dark' => 'linear-gradient(135deg, #ec6f66, #f3a183)',
        ],
        'starter-midnight' => [
            'light' => 'linear-gradient(135deg, #2b32b2, #1488cc)',
            'dark' => 'linear-gradient(135deg, #3c1053, #ad5389)',
        ],
        'starter-ocean' => [
            'light' => 'linear-gradient(135deg, #0c4782, #1cb5e0)',
            'dark' => 'linear-gradient(135deg, #3a7bd5, #00d2ff)',
        ],
        'modern-neon' => [
            'light' =>
                'linear-gradient(135deg, #4013a4, #7303c0, #ec38bc, #ffa1e4)',
            'dark' => 'linear-gradient(135deg, #b91d73, #f953c6)',
        ],
        'modern-coffee' => [
            'light' => 'linear-gradient(135deg, #dd5e89, #f7bb97)',
            'dark' => 'linear-gradient(135deg, #ef629f, #eecda3)',
        ],
        'modern-midnight' => [
            'light' => 'linear-gradient(135deg, #2a0845, #6441a5)',
            'dark' => 'linear-gradient(135deg, #61045f, #aa076b)',
        ],
        'modern-ocean' => [
            'light' => 'linear-gradient(135deg, #0072ff, #00c6ff)',
            'dark' => 'linear-gradient(135deg, #3a7bd5, #00d2ff)',
        ],
    ];

    /**
     * Retrieve preset modes for a palette.
     *
     * @return array{light: array<string, string>, dark: array<string, string>}
     */
    public static function getModes(string $paletteId): array
    {
        $paletteId = \sanitize_key($paletteId);

        if (!isset(self::PRESETS[$paletteId])) {
            return [
                'light' => [],
                'dark' => [],
            ];
        }

        $preset = self::attachMainGradients(self::PRESETS[$paletteId], $paletteId);

        return [
            'light' => $preset['light'] ?? [],
            'dark' => $preset['dark'] ?? [],
        ];
    }

    private static function attachMainGradients(array $preset, string $paletteId): array
    {
        foreach (['light', 'dark'] as $mode) {
            $gradient = self::MAIN_GRADIENTS[$paletteId][$mode] ?? null;
            if ($gradient === null) {
                continue;
            }

            if (!isset($preset[$mode]) || !is_array($preset[$mode])) {
                $preset[$mode] = [];
            }

            $preset[$mode]['--main-gradient'] = $gradient;
        }

        return $preset;
    }
}
