<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Handle Theme Switching requests
if (isset($_GET['set_theme'])) {
    $theme = $_GET['set_theme'];
    if (in_array($theme, ['shopee', 'dark', 'blue'])) {
        $_SESSION['theme'] = $theme;
    }
    // Redirect back to referring page or index.php
    $referrer = $_SERVER['HTTP_REFERER'] ?? 'index.php';
    header("Location: " . $referrer);
    exit;
}

/**
 * Get color values according to current active theme
 */
function getThemeVars() {
    $theme = $_SESSION['theme'] ?? 'shopee';
    switch ($theme) {
        case 'dark':
            return [
                'primary' => '#f97316',        // Vibrant orange accents for dark mode
                'primary-hover' => '#ea580c',
                'bg' => '#0f172a',             // Slate-900
                'card-bg' => '#1e293b',        // Slate-800
                'text' => '#f8fafc',           // Slate-50
                'text-secondary' => '#94a3b8', // Slate-400
                'border' => '#334155',         // Slate-700
                'header-bg' => '#1e293b',      // Dark slate header
            ];
        case 'blue':
            return [
                'primary' => '#2563eb',        // Luxury tech blue
                'primary-hover' => '#1d4ed8',
                'bg' => '#f8fafc',             // Slate-50 light
                'card-bg' => '#ffffff',
                'text' => '#1e293b',
                'text-secondary' => '#64748b',
                'border' => '#cbd5e1',
                'header-bg' => '#1e3a8a',      // Royal blue header
            ];
        case 'shopee':
        default:
            return [
                'primary' => '#ee4d2d',        // Shopee traditional orange-red
                'primary-hover' => '#d83f20',
                'bg' => '#f5f5f5',             // Light gray bg
                'card-bg' => '#ffffff',
                'text' => '#1f2937',           // Slate-800
                'text-secondary' => '#6b7280', // Gray-500
                'border' => '#e5e7eb',         // Gray-200
                'header-bg' => '#ee4d2d',
            ];
    }
}

/**
 * Inject the Tailwind configurations and styling variables
 */
function injectThemeStyles() {
    $vars = getThemeVars();
    $theme = $_SESSION['theme'] ?? 'shopee';
    $isDark = ($theme === 'dark') ? 'true' : 'false';
    ?>
    <style>
        :root {
            --primary-color: <?= $vars['primary'] ?>;
            --primary-hover: <?= $vars['primary-hover'] ?>;
            --primary-bg: <?= $vars['bg'] ?>;
            --card-bg: <?= $vars['card-bg'] ?>;
            --text-color: <?= $vars['text'] ?>;
            --text-secondary: <?= $vars['text-secondary'] ?>;
            --border-color: <?= $vars['border'] ?>;
            --header-bg: <?= $vars['header-bg'] ?>;
        }
        
        /* Smooth transitions for theme switches */
        body {
            background-color: var(--primary-bg) !important;
            color: var(--text-color) !important;
            border-color: var(--border-color) !important;
            transition: background-color 0.2s ease, color 0.2s ease, border-color 0.2s ease;
        }
        
        .theme-card {
            background-color: var(--card-bg) !important;
            border-color: var(--border-color) !important;
            color: var(--text-color) !important;
        }
        
        .theme-text-muted {
            color: var(--text-secondary) !important;
        }
        
        .theme-border {
            border-color: var(--border-color) !important;
        }
        
        .theme-bg-header {
            background-color: var(--header-bg) !important;
        }
    </style>
    <script>
        // Extend tailwind css with custom theme properties mapped to our CSS variables
        if (typeof tailwind !== 'undefined') {
            tailwind.config = {
                darkMode: 'class',
                theme: {
                    extend: {
                        colors: {
                            primary: 'var(--primary-color)',
                            'primary-hover': 'var(--primary-hover)',
                            customBg: 'var(--primary-bg)',
                            cardBg: 'var(--card-bg)',
                            customText: 'var(--text-color)',
                            customMuted: 'var(--text-secondary)',
                            borderColor: 'var(--border-color)',
                            headerBg: 'var(--header-bg)',
                        }
                    }
                }
            };
        }
        
        // Sync document element class
        if (<?= $isDark ?>) {
            document.documentElement.classList.add('dark');
        } else {
            document.documentElement.classList.remove('dark');
        }
    </script>
    <?php
}

/**
 * Render a beautiful, premium theme switcher component in the header
 */
function renderThemeSwitcher() {
    $currentTheme = $_SESSION['theme'] ?? 'shopee';
    $themeNames = [
        'shopee' => '🍊 สีส้ม Shopee',
        'dark' => '🌙 โหมดมืด (Dark)',
        'blue' => '💎 สีน้ำเงินหรูหรา'
    ];
    ?>
    <div class="relative inline-block text-left group">
        <button type="button" class="inline-flex items-center gap-2 bg-white/10 hover:bg-white/20 text-white px-3 py-2 rounded-md text-xs font-semibold transition border border-white/20 cursor-pointer">
            <span>ธีม: <?= $themeNames[$currentTheme] ?></span>
            <svg class="w-3.5 h-3.5 text-orange-200" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M19 9l-7 7-7-7"></path>
            </svg>
        </button>
        <!-- Dropdown Menu (reveals on hover) -->
        <div class="absolute right-0 mt-1 w-48 bg-white dark:bg-slate-800 rounded-lg shadow-xl border border-gray-200 dark:border-slate-700 hidden group-hover:block hover:block z-50 overflow-hidden">
            <div class="py-1">
                <?php foreach ($themeNames as $key => $name): ?>
                    <a href="theme.php?set_theme=<?= $key ?>" 
                       class="block px-4 py-2.5 text-xs text-gray-700 dark:text-slate-200 hover:bg-orange-50 dark:hover:bg-slate-700/50 transition font-medium <?= $currentTheme === $key ? 'font-bold bg-orange-50 dark:bg-orange-950/20 text-[#ee4d2d] dark:text-orange-400' : '' ?>">
                        <?= $name ?>
                    </a>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
    <?php
}
