'use client'

import React from 'react'
import Link from 'next/link'
import { useSDK, getStepsForPhase } from '@/lib/sdk'

// =============================================================================
// DASHBOARD CARDS
// =============================================================================

function StatCard({
  title,
  value,
  subtitle,
  icon,
  color,
}: {
  title: string
  value: string | number
  subtitle: string
  icon: React.ReactNode
  color: string
}) {
  return (
    <div className="bg-white rounded-xl border border-gray-200 p-6">
      <div className="flex items-start justify-between">
        <div>
          <p className="text-sm text-gray-500">{title}</p>
          <p className="mt-1 text-3xl font-bold text-gray-900">{value}</p>
          <p className="mt-1 text-sm text-gray-500">{subtitle}</p>
        </div>
        <div className={`p-3 rounded-lg ${color}`}>{icon}</div>
      </div>
    </div>
  )
}

function PhaseCard({
  phase,
  title,
  description,
  completion,
  steps,
  href,
}: {
  phase: number
  title: string
  description: string
  completion: number
  steps: number
  href: string
}) {
  return (
    <Link
      href={href}
      className="block bg-white rounded-xl border border-gray-200 p-6 hover:border-purple-300 hover:shadow-lg transition-all"
    >
      <div className="flex items-start gap-4">
        <div
          className={`w-12 h-12 rounded-xl flex items-center justify-center text-xl font-bold ${
            completion === 100
              ? 'bg-green-100 text-green-600'
              : 'bg-purple-100 text-purple-600'
          }`}
        >
          {completion === 100 ? (
            <svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
            </svg>
          ) : (
            phase
          )}
        </div>
        <div className="flex-1">
          <h3 className="text-lg font-semibold text-gray-900">{title}</h3>
          <p className="mt-1 text-sm text-gray-500">{description}</p>
          <div className="mt-4">
            <div className="flex items-center justify-between text-sm mb-1">
              <span className="text-gray-500">{steps} Schritte</span>
              <span className="font-medium text-purple-600">{completion}%</span>
            </div>
            <div className="h-2 bg-gray-100 rounded-full overflow-hidden">
              <div
                className={`h-full rounded-full transition-all duration-500 ${
                  completion === 100 ? 'bg-green-500' : 'bg-purple-600'
                }`}
                style={{ width: `${completion}%` }}
              />
            </div>
          </div>
        </div>
      </div>
    </Link>
  )
}

function QuickActionCard({
  title,
  description,
  icon,
  href,
  color,
}: {
  title: string
  description: string
  icon: React.ReactNode
  href: string
  color: string
}) {
  return (
    <Link
      href={href}
      className="flex items-center gap-4 p-4 bg-white rounded-xl border border-gray-200 hover:border-purple-300 hover:shadow-md transition-all"
    >
      <div className={`p-3 rounded-lg ${color}`}>{icon}</div>
      <div>
        <h4 className="font-medium text-gray-900">{title}</h4>
        <p className="text-sm text-gray-500">{description}</p>
      </div>
      <svg className="w-5 h-5 text-gray-400 ml-auto" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
      </svg>
    </Link>
  )
}

// =============================================================================
// MAIN DASHBOARD
// =============================================================================

export default function SDKDashboard() {
  const { state, phase1Completion, phase2Completion, completionPercentage } = useSDK()

  const phase1Steps = getStepsForPhase(1)
  const phase2Steps = getStepsForPhase(2)

  // Calculate stats
  const completedCheckpoints = Object.values(state.checkpoints).filter(cp => cp.passed).length
  const totalRisks = state.risks.length
  const criticalRisks = state.risks.filter(r => r.severity === 'CRITICAL' || r.severity === 'HIGH').length

  return (
    <div className="space-y-8">
      {/* Header */}
      <div>
        <h1 className="text-2xl font-bold text-gray-900">AI Compliance SDK</h1>
        <p className="mt-1 text-gray-500">
          Willkommen zum Compliance Assessment. Starten Sie mit Phase 1 oder setzen Sie Ihre Arbeit fort.
        </p>
      </div>

      {/* Stats Grid */}
      <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
        <StatCard
          title="Gesamtfortschritt"
          value={`${completionPercentage}%`}
          subtitle={`${state.completedSteps.length} von ${phase1Steps.length + phase2Steps.length} Schritten`}
          icon={
            <svg className="w-6 h-6 text-purple-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z" />
            </svg>
          }
          color="bg-purple-50"
        />
        <StatCard
          title="Use Cases"
          value={state.useCases.length}
          subtitle={state.useCases.length === 0 ? 'Noch keine erstellt' : 'Erfasst'}
          icon={
            <svg className="w-6 h-6 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="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 2" />
            </svg>
          }
          color="bg-blue-50"
        />
        <StatCard
          title="Checkpoints"
          value={`${completedCheckpoints}/${phase1Steps.length + phase2Steps.length}`}
          subtitle="Validiert"
          icon={
            <svg className="w-6 h-6 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
            </svg>
          }
          color="bg-green-50"
        />
        <StatCard
          title="Risiken"
          value={totalRisks}
          subtitle={criticalRisks > 0 ? `${criticalRisks} kritisch` : 'Keine kritischen'}
          icon={
            <svg className="w-6 h-6 text-orange-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>
          }
          color="bg-orange-50"
        />
      </div>

      {/* Phases */}
      <div>
        <h2 className="text-lg font-semibold text-gray-900 mb-4">Phasen</h2>
        <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
          <PhaseCard
            phase={1}
            title="Compliance Assessment"
            description="Use Case erfassen, Screening durchführen, Risiken bewerten"
            completion={phase1Completion}
            steps={phase1Steps.length}
            href="/sdk/advisory-board"
          />
          <PhaseCard
            phase={2}
            title="Dokumentengenerierung"
            description="DSFA, TOMs, VVT, Cookie Banner und mehr generieren"
            completion={phase2Completion}
            steps={phase2Steps.length}
            href="/sdk/ai-act"
          />
        </div>
      </div>

      {/* Quick Actions */}
      <div>
        <h2 className="text-lg font-semibold text-gray-900 mb-4">Schnellaktionen</h2>
        <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
          <QuickActionCard
            title="Neuen Use Case erstellen"
            description="Starten Sie den 5-Schritte-Wizard"
            icon={
              <svg className="w-6 h-6 text-purple-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 6v6m0 0v6m0-6h6m-6 0H6" />
              </svg>
            }
            href="/sdk/advisory-board"
            color="bg-purple-50"
          />
          <QuickActionCard
            title="Security Screening"
            description="SBOM generieren und Schwachstellen scannen"
            icon={
              <svg className="w-6 h-6 text-red-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z" />
              </svg>
            }
            href="/sdk/screening"
            color="bg-red-50"
          />
          <QuickActionCard
            title="DSFA generieren"
            description="Datenschutz-Folgenabschätzung erstellen"
            icon={
              <svg className="w-6 h-6 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} 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>
            }
            href="/sdk/dsfa"
            color="bg-blue-50"
          />
          <QuickActionCard
            title="Legal RAG"
            description="Rechtliche Fragen stellen und Antworten erhalten"
            icon={
              <svg className="w-6 h-6 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 16l2.879-2.879m0 0a3 3 0 104.243-4.242 3 3 0 00-4.243 4.242zM21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
              </svg>
            }
            href="/sdk/rag"
            color="bg-green-50"
          />
        </div>
      </div>

      {/* Recent Activity */}
      {state.commandBarHistory.length > 0 && (
        <div>
          <h2 className="text-lg font-semibold text-gray-900 mb-4">Letzte Aktivitäten</h2>
          <div className="bg-white rounded-xl border border-gray-200 divide-y divide-gray-100">
            {state.commandBarHistory.slice(0, 5).map(entry => (
              <div key={entry.id} className="flex items-center gap-4 px-4 py-3">
                <div
                  className={`w-8 h-8 rounded-full flex items-center justify-center ${
                    entry.success ? 'bg-green-100 text-green-600' : 'bg-red-100 text-red-600'
                  }`}
                >
                  {entry.success ? (
                    <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                      <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
                    </svg>
                  ) : (
                    <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                      <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
                    </svg>
                  )}
                </div>
                <div className="flex-1">
                  <p className="text-sm font-medium text-gray-900">{entry.query}</p>
                  <p className="text-xs text-gray-500">
                    {new Date(entry.timestamp).toLocaleString('de-DE')}
                  </p>
                </div>
                <span className="px-2 py-1 text-xs bg-gray-100 text-gray-600 rounded-full">
                  {entry.type}
                </span>
              </div>
            ))}
          </div>
        </div>
      )}
    </div>
  )
}
