import { Card, CardContent } from '@/components/ui/Card';
import { TaskStats } from '@/lib/types';
import { useTheme } from '@/contexts/ThemeContext';
import { usePathname } from 'next/navigation';
import Link from 'next/link';

interface HeaderProps {
  title?: string;
  subtitle?: string;
  stats?: TaskStats;
  syncing?: boolean;
}

export function Header({ title = "TowerControl", subtitle = "Task Management", stats, syncing = false }: HeaderProps) {
  const { theme, toggleTheme } = useTheme();
  const pathname = usePathname();

  // Fonction pour déterminer si un lien est actif
  const isActiveLink = (href: string) => {
    if (href === '/') {
      return pathname === '/';
    }
    return pathname.startsWith(href);
  };

  // Fonction pour obtenir les classes CSS d'un lien
  const getLinkClasses = (href: string) => {
    const baseClasses = "font-mono text-sm uppercase tracking-wider transition-colors px-3 py-1.5 rounded-md";
    
    if (isActiveLink(href)) {
      return `${baseClasses} text-[var(--primary)] bg-[var(--primary)]/10 border border-[var(--primary)]/30`;
    }
    
    return `${baseClasses} text-[var(--muted-foreground)] hover:text-[var(--primary)] hover:bg-[var(--card-hover)]`;
  };

  return (
    <header className="bg-[var(--card)]/80 backdrop-blur-sm border-b border-[var(--border)]/50 shadow-lg shadow-[var(--card)]/20">
      <div className="container mx-auto px-6 py-4">
        <div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-6">
          {/* Titre tech avec glow */}
          <div className="flex items-center gap-6">
            <div className="flex items-center gap-4 w-[300px]">
              <div className={`w-3 h-3 rounded-full shadow-lg ${
                syncing 
                  ? 'bg-yellow-400 animate-spin shadow-yellow-400/50' 
                  : 'bg-cyan-400 animate-pulse shadow-cyan-400/50'
              }`}></div>
              <div>
                <h1 className="text-2xl font-mono font-bold text-[var(--foreground)] tracking-wider">
                  {title}
                </h1>
                <p className="text-[var(--muted-foreground)] mt-1 font-mono text-sm">
                  {subtitle} {syncing && '• Synchronisation...'}
                </p>
              </div>
            </div>

            {/* Navigation */}
            <nav className="hidden sm:flex items-center gap-2">
              <Link
                href="/"
                className={getLinkClasses('/')}
              >
                Dashboard
              </Link>
              <Link
                href="/kanban"
                className={getLinkClasses('/kanban')}
              >
                Kanban
              </Link>
              <Link
                href="/daily"
                className={getLinkClasses('/daily')}
              >
                Daily
              </Link>
              <Link
                href="/tags"
                className={getLinkClasses('/tags')}
              >
                Tags
              </Link>
              <Link
                href="/settings"
                className={getLinkClasses('/settings')}
              >
                Settings
              </Link>
              
              {/* Theme Toggle */}
              <button
                onClick={toggleTheme}
                className="text-[var(--muted-foreground)] hover:text-[var(--primary)] transition-colors p-1 rounded-md hover:bg-[var(--card-hover)]"
                title={`Switch to ${theme === 'dark' ? 'light' : 'dark'} theme`}
              >
                {theme === 'dark' ? (
                  <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z" />
                  </svg>
                ) : (
                  <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z" />
                  </svg>
                )}
              </button>
            </nav>
          </div>

          {/* Stats essentielles - seulement si stats disponibles */}
          {stats && (
            <div className="flex flex-wrap gap-3">
              <StatCard 
                label="TOTAL" 
                value={String(stats.total).padStart(2, '0')} 
                color="blue"
              />
              <StatCard 
                label="DONE" 
                value={String(stats.completed).padStart(2, '0')} 
                color="green"
              />
              <StatCard 
                label="ACTIVE" 
                value={String(stats.inProgress).padStart(2, '0')} 
                color="yellow"
              />
              <StatCard 
                label="RATE" 
                value={`${stats.completionRate}%`} 
                color="purple"
              />
            </div>
          )}
        </div>
      </div>
    </header>
  );
}

interface StatCardProps {
  label: string;
  value: number | string;
  color: 'blue' | 'green' | 'yellow' | 'gray' | 'purple';
}

function StatCard({ label, value, color }: StatCardProps) {

  const textColors = {
    blue: 'text-[var(--primary)]',
    green: 'text-[var(--success)]',
    yellow: 'text-[var(--accent)]', 
    gray: 'text-[var(--muted-foreground)]',
    purple: 'text-[var(--accent)]'
  };

  return (
    <Card variant="elevated" className="px-3 py-2 hover:border-opacity-75 transition-all duration-300">
      <CardContent className="p-0">
        <div className={`text-xs font-mono font-bold ${textColors[color]} opacity-75 uppercase tracking-wider`}>
          {label}
        </div>
        <div className={`text-lg font-mono font-bold ${textColors[color]}`}>
          {value}
        </div>
      </CardContent>
    </Card>
  );
}
