'use client'

import Link from 'next/link'
import type { DashboardData, MappingsData, FindingsData, NextAction } from './types'
import { DOMAIN_LABELS } from './types'

interface OverviewTabProps {
  dashboard: DashboardData | null
  mappings: MappingsData | null
  findings: FindingsData | null
  nextActions: NextAction[]
  evidenceDistribution: {
    by_confidence: Record<string, number>
    four_eyes_pending: number
    total: number
  } | null
  score: number
  scoreColor: string
  scoreBgColor: string
  loadData: () => void
  regulations: Array<{ id: string; code: string; name: string; regulation_type: string; requirement_count: number }>
}

export function OverviewTab({
  dashboard, mappings, findings, nextActions, evidenceDistribution,
  score, scoreColor, scoreBgColor, loadData, regulations,
}: OverviewTabProps) {
  return (
    <>
      {/* Quick Actions */}
      <div className="bg-white rounded-xl shadow-sm border p-6">
        <h3 className="text-lg font-semibold text-slate-900 mb-4">Schnellzugriff</h3>
        <div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-6 gap-4">
          {[
            { href: '/sdk/audit-checklist', icon: 'M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-3 7h3m-3 4h3m-6-4h.01M9 16h.01', label: 'Audit Checkliste', sub: `${dashboard?.total_requirements || '...'} Anforderungen`, color: 'purple' },
            { href: '/sdk/controls', icon: 'M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z', label: 'Controls', sub: `${dashboard?.total_controls || '...'} Massnahmen`, color: 'green' },
            { href: '/sdk/evidence', icon: 'M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z', label: 'Evidence', sub: 'Nachweise', color: 'blue' },
            { href: '/sdk/risks', icon: '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', label: 'Risk Matrix', sub: '5x5 Risiken', color: 'red' },
            { href: '/sdk/process-tasks', icon: 'M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4', label: 'Prozesse', sub: 'Aufgaben', color: 'indigo' },
            { href: '/sdk/audit-report', icon: 'M9 17v-2m3 2v-4m3 4v-6m2 10H7a2 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', label: 'Audit Report', sub: 'PDF Export', color: 'orange' },
          ].map(item => (
            <Link
              key={item.href}
              href={item.href}
              className={`p-4 rounded-lg border border-slate-200 hover:border-${item.color}-500 hover:bg-${item.color}-50 transition-colors text-center`}
            >
              <div className={`text-${item.color}-600 mb-2 flex justify-center`}>
                <svg className="w-8 h-8" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d={item.icon} />
                </svg>
              </div>
              <p className="font-medium text-slate-900 text-sm">{item.label}</p>
              <p className="text-xs text-slate-500 mt-1">{item.sub}</p>
            </Link>
          ))}
        </div>
      </div>

      {/* Score and Stats Row */}
      <div className="grid grid-cols-1 lg:grid-cols-5 gap-4">
        <div className="bg-white rounded-xl shadow-sm border p-6">
          <h3 className="text-sm font-medium text-slate-500 mb-4">Compliance Score</h3>
          <div className={`text-5xl font-bold ${scoreColor}`}>
            {score.toFixed(0)}%
          </div>
          <div className="mt-4 h-2 bg-slate-200 rounded-full overflow-hidden">
            <div className={`h-full transition-all duration-500 ${scoreBgColor}`} style={{ width: `${score}%` }} />
          </div>
          <p className="mt-2 text-sm text-slate-500">
            {dashboard?.controls_by_status?.pass || 0} von {dashboard?.total_controls || 0} Controls bestanden
          </p>
        </div>

        {[
          { label: 'Verordnungen', value: dashboard?.total_regulations || 0, sub: `${dashboard?.total_requirements || 0} Anforderungen`, iconColor: 'blue', icon: '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' },
          { label: 'Controls', value: dashboard?.total_controls || 0, sub: `${dashboard?.controls_by_status?.pass || 0} bestanden`, iconColor: 'green', icon: 'M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z' },
          { label: 'Nachweise', value: dashboard?.total_evidence || 0, sub: `${dashboard?.evidence_by_status?.valid || 0} aktiv`, iconColor: 'purple', icon: 'M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z' },
          { label: 'Risiken', value: dashboard?.total_risks || 0, sub: `${(dashboard?.risks_by_level?.high || 0) + (dashboard?.risks_by_level?.critical || 0)} kritisch`, iconColor: 'red', icon: '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' },
        ].map(stat => (
          <div key={stat.label} className="bg-white rounded-xl shadow-sm border p-6">
            <div className="flex items-center justify-between">
              <div>
                <p className="text-sm text-slate-500">{stat.label}</p>
                <p className="text-2xl font-bold text-slate-900">{stat.value}</p>
              </div>
              <div className={`w-10 h-10 bg-${stat.iconColor}-100 rounded-lg flex items-center justify-center`}>
                <svg className={`w-5 h-5 text-${stat.iconColor}-600`} fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d={stat.icon} />
                </svg>
              </div>
            </div>
            <p className="mt-2 text-sm text-slate-500">{stat.sub}</p>
          </div>
        ))}
      </div>

      {/* Anti-Fake-Evidence Section (Phase 3) */}
      {dashboard && (
        <div className="bg-white rounded-xl shadow-sm border p-6">
          <h3 className="text-lg font-semibold text-slate-900 mb-4">Anti-Fake-Evidence Status</h3>

          {/* Confidence Distribution Bar */}
          {evidenceDistribution && evidenceDistribution.total > 0 && (
            <div className="mb-6">
              <p className="text-sm text-slate-500 mb-2">Confidence-Verteilung ({evidenceDistribution.total} Nachweise)</p>
              <div className="flex h-6 rounded-full overflow-hidden">
                {(['E0', 'E1', 'E2', 'E3', 'E4'] as const).map(level => {
                  const count = evidenceDistribution.by_confidence[level] || 0
                  const pct = (count / evidenceDistribution.total) * 100
                  if (pct === 0) return null
                  const colors: Record<string, string> = {
                    E0: 'bg-red-400', E1: 'bg-yellow-400', E2: 'bg-blue-400', E3: 'bg-green-400', E4: 'bg-emerald-400'
                  }
                  return (
                    <div key={level} className={`${colors[level]} flex items-center justify-center text-xs text-white font-medium`}
                      style={{ width: `${pct}%` }} title={`${level}: ${count}`}>
                      {pct >= 10 ? `${level} (${count})` : ''}
                    </div>
                  )
                })}
              </div>
              <div className="flex items-center gap-4 mt-2 text-xs text-slate-500">
                {(['E0', 'E1', 'E2', 'E3', 'E4'] as const).map(level => {
                  const count = evidenceDistribution.by_confidence[level] || 0
                  const dotColors: Record<string, string> = {
                    E0: 'bg-red-400', E1: 'bg-yellow-400', E2: 'bg-blue-400', E3: 'bg-green-400', E4: 'bg-emerald-400'
                  }
                  return (
                    <span key={level} className="flex items-center gap-1">
                      <span className={`w-2 h-2 rounded-full ${dotColors[level]}`} />
                      {level}: {count}
                    </span>
                  )
                })}
              </div>
            </div>
          )}

          {/* Multi-Score Dimensions */}
          {dashboard.multi_score && (
            <div className="mb-6">
              <p className="text-sm text-slate-500 mb-2">Multi-dimensionaler Score</p>
              <div className="space-y-2">
                {([
                  { key: 'requirement_coverage', label: 'Anforderungsabdeckung', color: 'bg-blue-500' },
                  { key: 'evidence_strength', label: 'Evidence-Staerke', color: 'bg-green-500' },
                  { key: 'validation_quality', label: 'Validierungsqualitaet', color: 'bg-purple-500' },
                  { key: 'evidence_freshness', label: 'Aktualitaet', color: 'bg-yellow-500' },
                  { key: 'control_effectiveness', label: 'Control-Wirksamkeit', color: 'bg-indigo-500' },
                ] as const).map(dim => {
                  const value = (dashboard.multi_score as unknown as Record<string, number>)[dim.key] || 0
                  return (
                    <div key={dim.key} className="flex items-center gap-3">
                      <span className="text-xs text-slate-600 w-44 truncate">{dim.label}</span>
                      <div className="flex-1 h-2 bg-slate-200 rounded-full overflow-hidden">
                        <div className={`h-full ${dim.color} rounded-full transition-all`} style={{ width: `${value}%` }} />
                      </div>
                      <span className="text-xs text-slate-600 w-12 text-right">{typeof value === 'number' ? value.toFixed(0) : value}%</span>
                    </div>
                  )
                })}
                <div className="flex items-center gap-3 pt-2 border-t border-slate-100">
                  <span className="text-xs font-semibold text-slate-700 w-44">Audit-Readiness</span>
                  <div className="flex-1 h-3 bg-slate-200 rounded-full overflow-hidden">
                    <div className={`h-full rounded-full transition-all ${
                      (dashboard.multi_score.overall_readiness || 0) >= 80 ? 'bg-green-500' :
                      (dashboard.multi_score.overall_readiness || 0) >= 60 ? 'bg-yellow-500' : 'bg-red-500'
                    }`} style={{ width: `${dashboard.multi_score.overall_readiness || 0}%` }} />
                  </div>
                  <span className="text-xs font-semibold text-slate-700 w-12 text-right">
                    {typeof dashboard.multi_score.overall_readiness === 'number' ? dashboard.multi_score.overall_readiness.toFixed(0) : 0}%
                  </span>
                </div>
              </div>
            </div>
          )}

          {/* Bottom row: Four-Eyes + Hard Blocks */}
          <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div className="text-center p-3 rounded-lg bg-yellow-50">
              <div className="text-2xl font-bold text-yellow-700">{evidenceDistribution?.four_eyes_pending || 0}</div>
              <div className="text-xs text-yellow-600 mt-1">Four-Eyes Reviews ausstehend</div>
            </div>
            {dashboard.multi_score?.hard_blocks && dashboard.multi_score.hard_blocks.length > 0 ? (
              <div className="p-3 rounded-lg bg-red-50">
                <div className="text-xs font-medium text-red-700 mb-1">Hard Blocks ({dashboard.multi_score.hard_blocks.length})</div>
                <ul className="space-y-1">
                  {dashboard.multi_score.hard_blocks.slice(0, 3).map((block: string, i: number) => (
                    <li key={i} className="text-xs text-red-600 flex items-start gap-1">
                      <span className="text-red-400 mt-0.5">&#8226;</span>
                      <span>{block}</span>
                    </li>
                  ))}
                </ul>
              </div>
            ) : (
              <div className="text-center p-3 rounded-lg bg-green-50">
                <div className="text-2xl font-bold text-green-700">0</div>
                <div className="text-xs text-green-600 mt-1">Keine Hard Blocks</div>
              </div>
            )}
          </div>
        </div>
      )}

      {/* Next Actions + Findings */}
      <div className="grid grid-cols-1 lg:grid-cols-2 gap-4">
        {/* Next Actions */}
        <div className="bg-white rounded-xl shadow-sm border p-6">
          <h3 className="text-lg font-semibold text-slate-900 mb-4">Naechste Aktionen</h3>
          {nextActions.length === 0 ? (
            <p className="text-sm text-slate-500">Keine offenen Aktionen.</p>
          ) : (
            <div className="space-y-3">
              {nextActions.map(action => (
                <div key={action.id} className="flex items-center gap-3 p-3 rounded-lg bg-slate-50">
                  <div className={`w-2 h-2 rounded-full flex-shrink-0 ${
                    action.days_overdue > 0 ? 'bg-red-500' : 'bg-yellow-500'
                  }`} />
                  <div className="flex-1 min-w-0">
                    <p className="text-sm font-medium text-slate-900 truncate">{action.title}</p>
                    <p className="text-xs text-slate-500">
                      {action.control_id} · {DOMAIN_LABELS[action.domain] || action.domain}
                      {action.days_overdue > 0 && <span className="text-red-600 ml-2">{action.days_overdue}d ueberfaellig</span>}
                    </p>
                  </div>
                  <span className={`px-2 py-0.5 text-xs rounded-full ${
                    action.status === 'partial' ? 'bg-yellow-100 text-yellow-700' : 'bg-slate-100 text-slate-600'
                  }`}>
                    {action.status}
                  </span>
                </div>
              ))}
            </div>
          )}
        </div>

        {/* Audit Findings */}
        <div className="bg-white rounded-xl shadow-sm border p-6">
          <div className="flex items-center justify-between mb-4">
            <h3 className="text-lg font-semibold text-slate-900">Audit Findings</h3>
            <Link href="/sdk/audit-checklist" className="text-sm text-purple-600 hover:text-purple-700">
              Audit Checkliste →
            </Link>
          </div>
          <div className="grid grid-cols-2 gap-4 mb-4">
            <div className="p-4 bg-red-50 border border-red-200 rounded-lg">
              <div className="flex items-center gap-2 mb-1">
                <div className="w-3 h-3 bg-red-500 rounded-full" />
                <span className="text-sm font-medium text-red-800">Hauptabweichungen</span>
              </div>
              <p className="text-3xl font-bold text-red-600">{findings?.open_majors || 0}</p>
              <p className="text-xs text-red-600">offen (blockiert Zertifizierung)</p>
            </div>
            <div className="p-4 bg-yellow-50 border border-yellow-200 rounded-lg">
              <div className="flex items-center gap-2 mb-1">
                <div className="w-3 h-3 bg-yellow-500 rounded-full" />
                <span className="text-sm font-medium text-yellow-800">Nebenabweichungen</span>
              </div>
              <p className="text-3xl font-bold text-yellow-600">{findings?.open_minors || 0}</p>
              <p className="text-xs text-yellow-600">offen (erfordert CAPA)</p>
            </div>
          </div>
          <div className="flex items-center justify-between text-sm">
            <span className="text-slate-500">
              Gesamt: {findings?.total || 0} Findings ({findings?.major_count || 0} Major, {findings?.minor_count || 0} Minor, {findings?.ofi_count || 0} OFI)
            </span>
            {(findings?.open_majors || 0) === 0 ? (
              <span className="px-2 py-1 bg-green-100 text-green-700 rounded-full text-xs font-medium">
                Zertifizierung moeglich
              </span>
            ) : (
              <span className="px-2 py-1 bg-red-100 text-red-700 rounded-full text-xs font-medium">
                Zertifizierung blockiert
              </span>
            )}
          </div>
        </div>
      </div>

      {/* Control-Mappings & Domain Chart */}
      <div className="grid grid-cols-1 lg:grid-cols-2 gap-4">
        <div className="bg-white rounded-xl shadow-sm border p-6">
          <div className="flex items-center justify-between mb-4">
            <h3 className="text-lg font-semibold text-slate-900">Control-Mappings</h3>
            <Link href="/sdk/controls" className="text-sm text-purple-600 hover:text-purple-700">
              Alle anzeigen →
            </Link>
          </div>
          <div className="flex items-center gap-6 mb-4">
            <div>
              <p className="text-4xl font-bold text-purple-600">{mappings?.total || 0}</p>
              <p className="text-sm text-slate-500">Mappings gesamt</p>
            </div>
            <div className="flex-1 h-16 bg-slate-50 rounded-lg p-3">
              <p className="text-xs text-slate-500 mb-1">Nach Verordnung</p>
              <div className="flex gap-1 flex-wrap">
                {mappings?.by_regulation && Object.entries(mappings.by_regulation).slice(0, 5).map(([reg, count]) => (
                  <span key={reg} className="px-2 py-0.5 bg-purple-100 text-purple-700 rounded text-xs">
                    {reg}: {count}
                  </span>
                ))}
                {!mappings?.by_regulation && (
                  <span className="px-2 py-0.5 bg-slate-100 text-slate-500 rounded text-xs">Keine Mappings vorhanden</span>
                )}
              </div>
            </div>
          </div>
        </div>

        <div className="bg-white rounded-xl shadow-sm border p-6">
          <h3 className="text-lg font-semibold text-slate-900 mb-4">Controls nach Domain</h3>
          <div className="space-y-2">
            {Object.entries(dashboard?.controls_by_domain || {}).slice(0, 6).map(([domain, stats]) => {
              const total = stats.total || 0
              const pass = stats.pass || 0
              const partial = stats.partial || 0
              const passPercent = total > 0 ? ((pass + partial * 0.5) / total) * 100 : 0

              return (
                <div key={domain} className="flex items-center gap-3">
                  <span className="text-xs font-medium text-slate-600 w-24 truncate">
                    {DOMAIN_LABELS[domain] || domain}
                  </span>
                  <div className="flex-1 h-2 bg-slate-200 rounded-full overflow-hidden flex">
                    <div className="bg-green-500 h-full" style={{ width: `${(pass / (total || 1)) * 100}%` }} />
                    <div className="bg-yellow-500 h-full" style={{ width: `${(partial / (total || 1)) * 100}%` }} />
                  </div>
                  <span className="text-xs text-slate-500 w-16 text-right">{passPercent.toFixed(0)}%</span>
                </div>
              )
            })}
          </div>
        </div>
      </div>

      {/* Regulations Table */}
      <div className="bg-white rounded-xl shadow-sm border overflow-hidden">
        <div className="p-4 border-b flex items-center justify-between">
          <h3 className="text-lg font-semibold text-slate-900">Verordnungen & Standards ({regulations.length})</h3>
          <button onClick={loadData} className="text-sm text-purple-600 hover:text-purple-700">
            Aktualisieren
          </button>
        </div>
        <div className="overflow-x-auto">
          <table className="w-full">
            <thead className="bg-slate-50">
              <tr>
                <th className="px-4 py-3 text-left text-xs font-medium text-slate-500 uppercase">Code</th>
                <th className="px-4 py-3 text-left text-xs font-medium text-slate-500 uppercase">Name</th>
                <th className="px-4 py-3 text-left text-xs font-medium text-slate-500 uppercase">Typ</th>
                <th className="px-4 py-3 text-center text-xs font-medium text-slate-500 uppercase">Anforderungen</th>
              </tr>
            </thead>
            <tbody className="divide-y divide-slate-200">
              {regulations.slice(0, 15).map((reg) => (
                <tr key={reg.id} className="hover:bg-slate-50">
                  <td className="px-4 py-3">
                    <span className="font-mono font-medium text-purple-600">{reg.code}</span>
                  </td>
                  <td className="px-4 py-3">
                    <p className="font-medium text-slate-900">{reg.name}</p>
                  </td>
                  <td className="px-4 py-3">
                    <span className={`px-2 py-1 text-xs rounded-full ${
                      reg.regulation_type === 'eu_regulation' ? 'bg-blue-100 text-blue-700' :
                      reg.regulation_type === 'eu_directive' ? 'bg-purple-100 text-purple-700' :
                      reg.regulation_type === 'bsi_standard' ? 'bg-green-100 text-green-700' :
                      'bg-slate-100 text-slate-700'
                    }`}>
                      {reg.regulation_type === 'eu_regulation' ? 'EU-VO' :
                       reg.regulation_type === 'eu_directive' ? 'EU-RL' :
                       reg.regulation_type === 'bsi_standard' ? 'BSI' :
                       reg.regulation_type === 'de_law' ? 'DE' : reg.regulation_type}
                    </span>
                  </td>
                  <td className="px-4 py-3 text-center">
                    <span className="font-medium">{reg.requirement_count}</span>
                  </td>
                </tr>
              ))}
            </tbody>
          </table>
        </div>
      </div>
    </>
  )
}
