'use client'

import { useState, useEffect } from 'react'
import type {
  IngestionStatus,
  LiveProgress,
  IndexedStats,
  PendingFilesData,
  IngestionHistoryEntry
} from '../types'
import { IngestionHistory } from './IngestionHistory'

const API_BASE = process.env.NEXT_PUBLIC_KLAUSUR_SERVICE_URL || 'http://localhost:8086'

interface IngestionTabProps {
  status: IngestionStatus | null
  onRefresh: () => void
}

function IngestionTab({ status, onRefresh }: IngestionTabProps) {
  const [starting, setStarting] = useState(false)
  const [liveProgress, setLiveProgress] = useState<LiveProgress | null>(null)
  const [indexedStats, setIndexedStats] = useState<IndexedStats | null>(null)
  const [pendingFiles, setPendingFiles] = useState<PendingFilesData | null>(null)
  const [ingestionHistory, setIngestionHistory] = useState<IngestionHistoryEntry[]>([])
  const [showPending, setShowPending] = useState(false)

  useEffect(() => {
    const fetchStats = async () => {
      try { const res = await fetch(`${API_BASE}/api/v1/admin/nibis/stats`); if (res.ok) setIndexedStats(await res.json()) }
      catch (err) { console.error('Failed to fetch stats:', err) }
    }
    const fetchPending = async () => {
      try { const res = await fetch(`${API_BASE}/api/v1/admin/rag/files/pending`); if (res.ok) setPendingFiles(await res.json()) }
      catch (err) { console.error('Failed to fetch pending files:', err) }
    }
    const fetchHistory = async () => {
      try { const res = await fetch(`${API_BASE}/api/v1/admin/rag/ingestion/history?limit=20`); if (res.ok) { const data = await res.json(); setIngestionHistory(data.history || []) } }
      catch (err) { console.error('Failed to fetch history:', err) }
    }
    fetchStats(); fetchPending(); fetchHistory()
  }, [])

  useEffect(() => {
    let interval: NodeJS.Timeout | null = null
    const fetchProgress = async () => {
      try {
        const res = await fetch(`${API_BASE}/api/v1/admin/nibis/progress`)
        if (res.ok) {
          const data = await res.json()
          setLiveProgress(data)
          if (!data.running && data.phase === 'complete') {
            onRefresh()
            const statsRes = await fetch(`${API_BASE}/api/v1/admin/nibis/stats`)
            if (statsRes.ok) setIndexedStats(await statsRes.json())
          }
        }
      } catch (err) { console.error('Failed to fetch progress:', err) }
    }
    fetchProgress()
    interval = setInterval(fetchProgress, 1500)
    return () => { if (interval) clearInterval(interval) }
  }, [onRefresh])

  const startIngestion = async () => {
    setStarting(true)
    try {
      await fetch(`${API_BASE}/api/v1/admin/nibis/ingest`, {
        method: 'POST', headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ ewh_only: true, incremental: true }),
      })
      onRefresh()
    } catch (err) { console.error('Failed to start ingestion:', err) }
    finally { setStarting(false) }
  }

  const phaseLabels: Record<string, string> = {
    idle: 'Bereit', extracting: 'Entpacke ZIP-Dateien...', discovering: 'Suche Dokumente...',
    indexing: 'Indexiere Dokumente...', complete: 'Abgeschlossen',
  }

  return (
    <div className="space-y-6">
      <div className="flex items-center justify-between">
        <div>
          <h2 className="text-lg font-semibold text-slate-900">Ingestion Status</h2>
          <p className="text-sm text-slate-500">Uebersicht ueber laufende und vergangene Indexierungsvorgaenge</p>
        </div>
        <div className="flex gap-3">
          <button onClick={onRefresh} className="px-4 py-2 text-sm font-medium text-slate-700 bg-white border border-slate-300 rounded-lg hover:bg-slate-50">Aktualisieren</button>
          <button onClick={startIngestion} disabled={status?.running || starting} className="px-4 py-2 text-sm font-medium text-white bg-primary-600 rounded-lg hover:bg-primary-700 disabled:opacity-50 disabled:cursor-not-allowed">
            {starting ? 'Startet...' : 'Ingestion starten'}
          </button>
        </div>
      </div>

      {/* Live Progress */}
      {liveProgress?.running && (
        <div className="bg-primary-50 rounded-lg border border-primary-200 p-6">
          <div className="flex items-center gap-4 mb-4">
            <div className="w-3 h-3 bg-primary-500 rounded-full animate-pulse"></div>
            <span className="text-lg font-medium text-primary-900">{liveProgress.phase ? (phaseLabels[liveProgress.phase] || liveProgress.phase) : 'Laeuft...'}</span>
            <span className="ml-auto text-sm text-primary-700 font-mono">{(liveProgress.percent ?? 0).toFixed(1)}%</span>
          </div>
          <div className="h-3 bg-primary-200 rounded-full overflow-hidden mb-4">
            <div className="h-full bg-primary-600 transition-all duration-300" style={{ width: `${liveProgress.percent ?? 0}%` }} />
          </div>
          {liveProgress.current_filename && (
            <p className="text-sm text-primary-700 mb-4 truncate">
              <span className="font-medium">[{liveProgress.current_doc}/{liveProgress.total_docs}]</span> {liveProgress.current_filename}
            </p>
          )}
          <div className="grid grid-cols-2 md:grid-cols-4 gap-4 text-sm">
            <div className="bg-white/50 rounded p-2"><p className="text-primary-600 text-xs uppercase">Indexiert</p><p className="text-lg font-semibold text-primary-900">{liveProgress.documents_indexed}</p></div>
            <div className="bg-white/50 rounded p-2"><p className="text-primary-600 text-xs uppercase">Uebersprungen</p><p className="text-lg font-semibold text-primary-900">{liveProgress.documents_skipped}</p></div>
            <div className="bg-white/50 rounded p-2"><p className="text-primary-600 text-xs uppercase">Chunks</p><p className="text-lg font-semibold text-primary-900">{liveProgress.chunks_created}</p></div>
            <div className="bg-white/50 rounded p-2"><p className="text-primary-600 text-xs uppercase">Fehler</p><p className={`text-lg font-semibold ${(liveProgress.errors_count ?? 0) > 0 ? 'text-red-600' : 'text-primary-900'}`}>{liveProgress.errors_count ?? 0}</p></div>
          </div>
        </div>
      )}

      {/* Indexed Data Overview */}
      {indexedStats?.indexed && (
        <div className="bg-gradient-to-r from-green-50 to-emerald-50 rounded-lg border border-green-200 p-6">
          <h3 className="text-lg font-semibold text-green-900 mb-4 flex items-center gap-2">
            <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" /></svg>
            Indexierte Daten (Gesamt)
          </h3>
          <div className="grid grid-cols-2 md:grid-cols-4 gap-6 mb-4">
            <div className="bg-white/60 rounded-lg p-3"><p className="text-xs text-green-700 uppercase tracking-wider">Chunks gesamt</p><p className="text-2xl font-bold text-green-900">{(indexedStats.total_chunks ?? 0).toLocaleString()}</p></div>
            <div className="bg-white/60 rounded-lg p-3"><p className="text-xs text-green-700 uppercase tracking-wider">Jahre</p><p className="text-2xl font-bold text-green-900">{indexedStats.years?.length ?? 0}</p><p className="text-xs text-green-600 mt-1">{(indexedStats.years?.length ?? 0) > 0 ? `${Math.min(...indexedStats.years!)} - ${Math.max(...indexedStats.years!)}` : '-'}</p></div>
            <div className="bg-white/60 rounded-lg p-3"><p className="text-xs text-green-700 uppercase tracking-wider">Faecher</p><p className="text-2xl font-bold text-green-900">{indexedStats.subjects?.length || 0}</p></div>
            <div className="bg-white/60 rounded-lg p-3"><p className="text-xs text-green-700 uppercase tracking-wider">Status</p><p className="text-lg font-bold text-green-600">Bereit</p></div>
          </div>
          {indexedStats.years && indexedStats.years.length > 0 && (
            <div className="flex flex-wrap gap-2">
              {indexedStats.years.sort((a, b) => a - b).map((year) => (
                <span key={year} className="px-3 py-1 bg-white/80 text-green-800 text-sm rounded-full font-medium">{year}</span>
              ))}
            </div>
          )}
        </div>
      )}

      {/* Last Run Status */}
      {!liveProgress?.running && (
        <div className="bg-white rounded-lg border border-slate-200 p-6">
          <h3 className="text-sm font-medium text-slate-700 mb-4">Letzter Indexierungslauf</h3>
          <div className="flex items-center gap-4 mb-4">
            <div className="w-3 h-3 bg-green-500 rounded-full"></div>
            <span className="text-lg font-medium text-slate-900">{liveProgress?.phase === 'complete' ? 'Abgeschlossen' : 'Bereit'}</span>
          </div>
          {status && (
            <div className="grid grid-cols-2 md:grid-cols-4 gap-6">
              <div><p className="text-xs text-slate-500 uppercase tracking-wider">Zeitpunkt</p><p className="text-lg font-semibold text-slate-900">{status.lastRun ? new Date(status.lastRun).toLocaleString('de-DE') : 'Noch nie'}</p></div>
              <div><p className="text-xs text-slate-500 uppercase tracking-wider">Neu indexiert</p><p className="text-lg font-semibold text-slate-900">{status.documentsIndexed ?? '-'}</p></div>
              <div><p className="text-xs text-slate-500 uppercase tracking-wider">Neue Chunks</p><p className="text-lg font-semibold text-slate-900">{status.chunksCreated ?? '-'}</p></div>
              <div><p className="text-xs text-slate-500 uppercase tracking-wider">Fehler</p><p className={`text-lg font-semibold ${status.errors.length > 0 ? 'text-red-600' : 'text-slate-900'}`}>{status.errors.length}</p></div>
            </div>
          )}
          {liveProgress && (liveProgress.documents_skipped ?? 0) > 0 && (
            <p className="mt-3 text-sm text-slate-500">{liveProgress.documents_skipped} Dokumente uebersprungen (bereits indexiert)</p>
          )}
          {status?.errors && status.errors.length > 0 && (
            <div className="mt-4 p-4 bg-red-50 rounded-lg">
              <h4 className="text-sm font-medium text-red-800 mb-2">Fehler</h4>
              <ul className="text-sm text-red-700 space-y-1">
                {status.errors.slice(0, 5).map((error, i) => <li key={i}>{error}</li>)}
                {status.errors.length > 5 && <li className="text-red-500">... und {status.errors.length - 5} weitere</li>}
              </ul>
            </div>
          )}
        </div>
      )}

      {/* Pending Files Section */}
      {pendingFiles && (pendingFiles.pending_count ?? 0) > 0 && (
        <div className="bg-amber-50 rounded-lg border border-amber-200 p-6">
          <div className="flex items-center justify-between mb-4">
            <div className="flex items-center gap-3">
              <svg className="w-6 h-6 text-amber-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" /></svg>
              <div>
                <h3 className="text-lg font-semibold text-amber-900">{pendingFiles.pending_count} Dateien warten auf Indexierung</h3>
                <p className="text-sm text-amber-700">{pendingFiles.indexed_count} von {pendingFiles.total_files} Dateien sind indexiert</p>
              </div>
            </div>
            <button onClick={() => setShowPending(!showPending)} className="text-sm text-amber-700 hover:text-amber-900 font-medium">{showPending ? 'Ausblenden' : 'Details anzeigen'}</button>
          </div>
          {pendingFiles.by_year && Object.keys(pendingFiles.by_year).length > 0 && (
            <div className="flex flex-wrap gap-2 mb-4">
              {Object.entries(pendingFiles.by_year).sort(([a], [b]) => Number(b) - Number(a)).map(([year, count]) => (
                <span key={year} className="px-3 py-1 bg-amber-100 text-amber-800 text-sm rounded-full font-medium">{year}: {count} Dateien</span>
              ))}
            </div>
          )}
          {showPending && pendingFiles.pending_files && (
            <div className="mt-4 max-h-64 overflow-y-auto">
              <table className="w-full text-sm">
                <thead className="text-left text-amber-700 border-b border-amber-200"><tr><th className="pb-2">Dateiname</th><th className="pb-2">Jahr</th><th className="pb-2">Fach</th><th className="pb-2">Niveau</th></tr></thead>
                <tbody className="divide-y divide-amber-100">
                  {pendingFiles.pending_files.map((file) => (
                    <tr key={file.id} className="text-amber-900"><td className="py-2 font-mono text-xs">{file.filename}</td><td className="py-2">{file.year}</td><td className="py-2">{file.subject}</td><td className="py-2">{file.niveau}</td></tr>
                  ))}
                </tbody>
              </table>
              {(pendingFiles.pending_count ?? 0) > 100 && <p className="mt-2 text-xs text-amber-600">Zeige 100 von {pendingFiles.pending_count} Dateien</p>}
            </div>
          )}
        </div>
      )}

      <IngestionHistory history={ingestionHistory} />
    </div>
  )
}

export { IngestionTab }
