<?php

namespace FleekDash\Services\Theme;

defined('ABSPATH') || exit;

/**
 * ThemeRegistry
 *
 * Holds metadata for bundled FleekDash themes and palettes.
 * Bundled assets are immutable and delivered with the plugin.
 */
class ThemeRegistry
{
    /**
     * Bundled theme definitions.
     *
     * @var array<string, array<string, mixed>>
     */
    private const THEMES = [
        'starter' => [
            'id' => 'starter',
            'name' => 'Fleekdash Starter',
            'description' => 'Clean and professional baseline experience for FleekDash.',
            'author' => 'Fleekdash Team',
            'version' => '1.0.0',
            'default_palette' => 'starter-fresh',
            'palettes' => [
                'starter-fresh',
                'starter-modern',
                'starter-sunrise',
                'starter-coffee',
                'starter-midnight',
                'starter-ocean',
            ],
        ],
        'modern' => [
            'id' => 'modern',
            'name' => 'Fleekdash Modern',
            'description' => 'Vibrant and rounded UI focused on ecommerce and analytics.',
            'author' => 'Fleekdash Team',
            'version' => '1.0.0',
            'default_palette' => 'modern-fresh',
            'palettes' => [
                'modern-fresh',
                'modern-neon',
                'modern-coffee',
                'modern-midnight',
                'modern-ocean',
            ],
        ],
    ];

    /**
     * Bundled palette definitions.
     *
     * @var array<string, array<string, mixed>>
     */
    private const PALETTES = [
        'starter-fresh' => [
            'id' => 'starter-fresh',
            'theme' => 'starter',
            'name' => 'Fresh',
            'description' => 'Classic WordPress admin color scheme.',
            'source' => 'bundled',
            'wp_sync_key' => 'fresh',
        ],
        'starter-modern' => [
            'id' => 'starter-modern',
            'theme' => 'starter',
            'name' => 'Modern',
            'description' => 'WordPress 7.0 default admin color scheme (pink accent).',
            'source' => 'bundled',
            'wp_sync_key' => 'modern',
            'mode_sync_variables' => [
                '--primary-foreground',
                '--accent-foreground',
                '--primary',
                '--accent',
                '--main-gradient',
            ],
        ],
        'starter-sunrise' => [
            'id' => 'starter-sunrise',
            'theme' => 'starter',
            'name' => 'Sunrise',
            'description' => 'Warm gradient inspired by early morning dashboards.',
            'source' => 'bundled',
            'wp_sync_key' => 'sunrise',
        ],
        'starter-coffee' => [
            'id' => 'starter-coffee',
            'theme' => 'starter',
            'name' => 'Coffee',
            'description' => 'Earthy, high-contrast palette ideal for focus sessions.',
            'source' => 'bundled',
            'wp_sync_key' => 'coffee',
        ],
        'starter-midnight' => [
            'id' => 'starter-midnight',
            'theme' => 'starter',
            'name' => 'Midnight',
            'description' => 'Deep blues with balanced contrast for dark mode.',
            'source' => 'bundled',
            'wp_sync_key' => 'midnight',
        ],
        'starter-ocean' => [
            'id' => 'starter-ocean',
            'theme' => 'starter',
            'name' => 'Ocean',
            'description' => 'Cool aquatic scheme for relaxed analytics reviews.',
            'source' => 'bundled',
            'wp_sync_key' => 'ocean',
        ],
        'modern-fresh' => [
            'id' => 'modern-fresh',
            'theme' => 'modern',
            'name' => 'Fresh',
            'description' => 'Bright neutral tones paired with Fleekdash purple accent.',
            'source' => 'bundled',
            'wp_sync_key' => 'fresh',
        ],
        'modern-neon' => [
            'id' => 'modern-neon',
            'theme' => 'modern',
            'name' => 'Neon',
            'description' => 'Electric gradients for vibrant storefront analytics.',
            'source' => 'bundled',
            'wp_sync_key' => 'ectoplasm',
        ],
        'modern-coffee' => [
            'id' => 'modern-coffee',
            'theme' => 'modern',
            'name' => 'Coffee',
            'description' => 'Rich browns that echo artisan brand dashboards.',
            'source' => 'bundled',
            'wp_sync_key' => 'coffee',
        ],
        'modern-midnight' => [
            'id' => 'modern-midnight',
            'theme' => 'modern',
            'name' => 'Midnight',
            'description' => 'High-contrast indigo tones for late-night sprints.',
            'source' => 'bundled',
            'wp_sync_key' => 'midnight',
        ],
        'modern-ocean' => [
            'id' => 'modern-ocean',
            'theme' => 'modern',
            'name' => 'Ocean',
            'description' => 'Teal and cyan blend tailored to SaaS analytics.',
            'source' => 'bundled',
            'wp_sync_key' => 'blue',
        ],
    ];

    /**
     * WordPress admin color → FleekDash palette mapping.
     *
     * @var array<string, array<string, string>>
     */
    private const WP_SYNC_MAP = [
        'modern' => [
            'starter' => 'starter-modern',
            'modern' => 'modern-fresh',
        ],
        'fresh' => [
            'starter' => 'starter-fresh',
            'modern' => 'modern-fresh',
        ],
        'light' => [
            'starter' => 'starter-sunrise',
            'modern' => 'modern-fresh',
        ],
        'blue' => [
            'starter' => 'starter-ocean',
            'modern' => 'modern-ocean',
        ],
        'coffee' => [
            'starter' => 'starter-coffee',
            'modern' => 'modern-coffee',
        ],
        'ectoplasm' => [
            'starter' => 'starter-midnight',
            'modern' => 'modern-neon',
        ],
        'midnight' => [
            'starter' => 'starter-midnight',
            'modern' => 'modern-midnight',
        ],
        'ocean' => [
            'starter' => 'starter-ocean',
            'modern' => 'modern-ocean',
        ],
        'sunrise' => [
            'starter' => 'starter-sunrise',
            'modern' => 'modern-neon',
        ],
    ];

    /**
     * Retrieve all bundled themes.
     *
     * @return array<int, array<string, mixed>>
     */
    public static function getBundledThemes(): array
    {
        return array_values(self::THEMES);
    }

    /**
     * Retrieve a bundled theme by ID.
     */
    public static function getBundledTheme(string $themeId): ?array
    {
        $themeId = \sanitize_key($themeId);
        if ($themeId === '' || !isset(self::THEMES[$themeId])) {
            return null;
        }

        return self::THEMES[$themeId];
    }

    /**
     * Retrieve all bundled palettes.
     *
     * @return array<int, array<string, mixed>>
     */
    public static function getBundledPalettes(): array
    {
        return array_values(self::PALETTES);
    }

    /**
     * Retrieve a bundled palette by ID.
     */
    public static function getBundledPalette(string $paletteId): ?array
    {
        $paletteId = \sanitize_key($paletteId);
        if ($paletteId === '' || !isset(self::PALETTES[$paletteId])) {
            return null;
        }

        return self::PALETTES[$paletteId];
    }

    /**
     * Get default palette ID for a bundled theme.
     */
    public static function getDefaultPaletteId(string $themeId): string
    {
        $theme = self::getBundledTheme($themeId);

        return $theme['default_palette'] ?? 'starter-fresh';
    }

    /**
     * Retrieve palette IDs associated with a bundled theme.
     *
     * @return array<int, string>
     */
    public static function getPaletteIdsForTheme(string $themeId): array
    {
        $theme = self::getBundledTheme($themeId);
        if (!$theme) {
            return [];
        }

        return $theme['palettes'] ?? [];
    }

    /**
     * Resolve FleekDash palette ID for given WordPress palette.
     */
    public static function getSyncedPaletteId(string $themeId, string $wpPaletteKey): ?string
    {
        $themeId = \sanitize_key($themeId);
        $wpPaletteKey = \sanitize_key($wpPaletteKey);

        $result = null;
        $canResolve = $themeId !== '' && $wpPaletteKey !== '';

        if ($canResolve) {
            $map = self::WP_SYNC_MAP[$wpPaletteKey] ?? null;
            $paletteId = is_array($map) ? ($map[$themeId] ?? null) : null;
            if ($paletteId !== null && isset(self::PALETTES[$paletteId])) {
                $result = $paletteId;
            }
        }

        return $result;
    }
}
