import { BQASMetrics } from '../types'
import { IntentScoresChart } from './IntentScoresChart'
import { FailedTestsList } from './FailedTestsList'

export function RagTab({
  ragMetrics,
  isRunningRag,
  runRagTests,
}: {
  ragMetrics: BQASMetrics | null
  isRunningRag: boolean
  runRagTests: () => void
}) {
  return (
    <div className="space-y-6">
      <div className="bg-white rounded-xl border border-slate-200 p-6">
        <div className="flex items-center justify-between mb-6">
          <div>
            <h3 className="text-lg font-semibold text-slate-900">RAG/Korrektur Test Suite</h3>
            <p className="text-sm text-slate-500">Erwartungshorizont-Retrieval, Operatoren-Alignment, Citations</p>
          </div>
          <button
            onClick={runRagTests}
            disabled={isRunningRag}
            className="px-4 py-2 bg-primary-600 text-white rounded-lg hover:bg-primary-700 disabled:bg-slate-300"
          >
            {isRunningRag ? 'Laeuft...' : 'Tests starten'}
          </button>
        </div>

        {ragMetrics ? (
          <>
            {/* RAG Metrics */}
            <div className="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
              <div className="text-center p-4 bg-slate-50 rounded-lg">
                <p className="text-2xl font-bold text-slate-900">{ragMetrics.total_tests}</p>
                <p className="text-xs text-slate-500">Tests</p>
              </div>
              <div className="text-center p-4 bg-purple-50 rounded-lg">
                <p className="text-2xl font-bold text-purple-600">{ragMetrics.avg_faithfulness.toFixed(2)}</p>
                <p className="text-xs text-slate-500">Faithfulness</p>
              </div>
              <div className="text-center p-4 bg-blue-50 rounded-lg">
                <p className="text-2xl font-bold text-blue-600">{ragMetrics.avg_relevance.toFixed(2)}</p>
                <p className="text-xs text-slate-500">Relevance</p>
              </div>
              <div className="text-center p-4 bg-emerald-50 rounded-lg">
                <p className="text-2xl font-bold text-emerald-600">{(ragMetrics.safety_pass_rate * 100).toFixed(0)}%</p>
                <p className="text-xs text-slate-500">Safety Pass</p>
              </div>
            </div>

            {/* RAG Categories */}
            <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
              <div>
                <h4 className="font-medium text-slate-900 mb-4">RAG Kategorien</h4>
                <IntentScoresChart scores={ragMetrics.scores_by_intent} />
              </div>
              <div>
                <h4 className="font-medium text-slate-900 mb-4">Fehlgeschlagene Tests</h4>
                <FailedTestsList testIds={ragMetrics.failed_test_ids} />
              </div>
            </div>
          </>
        ) : (
          <div className="text-center py-12 text-slate-400">
            <svg className="w-16 h-16 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
            </svg>
            <p>Noch keine RAG-Test-Ergebnisse</p>
            <p className="text-sm mt-2">Klicke &quot;Tests starten&quot; um die RAG-Suite auszufuehren</p>
          </div>
        )}
      </div>

      {/* RAG Test Categories Explanation */}
      <div className="bg-white rounded-xl border border-slate-200 p-6">
        <h3 className="text-lg font-semibold text-slate-900 mb-4">Test-Kategorien</h3>
        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
          {[
            { name: 'EH Retrieval', desc: 'Korrektes Abrufen von Erwartungshorizont-Passagen', color: 'blue' },
            { name: 'Operator Alignment', desc: 'Passende Operatoren fuer Abitur-Aufgaben', color: 'purple' },
            { name: 'Hallucination Control', desc: 'Keine erfundenen Fakten oder Inhalte', color: 'red' },
            { name: 'Citation Enforcement', desc: 'Quellenangaben bei EH-Bezuegen', color: 'green' },
            { name: 'Privacy Compliance', desc: 'Keine PII-Leaks, DSGVO-Konformitaet', color: 'amber' },
            { name: 'Namespace Isolation', desc: 'Strikte Trennung zwischen Lehrern', color: 'slate' },
          ].map((cat) => (
            <div key={cat.name} className={`p-4 rounded-lg border bg-${cat.color}-50 border-${cat.color}-200`}>
              <h4 className="font-medium text-slate-900">{cat.name}</h4>
              <p className="text-sm text-slate-600 mt-1">{cat.desc}</p>
            </div>
          ))}
        </div>
      </div>
    </div>
  )
}
