import React from 'react';
import { LogoBase64, SunIcon, MoonIcon, RefreshIcon, SignOutIcon } from './Icons';
import { useTheme } from '../context/ThemeContext';
import type { AIProvider } from '../types/providers';

interface HeaderProps {
  onReset: () => void;
  onClearKey: () => void;
  onToggleAdmin: () => void;
  isAdminView: boolean;
  hasKey: boolean;
  provider?: AIProvider;
}

export const Header: React.FC<HeaderProps> = ({ onReset, onClearKey, onToggleAdmin, isAdminView, hasKey, provider }) => {
  const { theme, toggleTheme } = useTheme();

  const providerColors: Record<AIProvider, string> = {
    gemini: 'from-blue-500 to-indigo-600',
    openai: 'from-green-500 to-emerald-600',
    openrouter: 'from-purple-500 to-fuchsia-600',
    ollama: 'from-slate-500 to-gray-600'
  };

  const providerNames: Record<AIProvider, string> = {
    gemini: 'Gemini',
    openai: 'OpenAI',
    openrouter: 'OpenRouter',
    ollama: 'Ollama'
  };

  return (
    <header className="sticky top-0 z-30 transition-all duration-300 backdrop-blur-md bg-white/70 dark:bg-slate-900/70 border-b border-white/20 dark:border-slate-800 shadow-sm">
      <div className="container mx-auto px-4 py-3 flex items-center justify-between">
        <div className="flex items-center gap-3">
          <div className="relative group cursor-pointer">
            <div className="absolute -inset-1 bg-gradient-to-r from-blue-600 to-indigo-600 rounded-full blur opacity-25 group-hover:opacity-50 transition duration-1000 group-hover:duration-200"></div>
            <img src={LogoBase64} alt="SteelCheck Logo" className="relative w-10 h-10 object-contain" />
          </div>

          <div className="hidden sm:flex items-center gap-2">
            <h1 className="text-2xl font-display font-bold bg-clip-text text-transparent bg-gradient-to-r from-slate-900 to-slate-700 dark:from-white dark:to-slate-300">
              SteelCheck
            </h1>
            <span className="text-[10px] bg-blue-100 text-blue-800 dark:bg-blue-900/50 dark:text-blue-200 px-2 py-0.5 rounded-full font-medium ml-1 border border-blue-200 dark:border-blue-800/50">v1.2 (Compact)</span>
          </div>
        </div>

        <div className="flex items-center gap-2 sm:gap-4">
          {hasKey && provider && (
            <div className={`hidden sm:flex items-center gap-2 px-3 py-1.5 rounded-full bg-gradient-to-r ${providerColors[provider]} text-white text-xs font-medium`}>
              {providerNames[provider]}
            </div>
          )}

          {hasKey && (
            <button
              onClick={onReset}
              className="flex items-center gap-1.5 px-3 py-1.5 rounded-full text-xs font-medium text-slate-600 hover:text-blue-600 hover:bg-blue-50 dark:text-slate-300 dark:hover:bg-slate-800 transition-all border border-transparent hover:border-blue-200 dark:hover:border-blue-900"
              title="Iniciar nova análise de certificado"
            >
              <RefreshIcon className="w-4 h-4" />
              <span className="hidden sm:inline">Nova Análise</span>
            </button>
          )}

          {hasKey && (
            <button
              onClick={onToggleAdmin}
              className={`flex items-center gap-1.5 px-3 py-1.5 rounded-full text-xs font-medium transition-all border ${isAdminView ? 'text-indigo-600 bg-indigo-50 border-indigo-200 dark:text-indigo-300 dark:bg-indigo-900/30 dark:border-indigo-800' : 'text-slate-600 hover:text-indigo-600 hover:bg-indigo-50 dark:text-slate-300 dark:hover:bg-slate-800 border-transparent hover:border-indigo-200 dark:hover:border-indigo-900'}`}
              title="Gerenciar Categorias e Normas"
            >
              <svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" strokeWidth="2" stroke="currentColor">
                <path strokeLinecap="round" strokeLinejoin="round" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
                <path strokeLinecap="round" strokeLinejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
              </svg>
              <span className="hidden sm:inline">Admin</span>
            </button>
          )}

          {hasKey && (
            <button
              onClick={onClearKey}
              className="flex items-center gap-1.5 px-3 py-1.5 rounded-full text-xs font-medium text-slate-500 hover:text-red-500 hover:bg-red-50 dark:text-slate-400 dark:hover:bg-red-900/20 transition-all border border-transparent hover:border-red-200 dark:hover:border-red-900"
              title="Alterar Chave de API"
            >
              <SignOutIcon className="w-4 h-4" />
              <span className="hidden sm:inline">Sair</span>
            </button>
          )}

          <div className="flex items-center bg-slate-100 dark:bg-slate-800/50 p-1 rounded-full border border-slate-200 dark:border-slate-700">
            <button
              onClick={toggleTheme}
              className="p-1.5 rounded-full text-slate-500 dark:text-slate-400 hover:bg-white dark:hover:bg-slate-700 hover:shadow-sm focus:outline-none transition-all relative overflow-hidden"
              aria-label="Alternar tema escuro/claro"
            >
              <div className="relative z-10">
                {theme === 'light' ? (
                  <MoonIcon className="w-4 h-4" />
                ) : (
                  <SunIcon className="w-4 h-4" />
                )}
              </div>
            </button>
          </div>
        </div>
      </div>
    </header>
  );
};
