'use client'

import type { BQASMetrics } from '../types'

export function TestSuiteCard({
  title,
  description,
  metrics,
  onRun,
  isRunning,
  lastRun,
}: {
  title: string
  description: string
  metrics?: BQASMetrics
  onRun: () => void
  isRunning: boolean
  lastRun?: string
}) {
  const passRate = metrics ? (metrics.passed_tests / metrics.total_tests) * 100 : 0

  return (
    <div className="bg-white rounded-xl border border-slate-200 p-6">
      <div className="flex items-start justify-between">
        <div>
          <h3 className="text-lg font-semibold text-slate-900">{title}</h3>
          <p className="mt-1 text-sm text-slate-500">{description}</p>
        </div>
        <button
          onClick={onRun}
          disabled={isRunning}
          className={`px-4 py-2 rounded-lg text-sm font-medium transition-all ${
            isRunning
              ? 'bg-teal-100 text-teal-600 cursor-wait'
              : 'bg-teal-600 text-white hover:bg-teal-700 active:scale-95'
          }`}
        >
          {isRunning ? (
            <span className="flex items-center gap-2">
              <svg className="animate-spin h-4 w-4" fill="none" viewBox="0 0 24 24">
                <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" />
                <path
                  className="opacity-75"
                  fill="currentColor"
                  d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
                />
              </svg>
              Laeuft...
            </span>
          ) : (
            'Tests starten'
          )}
        </button>
      </div>

      {metrics && (
        <div className="mt-6">
          <div className="flex items-center justify-between text-sm">
            <span className="text-slate-600">Pass Rate</span>
            <span className="font-medium text-slate-900">{passRate.toFixed(1)}%</span>
          </div>
          <div className="mt-2 h-2 bg-slate-100 rounded-full overflow-hidden">
            <div
              className={`h-full rounded-full transition-all ${
                passRate >= 80 ? 'bg-emerald-500' : passRate >= 60 ? 'bg-amber-500' : 'bg-red-500'
              }`}
              style={{ width: `${passRate}%` }}
            />
          </div>

          <div className="mt-4 grid grid-cols-3 gap-4">
            <div className="text-center">
              <p className="text-2xl font-bold text-slate-900">{metrics.total_tests}</p>
              <p className="text-xs text-slate-500">Tests</p>
            </div>
            <div className="text-center">
              <p className="text-2xl font-bold text-emerald-600">{metrics.passed_tests}</p>
              <p className="text-xs text-slate-500">Bestanden</p>
            </div>
            <div className="text-center">
              <p className="text-2xl font-bold text-red-600">{metrics.failed_tests}</p>
              <p className="text-xs text-slate-500">Fehlgeschlagen</p>
            </div>
          </div>

          <div className="mt-4 pt-4 border-t border-slate-100">
            <p className="text-xs text-slate-500">
              Durchschnittlicher Score: <span className="font-medium">{metrics.avg_composite_score.toFixed(2)}</span>
            </p>
          </div>
        </div>
      )}

      {lastRun && (
        <p className="mt-4 text-xs text-slate-400">Letzter Lauf: {new Date(lastRun).toLocaleString('de-DE')}</p>
      )}
    </div>
  )
}
