import { useState, useCallback, useRef } from 'react';
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Badge } from '@/components/ui/badge';
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from '@/components/ui/select';
import { Play, FileDown, Loader2, AlertCircle } from 'lucide-react';
import type { AuditConfig, AuditScenario, ScenarioResult, AuditReport } from '@/lib/audit/types';
import { MODULE_LABELS } from '@/lib/audit/types';
import { generateAllScenarios } from '@/lib/audit/scenarios';
import AuditDetailPopup from './AuditDetailPopup';

const PROVIDERS: { value: string; label: string }[] = [
  { value: 'openai', label: 'OpenAI' },
  { value: 'anthropic', label: 'Anthropic' },
  { value: 'minimax', label: 'MiniMax' },
  { value: 'openrouter', label: 'OpenRouter' },
  { value: 'ollama', label: 'Ollama' },
];

const DEFAULT_MODELS: Record<string, string> = {
  openai: 'gpt-4o',
  anthropic: 'claude-3-5-sonnet-latest',
  minimax: 'MiniMax-M3',
  openrouter: 'openai/gpt-4o',
  ollama: 'deepseek-r1',
};

export default function AuditPanel() {

  const [config, setConfig] = useState<AuditConfig & { testScope?: string }>({
    provider: 'openai',
    model: 'gpt-4o',
    apiKey: '',
    baseUrl: '',
    testScope: 'all',
  });

  const [status, setStatus] = useState<'idle' | 'generating' | 'running' | 'done' | 'error'>('idle');
  const [progress, setProgress] = useState(0);
  const [total, setTotal] = useState(0);
  const [report, setReport] = useState<AuditReport | null>(null);
  const [error, setError] = useState<string | null>(null);
  const abortRef = useRef(false);

  const [popupOpen, setPopupOpen] = useState(false);
  const [selectedScenario, setSelectedScenario] = useState<AuditScenario | null>(null);
  const [selectedResult, setSelectedResult] = useState<ScenarioResult | null>(null);

  const allScenarios = useRef(generateAllScenarios()).current;

  const [checkedScenarioIds, setCheckedScenarioIds] = useState<string[]>([]);
  const [runOnlyBibliographic, setRunOnlyBibliographic] = useState(false);

  const handleProviderChange = (val: string) => {
    setConfig((c) => ({
      ...c,
      provider: val as AuditConfig['provider'],
      model: DEFAULT_MODELS[val] ?? c.model,
    }));
  };

  const handleScopeChange = (val: string) => {
    setConfig((c) => ({ ...c, testScope: val }));
    if (val !== 'all') {
      const ids = allScenarios.filter(s => s.module === val).map(s => s.id);
      setCheckedScenarioIds(ids);
    } else {
      setCheckedScenarioIds([]);
    }
  };

  const handleRun = useCallback(async () => {
    if (!config.apiKey && config.provider !== 'ollama') {
      setError('Informe uma API key para continuar.');
      setStatus('error');
      return;
    }

    const controller = new AbortController();
    abortRef.current = controller as any; // Usando any pois não precisamos refatorar todos os tipos agora, apenas para passar o signal

    setError(null);
    setReport(null);
    setProgress(0);

    setStatus('generating');
    let scenarios = generateAllScenarios();
    if (runOnlyBibliographic) {
      scenarios = scenarios.filter(s => s.isBibliographic);
    }
    if (config.testScope && config.testScope !== 'all') {
      scenarios = scenarios.filter(s => s.module === config.testScope && checkedScenarioIds.includes(s.id));
    }
    setTotal(scenarios.length);

    setStatus('running');

    try {
      const { runAudit } = await import('@/lib/audit/runner');
      const reportResult = await runAudit(
        config,
        scenarios,
        (p) => {
          if (typeof p === 'number') setProgress(p);
        },
        controller.signal
      );
      setReport(reportResult);
      setStatus('done');
      
      // Auto-download MD
      import('@/lib/audit/export-audit-md').then(m => m.downloadAuditMarkdown(reportResult));

    } catch (e) {
      if (e instanceof Error && e.message.includes('Abortado')) {
        setStatus('idle');
        return;
      }
      setError(e instanceof Error ? e.message : 'Erro desconhecido');
      setStatus('error');
    }
  }, [config, checkedScenarioIds]);

  const handleExportPDF = () => {
    if (!report) return;
    import('@/lib/audit/export-audit-pdf').then((mod) => mod.exportAuditReportToPDF(report));
  };

  const grouped = report && report.modules
    ? report.modules.map((m: { module: string; moduleLabel: string; totalScenarios: number; passed: number; failed: number; warnings: number }) => ({
        module: m.module,
        label: m.moduleLabel,
        total: m.totalScenarios,
        passed: m.passed,
        failed: m.failed,
        warnings: m.warnings,
      }))
    : [];

  return (
    <div className="space-y-4">
      <Card>
        <CardHeader>
          <CardTitle className="text-lg">Auditoria LLM</CardTitle>
          <CardDescription>
            Validação dos cálculos de vento usando LLMs sequenciais. 100+ cenários cobrindo todos os módulos.
          </CardDescription>
        </CardHeader>
        <CardContent className="space-y-4">
          <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
            <div className="space-y-1.5">
              <label className="text-sm font-medium">Provedor</label>
              <Select value={config.provider} onValueChange={handleProviderChange}>
                <SelectTrigger>
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  {PROVIDERS.map((p) => (
                    <SelectItem key={p.value} value={p.value}>{p.label}</SelectItem>
                  ))}
                </SelectContent>
              </Select>
            </div>
            <div className="space-y-1.5">
              <label className="text-sm font-medium">Modelo</label>
              <Input
                value={config.model}
                onChange={(e) => setConfig((c) => ({ ...c, model: e.target.value }))}
                placeholder="gpt-4o, claude-3-5-sonnet-..."
              />
            </div>
            <div className="space-y-1.5">
              <label className="text-sm font-medium">API Key</label>
              <Input
                type="password"
                value={config.apiKey}
                onChange={(e) => setConfig((c) => ({ ...c, apiKey: e.target.value }))}
                placeholder="sk-..."
              />
            </div>
            <div className="space-y-1.5">
              <label className="text-sm font-medium">Base URL (opcional)</label>
              <Input
                value={config.baseUrl ?? ''}
                onChange={(e) => setConfig((c) => ({ ...c, baseUrl: e.target.value || undefined }))}
                placeholder={config.provider === 'openrouter' ? 'https://openrouter.ai/api/v1 (padrão)' : config.provider === 'minimax' ? 'https://api.minimax.io/v1 (padrão)' : config.provider === 'ollama' ? 'http://localhost:11434 (padrão)' : 'https://api.openai.com/v1'}
              />
            </div>
            <div className="space-y-1.5 sm:col-span-2">
              <label className="text-sm font-medium">Escopo do Teste</label>
              <Select value={config.testScope} onValueChange={handleScopeChange}>
                <SelectTrigger>
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  <SelectItem value="all">Todos os Cenários (100+ testes)</SelectItem>
                  {Object.entries(MODULE_LABELS).map(([key, labels]) => (
                    <SelectItem key={key} value={key}>
                      Apenas módulo: {labels['pt-BR']}
                    </SelectItem>
                  ))}
                </SelectContent>
              </Select>
            </div>
          </div>

          <div className="flex items-center space-x-2 mt-4 mb-2">
            <input 
              type="checkbox" 
              id="runOnlyBib" 
              checked={runOnlyBibliographic} 
              onChange={(e) => setRunOnlyBibliographic(e.target.checked)} 
              className="rounded border-gray-300 text-primary focus:ring-primary h-4 w-4"
            />
            <label htmlFor="runOnlyBib" className="text-sm font-medium leading-none cursor-pointer">
              Executar apenas cenários bibliográficos (TesteBib)
            </label>
          </div>

          {config.testScope && config.testScope !== 'all' && (
            <div className="border rounded-md p-3 mt-2 bg-muted/20">
              <div className="flex items-center justify-between mb-2">
                <label className="text-sm font-medium">Cenários Específicos ({checkedScenarioIds.length} selecionados)</label>
                <Button 
                  variant="ghost" 
                  size="sm" 
                  className="h-6 text-xs" 
                  onClick={() => {
                    const allIds = allScenarios.filter(s => s.module === config.testScope).map(s => s.id);
                    if (checkedScenarioIds.length === allIds.length) {
                      setCheckedScenarioIds([]);
                    } else {
                      setCheckedScenarioIds(allIds);
                    }
                  }}
                >
                  {checkedScenarioIds.length === allScenarios.filter(s => s.module === config.testScope).length ? 'Desmarcar Todos' : 'Marcar Todos'}
                </Button>
              </div>
              <div className="max-h-48 overflow-y-auto space-y-1 pr-2">
                {allScenarios
                  .filter(s => s.module === config.testScope)
                  .map(s => (
                    <label key={s.id} className="flex items-start gap-2 text-sm p-1.5 hover:bg-muted/50 rounded cursor-pointer">
                      <input 
                        type="checkbox" 
                        className="mt-0.5 rounded border-gray-300"
                        checked={checkedScenarioIds.includes(s.id)}
                        onChange={(e) => {
                          if (e.target.checked) {
                            setCheckedScenarioIds(prev => [...prev, s.id]);
                          } else {
                            setCheckedScenarioIds(prev => prev.filter(id => id !== s.id));
                          }
                        }}
                      />
                      <div className="flex flex-col">
                        <span className="font-medium">{s.id}</span>
                        <span className="text-xs text-muted-foreground">{s.description}</span>
                      </div>
                    </label>
                  ))}
              </div>
            </div>
          )}

          <div className="flex items-center gap-2 flex-wrap">
            <Button
              onClick={handleRun}
              disabled={status === 'running' || status === 'generating'}
            >
              {status === 'running' || status === 'generating' ? (
                <Loader2 className="w-4 h-4 mr-1.5 animate-spin" />
              ) : (
                <Play className="w-4 h-4 mr-1.5" />
              )}
              Executar Auditoria
            </Button>
            {(status === 'running' || status === 'generating') && (
              <Button variant="outline" size="sm" onClick={() => { if (abortRef.current && typeof (abortRef.current as any).abort === 'function') (abortRef.current as any).abort(); }}>
                Cancelar
              </Button>
            )}
            {status === 'done' && report && (
              <>
                <Button variant="outline" size="sm" onClick={() => import('@/lib/audit/export-audit-md').then(m => m.downloadAuditMarkdown(report))}>
                  <FileDown className="w-3.5 h-3.5 mr-1.5" />
                  Exportar MD
                </Button>
                <Button variant="outline" size="sm" onClick={handleExportPDF}>
                  <FileDown className="w-3.5 h-3.5 mr-1.5" />
                  Exportar PDF
                </Button>
              </>
            )}
          </div>

          {(status === 'running' || status === 'generating') && total > 0 && (
            <div className="space-y-2">
              <div className="flex justify-between text-xs text-muted-foreground">
                <span>{status === 'generating' ? 'Gerando cenários...' : `Processando cenários...`}</span>
                <span>{progress} / {total}</span>
              </div>
              <div className="w-full bg-muted rounded-full h-2 overflow-hidden">
                <div
                  className="bg-purple-600 h-full rounded-full transition-all duration-300"
                  style={{ width: `${(progress / total) * 100}%` }}
                />
              </div>
              <div className="text-xs text-muted-foreground animate-pulse text-center">
                Aguardando resposta do modelo para o lote atual... (pode demorar alguns segundos)
              </div>
            </div>
          )}

          {status === 'error' && error && (
            <div className="rounded-md border border-red-200 bg-red-50 dark:bg-red-950 dark:border-red-800 p-3 text-sm text-red-700 dark:text-red-300 flex items-start gap-2">
              <AlertCircle className="w-4 h-4 mt-0.5 shrink-0" />
              <span>{error}</span>
            </div>
          )}
        </CardContent>
      </Card>

      {status === 'done' && report && (
        <Card>
          <CardHeader>
            <CardTitle className="text-lg flex items-center justify-between">
              <span>Resultados</span>
              <span className="text-sm font-normal text-muted-foreground">
                {report.totalPassed}/{report.totalScenarios} PASS —
                W:{report.totalWarnings} F:{report.totalFailed}
              </span>
            </CardTitle>
            <CardDescription>
              Provedor: {report.provider} ({report.model}) — {new Date(report.timestamp).toLocaleString('pt-BR')}
            </CardDescription>
          </CardHeader>
          <CardContent>
            <div className="rounded-md border overflow-hidden">
              <table className="w-full text-sm">
                <thead className="bg-muted/50">
                  <tr>
                    <th className="p-2 text-left">Módulo</th>
                    <th className="p-2 text-center">Total</th>
                    <th className="p-2 text-center">PASS</th>
                    <th className="p-2 text-center">WARN</th>
                    <th className="p-2 text-center">FAIL</th>
                  </tr>
                </thead>
                <tbody>
                  {grouped.map((g: { module: string; label: string; total: number; passed: number; failed: number; warnings: number }) => (
                    <tr key={g.module} className="border-t border-border cursor-pointer hover:bg-muted/30"
                      onClick={() => {
                        const mod = report.modules.find((m) => m.module === g.module);
                        if (!mod) return;
                        const modScenarios = generateAllScenarios().filter((s) => s.module === g.module);
                        const dialog = mod.scenarios[0];
                        if (modScenarios.length > 0 && dialog) {
                          const matchingScenario = modScenarios.find(s => s.id === dialog.scenarioId) || modScenarios[0];
                          setSelectedScenario(matchingScenario);
                          setSelectedResult(dialog);
                          setPopupOpen(true);
                        }
                      }}>
                      <td className="p-2 font-medium">{g.label}</td>
                      <td className="p-2 text-center">{g.total}</td>
                      <td className="p-2 text-center"><Badge className="bg-emerald-100 text-emerald-800 dark:bg-emerald-900 dark:text-emerald-300">{g.passed}</Badge></td>
                      <td className="p-2 text-center"><Badge className="bg-amber-100 text-amber-800 dark:bg-amber-900 dark:text-amber-300">{g.warnings}</Badge></td>
                      <td className="p-2 text-center"><Badge className="bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-300">{g.failed}</Badge></td>
                    </tr>
                  ))}
                </tbody>
              </table>
            </div>
            <p className="text-xs text-muted-foreground mt-2">
              Clique em um módulo para ver detalhes dos cenários e verificações.
            </p>
          </CardContent>
        </Card>
      )}

      <AuditDetailPopup
        open={popupOpen}
        onOpenChange={setPopupOpen}
        scenario={selectedScenario}
        result={selectedResult}
      />
    </div>
  );
}
