import React from 'react';

const auditEntries = [
  { time: '2026-05-07 10:15', action: 'role_changed', target: 'Markus Keller', actor: 'David Admin', change: 'mitarbeiter → tenant_admin', result: 'erfolgt', reason: 'Vertretung', tone: 'green' },
  { time: '2026-05-07 09:30', action: 'user_invited', target: 'max@example.test', actor: 'David Admin', change: '- → pending', result: 'erfolgt', reason: 'Einladung', tone: 'green' },
  { time: '2026-05-06 18:20', action: 'last_admin_protection', target: 'David Admin', actor: 'System', change: 'active → blocked', result: 'blockiert', reason: 'letzter Admin', tone: 'yellow' },
  { time: '2026-05-05 14:00', action: 'user_deactivated', target: 'Lisa Beispiel', actor: 'Markus Keller', change: 'active → inactive', result: 'erfolgt', reason: 'Austritt', tone: 'green' },
];

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>;
}

export default function BU08AuditHistorieBenutzerverwaltung() {
  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">UC08 · BU-08</p>
              <h1 className="mt-1 text-3xl font-bold">Audit-Historie Benutzerverwaltung</h1>
              <p className="mt-2 max-w-3xl text-sm text-slate-600">
                Unveränderbare Historie für Einladungen, Rollen, Statuswechsel und Sicherheitsereignisse.
              </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">Zurück</button>
              <button className="rounded-xl border border-slate-300 bg-white px-4 py-2 text-sm font-semibold text-slate-800">Export</button>
              <button className="rounded-xl bg-sky-700 px-4 py-2 text-sm font-semibold text-white">Filter</button>
            </div>
          </div>
        </header>

        <section className="rounded-2xl border border-slate-200 bg-white p-5 shadow-sm">
          <h2 className="text-lg font-bold">Audit-Status</h2>
          <div className="mt-4 flex flex-wrap gap-2">
            <Badge tone="green">Tenant-Isolation OK</Badge>
            <Badge tone="blue">32 Einträge</Badge>
            <Badge tone="yellow">3 blockierte Aktionen</Badge>
            <Badge tone="green">Read-only aktiv</Badge>
            <Badge tone="gray">Zeitraum: letzte 30 Tage</Badge>
          </div>
        </section>

        <section className="rounded-2xl border border-slate-200 bg-white p-5 shadow-sm">
          <h2 className="text-lg font-bold">Filter</h2>
          <div className="mt-4 grid gap-3 lg:grid-cols-6">
            {['letzte 30 Tage', 'Aktion', 'Zielbenutzer', 'ausgeführt durch', 'Ergebnis'].map((label) => (
              <div key={label} className="rounded-xl border border-slate-300 bg-slate-50 px-3 py-2 text-sm font-semibold text-slate-700">{label}</div>
            ))}
            <button className="rounded-xl bg-slate-900 px-3 py-2 text-sm font-semibold text-white">Filtern</button>
          </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">Audit-Einträge</h2>
            <p className="text-sm text-slate-500">Audit-Einträge sind unveränderbar und rein lesend.</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">Aktion</th>
                  <th className="px-5 py-3">Ziel</th>
                  <th className="px-5 py-3">Ausgeführt durch</th>
                  <th className="px-5 py-3">Alt → Neu</th>
                  <th className="px-5 py-3">Ergebnis</th>
                  <th className="px-5 py-3">Grund</th>
                </tr>
              </thead>
              <tbody className="divide-y divide-slate-100">
                {auditEntries.map((entry) => (
                  <tr key={`${entry.time}-${entry.action}`} className="hover:bg-slate-50">
                    <td className="px-5 py-4 text-slate-600">{entry.time}</td>
                    <td className="px-5 py-4 font-semibold">{entry.action}</td>
                    <td className="px-5 py-4 text-slate-600">{entry.target}</td>
                    <td className="px-5 py-4 text-slate-600">{entry.actor}</td>
                    <td className="px-5 py-4 text-slate-600">{entry.change}</td>
                    <td className="px-5 py-4"><Badge tone={entry.tone}>{entry.result}</Badge></td>
                    <td className="px-5 py-4 text-slate-600">{entry.reason}</td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        </section>

        <section className="rounded-2xl border border-slate-200 bg-white p-5 shadow-sm">
          <p className="text-sm text-slate-600">
            Hinweis: Passwörter, Tokens und 2FA-Secrets werden nicht angezeigt. Export ist in V1 nur vorbereitet.
          </p>
        </section>
      </div>
    </div>
  );
}
