'use client'

import { useState } from 'react'
import { Certificate } from '@/lib/sdk/academy/types'
import { downloadCertificatePDF } from '@/lib/sdk/academy/api'

interface CertificateViewerProps {
  certificate: Certificate
  onClose?: () => void
}

export default function CertificateViewer({ certificate, onClose }: CertificateViewerProps) {
  const [downloading, setDownloading] = useState(false)
  const [error, setError] = useState<string | null>(null)

  const handleDownloadPDF = async () => {
    setDownloading(true)
    setError(null)
    try {
      const blob = await downloadCertificatePDF(certificate.id)
      const url = window.URL.createObjectURL(blob)
      const a = document.createElement('a')
      a.href = url
      a.download = `zertifikat-${certificate.id.slice(0, 8)}.pdf`
      document.body.appendChild(a)
      a.click()
      document.body.removeChild(a)
      window.URL.revokeObjectURL(url)
    } catch (err) {
      setError(err instanceof Error ? err.message : 'PDF-Download fehlgeschlagen')
    } finally {
      setDownloading(false)
    }
  }

  const issuedDate = new Date(certificate.issuedAt).toLocaleDateString('de-DE', {
    day: '2-digit', month: '2-digit', year: 'numeric'
  })
  const validDate = new Date(certificate.validUntil).toLocaleDateString('de-DE', {
    day: '2-digit', month: '2-digit', year: 'numeric'
  })
  const isExpired = new Date(certificate.validUntil) < new Date()

  return (
    <div className="bg-white border border-gray-200 rounded-xl shadow-lg overflow-hidden">
      {/* Certificate Preview */}
      <div className="relative bg-gradient-to-br from-indigo-50 via-white to-purple-50 p-8">
        {/* Decorative border */}
        <div className="absolute inset-4 border-2 border-indigo-200 rounded-lg pointer-events-none" />
        <div className="absolute inset-5 border border-indigo-100 rounded-lg pointer-events-none" />

        <div className="relative text-center space-y-4">
          {/* Company */}
          <p className="text-sm text-gray-400 tracking-widest uppercase">BreakPilot Compliance</p>

          {/* Title */}
          <h2 className="text-2xl font-bold text-gray-900 tracking-wide">SCHULUNGSZERTIFIKAT</h2>

          {/* Decorative line */}
          <div className="mx-auto w-24 h-0.5 bg-indigo-500" />

          {/* Body */}
          <p className="text-sm text-gray-500">Hiermit wird bescheinigt, dass</p>

          <p className="text-xl font-bold text-gray-900">{certificate.userName}</p>

          <p className="text-sm text-gray-500">die folgende Compliance-Schulung erfolgreich abgeschlossen hat:</p>

          <p className="text-lg font-semibold text-indigo-600">{certificate.courseName}</p>

          {/* Score */}
          {certificate.score > 0 && (
            <p className="text-sm text-gray-500">
              Testergebnis: <span className="font-semibold text-gray-700">{certificate.score}%</span>
            </p>
          )}

          {/* Dates */}
          <div className="flex justify-between items-center px-8 pt-4 text-xs text-gray-400">
            <span>Abschlussdatum: {issuedDate}</span>
            <span className={isExpired ? 'text-red-500 font-medium' : ''}>
              Gueltig bis: {validDate}
              {isExpired && ' (abgelaufen)'}
            </span>
          </div>

          {/* Certificate ID */}
          <p className="text-xs text-gray-300">
            Zertifikats-Nr.: {certificate.id.slice(0, 12)}
          </p>
        </div>
      </div>

      {/* Actions */}
      <div className="px-6 py-4 bg-gray-50 border-t border-gray-200 flex items-center justify-between">
        <div className="text-xs text-gray-400">
          Elektronisch erstellt - ohne Unterschrift gueltig
        </div>
        <div className="flex gap-3">
          {onClose && (
            <button
              onClick={onClose}
              className="px-4 py-2 text-sm text-gray-600 hover:text-gray-800 transition-colors"
            >
              Schliessen
            </button>
          )}
          <button
            onClick={handleDownloadPDF}
            disabled={downloading}
            className="px-4 py-2 text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 disabled:bg-indigo-400 rounded-lg transition-colors flex items-center gap-2"
          >
            {downloading ? (
              <>
                <svg className="animate-spin h-4 w-4" viewBox="0 0 24 24">
                  <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" fill="none" />
                  <path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z" />
                </svg>
                Wird erstellt...
              </>
            ) : (
              <>
                <svg className="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 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>
                PDF herunterladen
              </>
            )}
          </button>
        </div>
      </div>

      {/* Error */}
      {error && (
        <div className="px-6 py-3 bg-red-50 border-t border-red-200 text-sm text-red-600">
          {error}
        </div>
      )}
    </div>
  )
}
