'use client';

import React from 'react';
import { useTheme } from '@/contexts/theme-context';
import { Sun, Moon, Monitor } from 'lucide-react';
import { Button } from './button';
import {
    DropdownMenu,
    DropdownMenuTrigger,
    DropdownMenuContent,
    DropdownMenuItem
} from './dropdown-menu';
import { cn } from '@/lib/utils';

/**
 * EN: Enhanced Theme Toggle - Professional theme switcher with brand styling
 * VI: Theme Toggle nâng cao - Bộ chuyển theme chuyên nghiệp với brand styling
 * 
 * Features:
 * - Dropdown menu with 3 options: Light, Dark, System
 * - Glassmorphism styling
 * - Icons from lucide-react
 * - Smooth transitions
 * - Keyboard accessible
 * 
 * @example
 * ```tsx
 * <ThemeToggle />
 * ```
 */
export function ThemeToggle() {
    const { theme, setTheme, resolvedTheme } = useTheme();

    const themeOptions = [
        { value: 'light' as const, label: 'Light', icon: Sun },
        { value: 'dark' as const, label: 'Dark', icon: Moon },
        { value: 'system' as const, label: 'System', icon: Monitor },
    ];

    const currentIcon = resolvedTheme === 'dark' ? Moon : Sun;
    const CurrentIcon = currentIcon;

    return (
        <DropdownMenu>
            <DropdownMenuTrigger asChild>
                <Button
                    variant="glass"
                    size="md"
                    className="w-10 h-10 p-0"
                    aria-label="Toggle theme"
                >
                    <CurrentIcon className="h-5 w-5" />
                </Button>
            </DropdownMenuTrigger>

            <DropdownMenuContent
                align="end"
                className="bg-glass-bg backdrop-blur-glass border border-glass-border min-w-[150px]"
            >
                {themeOptions.map((option) => {
                    const Icon = option.icon;
                    const isActive = theme === option.value;

                    return (
                        <DropdownMenuItem
                            key={option.value}
                            onClick={() => setTheme(option.value)}
                            className={cn(
                                'flex items-center gap-2 px-3 py-2 cursor-pointer transition-colors',
                                'hover:bg-bg-tertiary focus:bg-bg-tertiary',
                                isActive && 'bg-brand-primary/10 text-brand-primary'
                            )}
                        >
                            <Icon className="h-4 w-4" />
                            <span>{option.label}</span>
                            {isActive && (
                                <span className="ml-auto text-brand-primary">✓</span>
                            )}
                        </DropdownMenuItem>
                    );
                })}
            </DropdownMenuContent>
        </DropdownMenu>
    );
}

/**
 * EN: Simple Theme Toggle Button - Toggles between light and dark only
 * VI: Button Toggle Theme đơn giản - Chỉ chuyển đổi giữa light và dark
 * 
 * @example
 * ```tsx
 * <ThemeToggleButton />
 * ```
 */
export function ThemeToggleButton() {
    const { toggleTheme, resolvedTheme } = useTheme();
    const Icon = resolvedTheme === 'dark' ? Sun : Moon;

    return (
        <Button
            variant="glass"
            size="md"
            className="w-10 h-10 p-0"
            onClick={toggleTheme}
            aria-label={`Switch to ${resolvedTheme === 'dark' ? 'light' : 'dark'} mode`}
        >
            <Icon className="h-5 w-5 transition-transform duration-normal hover:rotate-12" />
        </Button>
    );
}
