<?php

namespace App\Support;

class AppearanceRegistry
{
    /**
     * All appearance setting definitions.
     *
     * Each entry describes one setting. The database stores only the admin's
     * chosen value; the registry is the authoritative schema.
     *
     * Keys:
     *   group        — admin UI tab: branding | colors | typography | homepage | advanced
     *   type         — control type: color | integer | boolean | string | select | media | css
     *   label        — human-readable label shown in admin UI
     *   default      — value used when the admin has never touched this setting
     *   theme        — null = global (all themes); 'tailwind' = scoped to that theme only
     *   options      — extra constraints / choices depending on type:
     *                    select  → ['ChoiceA', 'ChoiceB', ...]
     *                    integer → ['min' => X, 'max' => Y]
     *   sort_order   — display order within the group (lower = first)
     */
    public static function definitions(): array
    {
        return [

            // ── Branding (global) ──────────────────────────────────────────────

            'branding.logo_max_width' => [
                'group'      => 'branding',
                'type'       => 'integer',
                'label'      => 'Logo Max Width (px)',
                'default'    => null,
                'theme'      => null,
                'options'    => ['min' => 80, 'max' => 500],
                'sort_order' => 10,
            ],

            'branding.logo_alt' => [
                'group'      => 'branding',
                'type'       => 'string',
                'label'      => 'Logo Alt Text',
                'default'    => null,
                'theme'      => null,
                'options'    => null,
                'sort_order' => 20,
            ],

            'branding.logo_dark' => [
                'group'      => 'branding',
                'type'       => 'media',
                'label'      => 'Dark Mode Logo',
                'default'    => null,
                'theme'      => null,
                'options'    => null,
                'sort_order' => 30,
            ],

            // ── Colors (tailwind-scoped) ───────────────────────────────────────

            'colors.primary' => [
                'group'      => 'colors',
                'type'       => 'color',
                'label'      => 'Primary',
                'default'    => '#364d6d',
                'theme'      => 'tailwind',
                'options'    => null,
                'sort_order' => 10,
            ],

            'colors.primary_dark' => [
                'group'      => 'colors',
                'type'       => 'color',
                'label'      => 'Primary Dark',
                'default'    => '#2a3b55',
                'theme'      => 'tailwind',
                'options'    => null,
                'sort_order' => 20,
            ],

            'colors.accent' => [
                'group'      => 'colors',
                'type'       => 'color',
                'label'      => 'Accent',
                'default'    => '#DDC393',
                'theme'      => 'tailwind',
                'options'    => null,
                'sort_order' => 30,
            ],

            'colors.background_light' => [
                'group'      => 'colors',
                'type'       => 'color',
                'label'      => 'Background (light mode)',
                'default'    => '#fafaf9',
                'theme'      => 'tailwind',
                'options'    => null,
                'sort_order' => 40,
            ],

            'colors.background_dark' => [
                'group'      => 'colors',
                'type'       => 'color',
                'label'      => 'Background (dark mode)',
                'default'    => '#16181c',
                'theme'      => 'tailwind',
                'options'    => null,
                'sort_order' => 50,
            ],

            'colors.surface_light' => [
                'group'      => 'colors',
                'type'       => 'color',
                'label'      => 'Surface (light mode)',
                'default'    => '#ffffff',
                'theme'      => 'tailwind',
                'options'    => null,
                'sort_order' => 60,
            ],

            'colors.surface_dark' => [
                'group'      => 'colors',
                'type'       => 'color',
                'label'      => 'Surface (dark mode)',
                'default'    => '#23262b',
                'theme'      => 'tailwind',
                'options'    => null,
                'sort_order' => 70,
            ],

            'colors.border_light' => [
                'group'      => 'colors',
                'type'       => 'color',
                'label'      => 'Border (light mode)',
                'default'    => '#e5e7eb',
                'theme'      => 'tailwind',
                'options'    => null,
                'sort_order' => 80,
            ],

            'colors.border_dark' => [
                'group'      => 'colors',
                'type'       => 'color',
                'label'      => 'Border (dark mode)',
                'default'    => '#374151',
                'theme'      => 'tailwind',
                'options'    => null,
                'sort_order' => 90,
            ],

            'colors.text_main' => [
                'group'      => 'colors',
                'type'       => 'color',
                'label'      => 'Text — Primary',
                'default'    => '#121417',
                'theme'      => 'tailwind',
                'options'    => null,
                'sort_order' => 100,
            ],

            'colors.text_muted' => [
                'group'      => 'colors',
                'type'       => 'color',
                'label'      => 'Text — Muted',
                'default'    => '#647082',
                'theme'      => 'tailwind',
                'options'    => null,
                'sort_order' => 110,
            ],

            // ── Typography (tailwind-scoped) ───────────────────────────────────

            'typography.font_family' => [
                'group'      => 'typography',
                'type'       => 'select',
                'label'      => 'Font Family',
                'default'    => 'Inter',
                'theme'      => 'tailwind',
                'options'    => ['Inter', 'Roboto', 'Poppins', 'Lato', 'Open Sans'],
                'sort_order' => 10,
            ],

            'typography.base_font_size' => [
                'group'      => 'typography',
                'type'       => 'select',
                'label'      => 'Base Font Size (px)',
                'default'    => '16',
                'theme'      => 'tailwind',
                'options'    => ['14', '15', '16', '17', '18'],
                'sort_order' => 20,
            ],

            'typography.heading_weight' => [
                'group'      => 'typography',
                'type'       => 'select',
                'label'      => 'Heading Weight',
                'default'    => '700',
                'theme'      => 'tailwind',
                'options'    => ['600', '700', '800'],
                'sort_order' => 30,
            ],

            // ── Advanced (global) ──────────────────────────────────────────────

            'advanced.custom_css' => [
                'group'      => 'advanced',
                'type'       => 'css',
                'label'      => 'Custom CSS',
                'default'    => null,
                'theme'      => null,
                'options'    => null,
                'sort_order' => 10,
            ],

        ];
    }

    public static function group(string $group): array
    {
        return array_filter(
            static::definitions(),
            fn($def) => $def['group'] === $group,
        );
    }

    public static function forTheme(?string $theme): array
    {
        return array_filter(
            static::definitions(),
            fn($def) => $def['theme'] === $theme,
        );
    }
}
