<?php
namespace Components\Support;

/**
 * Shared UI color palette for components.
 * Provides a Tailwind utility class mapping for common color tokens.
 *
 * Tokens: blue, indigo, orange, yellow, green, gray, purple, red, pink, brand
 * Each token maps to [bg, text, dot].
 */
class Palette
{
    /**
     * Return the palette mapping.
     * @return array<string, array{bg:string, text:string, dot:string, ring:string}>
     */
    public static function map(): array
    {
        return [
            'blue'   => ['bg' => 'bg-blue-100',   'text' => 'text-blue-600', 'pill-text' => 'text-white',   'dot' => 'bg-blue-600/70',   'ring' => 'ring-blue-500 ring-inset'],
            'indigo' => ['bg' => 'bg-indigo-100', 'text' => 'text-indigo-600','pill-text' => 'text-white', 'dot' => 'bg-indigo-500/70', 'ring' => 'ring-indigo-500 ring-inset'],
            'orange' => ['bg' => 'bg-orange-100', 'text' => 'text-orange-600','pill-text' => 'text-white', 'dot' => 'bg-orange-500/70 hover:bg-orange-600', 'ring' => 'ring-orange-300/70 ring-inset'],
            'orange-outline' => ['bg' => 'bg-white', 'text' => 'text-orange-600','pill-text' => 'text-orange-600', 'dot' => 'bg-white', 'ring' => 'ring-orange-500 ring-inset'],
            'yellow' => ['bg' => 'bg-yellow-100', 'text' => 'text-yellow-600','pill-text' => 'text-white', 'dot' => 'bg-yellow-500', 'ring' => 'ring-yellow-500 ring-inset'],
            'green'  => ['bg' => 'bg-green-50',  'text' => 'text-green-600','pill-text' => 'text-white',  'dot' => 'bg-green-600/70 hover:bg-green-600',  'ring' => 'ring-green-600/70 ring-inset'],
            'muted'  => ['bg' => 'bg-muted',       'text' => 'text-secondary/50','pill-text' => 'text-white', 'dot' => 'bg-secondary/50 hover:bg-secondary',   'ring' => 'ring-secondary/30 ring-inset'],
            'gray'   => ['bg' => 'bg-gray-100',   'text' => 'text-gray-600','pill-text' => 'text-white',   'dot' => 'bg-gray-500/70 hover:bg-gray-500',   'ring' => 'ring-gray-500/70 ring-inset'],
            'purple' => ['bg' => 'bg-purple-100', 'text' => 'text-purple-600','pill-text' => 'text-white', 'dot' => 'bg-purple-600/70', 'ring' => 'ring-purple-500 ring-inset'],
            'red'    => ['bg' => 'bg-red-100',    'text' => 'text-red-600','pill-text' => 'text-white',    'dot' => 'bg-red-500',    'ring' => 'ring-red-500 ring-inset'],
            'pink'   => ['bg' => 'bg-pink-100',   'text' => 'text-pink-600','pill-text' => 'text-white',   'dot' => 'bg-pink-500',   'ring' => 'ring-pink-500 ring-inset'],
            'brand'  => ['bg' => 'bg-brand-50',  'text' => 'text-brand-600','pill-text' => 'text-white',  'dot' => 'bg-brand-600/70 hover:bg-brand-600',  'ring' => 'ring-brand-500/70  ring-inset'],
            'brand-light'  => ['bg' => 'bg-brand-50',  'text' => 'text-brand-600','pill-text' => 'text-brand-600',  'dot' => 'bg-brand-300',  'ring' => 'ring-brand-300 ring-inset'],
            // Event palette shortcuts
            'event-bounced' => ['bg' => 'bg-fuchsia-100', 'text' => 'text-fuchsia-600', 'dot' => 'bg-fuchsia-500', 'ring' => 'ring-fuchsia-500'],
            'event-sent'    => ['bg' => 'bg-yellow-100',  'text' => 'text-yellow-600',  'dot' => 'bg-yellow-500',  'ring' => 'ring-yellow-500'],
            'event-opened'  => ['bg' => 'bg-blue-100',    'text' => 'text-blue-600',    'dot' => 'bg-blue-500',    'ring' => 'ring-blue-500'],
            'event-clicked' => ['bg' => 'bg-orange-100',  'text' => 'text-orange-600',  'dot' => 'bg-orange-500',  'ring' => 'ring-orange-500'],
            'event-ordered' => ['bg' => 'bg-green-100',   'text' => 'text-green-600',   'dot' => 'bg-green-500',   'ring' => 'ring-green-500'],
        ];
    }

    /**
     * Map a newsletter item state to a palette token used across BeezUI.
     * Normalized states include: draft, scheduled, pending, active, completed, ended, past, paused, archived
     * Returns a token present in self::map() (e.g., green, orange, blue, muted, gray).
     */
    public static function newsletterStateToken(string $state): string
    {
        $state = strtolower(trim($state));
        $map = [
            // Semantic color system for NewsletterItem badges
            // Draft ? Orange
            'draft'     => 'orange',
            // Scheduled/Pending ? Blue
            'scheduled' => 'blue',
            'pending'   => 'blue',
            // Active ? Green
            'active'    => 'green',
            // Paused ? Yellow
            'paused'    => 'yellow',
            // Completed/Ended/Past ? Grey
            'completed' => 'gray',
            'ended'     => 'gray',
            'past'      => 'gray',
            // Archived ? neutral (use gray token; outline handled in badge class)
            'archived'  => 'gray',
            // Inactive ? gray
            'inactive'  => 'gray',
            // Expired ? red
            'expired'   => 'red',
        ];
        return $map[$state] ?? 'muted';
    }

    /**
     * Map a newsletter item state to a solid background utility used by status badges
     * in lists (kept as utilities for compact pills in Resource/ItemList).
     */
    public static function newsletterStateBg(string $state): string
    {
        $state = strtolower(trim($state));
        $bgMap = [
            // Active ? Green
            'active'    => 'bg-green-600',
            // Scheduled/Pending ? Blue
            'scheduled' => 'bg-blue-600',
            'pending'   => 'bg-blue-600',
            // Draft ? Orange
            'draft'     => 'bg-orange-500',
            // Paused ? Yellow
            'paused'    => 'bg-yellow-500',
            // Completed/Ended/Past ? Grey
            'completed' => 'bg-gray-500',
            'ended'     => 'bg-gray-500',
            'past'      => 'bg-gray-500',
            // Archived ? neutral outline on white (handled as full class set)
            'archived'  => 'bg-white text-gray-600 ring-1 ring-gray-300',
            // Inactive ? gray
            'inactive'  => 'bg-gray-500',
            // Expired ? red
            'expired'   => 'bg-red-500',
        ];
        return $bgMap[$state] ?? 'bg-slate-600';
    }

    /**
     * Return the compact pill/badge classes for a newsletter state as used in lists.
     * Flat soft pill with no ring and strong readable text tone (700).
     * Example: "active" => "bg-green-100 text-green-700 ring-0 font-medium".
     *
     * Centralizing this here keeps Badge/ItemList/Alpine updates in sync.
     */
    public static function newsletterStateBadgeClass(string $state): string
    {
        // Enforce ring-0, px-2 py-1 is handled by Ui\Badge or caller-provided inner_class.
        // Define explicit text color classes (no dynamic composition) for Tailwind parser.
        $st = strtolower(trim($state));

        // Special-case archived ? neutral, no ring
        if ($st === 'archived') {
            return 'bg-white text-gray-700 ring-0 font-medium';
        }

        $token = self::newsletterStateToken($st);

        // Explicit background and text-700 mappings for tokens we use in newsletter states
        $bgMap = [
            'green'  => 'bg-green-100',
            'blue'   => 'bg-blue-100',
            'orange' => 'bg-orange-100',
            'yellow' => 'bg-yellow-100',
            'gray'   => 'bg-gray-100',
            'muted'  => 'bg-gray-100',
            'brand'  => 'bg-brand-100',
            'indigo' => 'bg-indigo-100',
            'purple' => 'bg-purple-100',
            'red'    => 'bg-red-100',
            'pink'   => 'bg-pink-100',
        ];
        $textMap = [
            'green'  => 'text-green-700',
            'blue'   => 'text-blue-700',
            'orange' => 'text-orange-700',
            'yellow' => 'text-yellow-700',
            'gray'   => 'text-gray-700',
            'muted'  => 'text-gray-700',
            'brand'  => 'text-brand-700',
            'indigo' => 'text-indigo-700',
            'purple' => 'text-purple-700',
            'red'    => 'text-red-700',
            'pink'   => 'text-pink-700',
        ];

        $bg   = $bgMap[$token]   ?? $bgMap['gray'];
        $text = $textMap[$token] ?? $textMap['gray'];

        return trim($bg . ' ' . $text . ' ring-0 font-medium');
    }

    /**
     * Resolve classes for a token or pass-through utility string.
     * If $value contains utility classes (bg-*, text-*, etc.), it is returned as-is.
     * Otherwise, it is treated as a palette token and returns "bg-* text-*" for that token.
     */
    public static function classes(string $value, string $fallbackToken = 'gray'): string
    {
        $value = trim($value);
        $isUtility = $value !== '' && (preg_match('/\b(bg|text|from|to|via|fill|stroke|ring|border)-/i', $value) || strpos($value, ' ') !== false);
        if ($isUtility) {
            return $value;
        }
        $pal = self::map();
        $token = $value !== '' ? $value : $fallbackToken;
        if (isset($pal[$token])) {
            return $pal[$token]['bg'] . ' ' . $pal[$token]['text'];
        }
        // Fallback to gray
        $g = $pal[$fallbackToken] ?? ['bg' => 'bg-gray-100', 'text' => 'text-gray-600'];
        return $g['bg'] . ' ' . $g['text'];
    }

    /**
     * Get a background class for a small dot indicator corresponding to a token.
     * Falls back to the token bg if no dedicated dot color.
     */
    public static function dotBg(string $token, string $fallbackToken = 'gray'): string
    {
        $pal = self::map();
        if (isset($pal[$token])) {
            return $pal[$token]['dot'] ?: $pal[$token]['bg'];
        }
        return ($pal[$fallbackToken]['dot'] ?? '') ?: ($pal[$fallbackToken]['bg'] ?? 'bg-gray-500');
    }

    /**
     * Curated set of color tokens suitable for user-facing pickers (e.g., ColorChoice component).
     *
     * When `$withOutline` is true, returns a map where each `bg-*` token has matching
     * outline/focus utilities (e.g. `outline-red-500 focus-visible:outline-red-500`).
     *
     * @return array<int, string>|array<string, string>
     */
    public static function colorTokensForChoice(bool $withOutline = false): array
    {
        $map = [
            'bg-zinc-500'    => 'outline-zinc-500 focus-visible:outline-zinc-500',
            'bg-red-500'     => 'outline-red-500 focus-visible:outline-red-500',
            'bg-orange-500'  => 'outline-orange-500 focus-visible:outline-orange-500',
            'bg-amber-500'   => 'outline-amber-500 focus-visible:outline-amber-500',
            'bg-yellow-500'  => 'outline-yellow-500 focus-visible:outline-yellow-500',
            'bg-lime-500'    => 'outline-lime-500 focus-visible:outline-lime-500',
            'bg-green-500'   => 'outline-green-500 focus-visible:outline-green-500',
            'bg-emerald-500' => 'outline-emerald-500 focus-visible:outline-emerald-500',
            'bg-teal-500'    => 'outline-teal-500 focus-visible:outline-teal-500',
            'bg-cyan-500'    => 'outline-cyan-500 focus-visible:outline-cyan-500',
            'bg-sky-500'     => 'outline-sky-500 focus-visible:outline-sky-500',
            'bg-blue-500'    => 'outline-blue-500 focus-visible:outline-blue-500',
            'bg-indigo-500'  => 'outline-indigo-500 focus-visible:outline-indigo-500',
            'bg-violet-500'  => 'outline-violet-500 focus-visible:outline-violet-500',
            'bg-purple-500'  => 'outline-purple-500 focus-visible:outline-purple-500',
            'bg-fuchsia-500' => 'outline-fuchsia-500 focus-visible:outline-fuchsia-500',
            'bg-pink-500'    => 'outline-pink-500 focus-visible:outline-pink-500',
            'bg-rose-500'    => 'outline-rose-500 focus-visible:outline-rose-500',
        ];

        return $withOutline ? $map : array_keys($map);
    }



}
