import React from 'react';

const timeline = [
  { time: '2026-05-03 08:15', event: 'vorbereitet', source: 'UC06', before: 'not_invited', after: 'draft', actor: 'tenant_admin', note: 'OK' },
  { time: '2026-05-03 08:17', event: 'übergeben', source: 'UC07', before: 'draft', after: 'queued', actor: 'system', note: 'Queue' },
  { time: '2026-05-03 08:18', event: 'versendet', source: 'UC07', before: 'queued', after: 'sent', actor: 'mail service', note: 'OK' },
  { time: '2026-05-21 00:00', event: 'Ablauf geplant', source: 'System', before: 'sent', after: 'expired', actor: 'system', note: 'falls nicht angenommen' },
];

function Badge({ children, tone = 'gray' }) {
  const tones = {
    green: 'bg-green-100 text-green-800 border-green-200',
    blue: 'bg-blue-100 text-blue-800 border-blue-200',
    yellow: 'bg-yellow-100 text-yellow-800 border-yellow-200',
    red: 'bg-red-100 text-red-800 border-red-200',
    gray: 'bg-slate-100 text-slate-700 border-slate-200',
  };
  return <span className={`inline-flex rounded-full border px-3 py-1 text-xs font-semibold ${tones[tone]}`}>{children}</span>;
}

function Card({ title, children }) {
  return (
    <section className="rounded-2xl border border-slate-200 bg-white p-5 shadow-sm">
      <h2 className="text-lg font-bold">{title}</h2>
      <div className="mt-4">{children}</div>
    </section>
  );
}

export default function KP04EinladungsstatusFehler() {
  return (
    <div className="min-h-screen bg-slate-50 p-6 text-slate-900">
      <div className="mx-auto max-w-7xl space-y-5">
        <header className="rounded-2xl border border-sky-200 bg-sky-50 p-6 shadow-sm">
          <div className="flex flex-col gap-4 lg:flex-row lg:items-center lg:justify-between">
            <div>
              <p className="text-sm font-semibold uppercase tracking-wide text-sky-700">UC06 · KP-04</p>
              <h1 className="mt-1 text-3xl font-bold">Einladungsstatus & Fehler</h1>
              <p className="mt-2 max-w-3xl text-sm text-slate-600">Status, Fehler, Ablauf, Widerruf und erneute Einladung prüfen.</p>
            </div>
            <div className="flex flex-wrap gap-2">
              <button className="rounded-xl border border-slate-300 bg-white px-4 py-2 text-sm font-semibold text-slate-800">KP-01</button>
              <button className="rounded-xl border border-slate-300 bg-white px-4 py-2 text-sm font-semibold text-slate-800">KP-03</button>
              <button className="rounded-xl bg-sky-700 px-4 py-2 text-sm font-semibold text-white">UC07</button>
            </div>
          </div>
        </header>

        <section className="rounded-2xl border border-slate-200 bg-white p-5 shadow-sm">
          <h2 className="text-lg font-bold">Statuskopf</h2>
          <div className="mt-4 flex flex-wrap gap-2">
            <Badge tone="gray">Sara Frei · K-2026-018</Badge>
            <Badge tone="blue">invited</Badge>
            <Badge tone="blue">sent</Badge>
            <Badge tone="yellow">läuft ab 2026-05-21</Badge>
            <Badge tone="green">Token geschützt</Badge>
            <Badge tone="gray">Versandstatus aus UC07</Badge>
          </div>
        </section>

        <section className="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm">
          <div className="border-b border-slate-200 px-5 py-4">
            <h2 className="text-lg font-bold">Statusverlauf</h2>
            <p className="text-sm text-slate-500">Statuswechsel, Benutzer/System und Hinweise werden nachvollziehbar geführt.</p>
          </div>
          <div className="overflow-x-auto">
            <table className="w-full min-w-[1120px] text-left text-sm">
              <thead className="bg-slate-100 text-xs uppercase tracking-wide text-slate-600">
                <tr>
                  <th className="px-5 py-3">Zeitpunkt</th>
                  <th className="px-5 py-3">Ereignis</th>
                  <th className="px-5 py-3">Quelle</th>
                  <th className="px-5 py-3">Status vorher</th>
                  <th className="px-5 py-3">Status danach</th>
                  <th className="px-5 py-3">Auslöser</th>
                  <th className="px-5 py-3">Hinweis</th>
                </tr>
              </thead>
              <tbody className="divide-y divide-slate-100">
                {timeline.map((row) => (
                  <tr key={`${row.time}-${row.event}`} className="hover:bg-slate-50">
                    <td className="px-5 py-4 text-slate-600">{row.time}</td>
                    <td className="px-5 py-4 font-semibold">{row.event}</td>
                    <td className="px-5 py-4 text-slate-600">{row.source}</td>
                    <td className="px-5 py-4 text-slate-600">{row.before}</td>
                    <td className="px-5 py-4 text-slate-600">{row.after}</td>
                    <td className="px-5 py-4 text-slate-600">{row.actor}</td>
                    <td className="px-5 py-4 text-slate-600">{row.note}</td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        </section>

        <div className="grid gap-5 lg:grid-cols-2">
          <Card title="Fehler & Hinweise">
            <div className="flex flex-wrap gap-2">
              <Badge tone="green">kein Fehler</Badge>
              <Badge tone="yellow">noch nicht angenommen</Badge>
              <Badge tone="green">vollständige Token ausgeblendet</Badge>
            </div>
          </Card>

          <Card title="Folgeaktionen">
            <div className="flex flex-wrap gap-2">
              <button className="rounded-xl border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700">erneut vorbereiten</button>
              <button className="rounded-xl border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700">widerrufen</button>
              <button className="rounded-xl bg-sky-700 px-4 py-2 text-sm font-semibold text-white">UC07 öffnen</button>
            </div>
            <p className="mt-4 text-sm text-slate-500">Widerruf, Sperre und erneute Einladung werden protokolliert.</p>
          </Card>
        </div>

        <section className="rounded-2xl border border-slate-200 bg-white p-5 shadow-sm">
          <p className="text-sm text-slate-600">KP-04 ist Status- und Fehleransicht. Neue Versandübergabe erfolgt über KP-03 / UC07.</p>
        </section>
      </div>
    </div>
  );
}
