import AdminShell from "@/components/admin/AdminShell";
import { requireAdminPermission } from "@/lib/auth";
import { prisma } from "@/lib/db";

export const revalidate = 0;

const actionLabels: Record<string, string> = {
  create: "Oluşturuldu",
  update: "Güncellendi",
  archive: "Arşivlendi",
  toggle: "Görünürlük değişti",
  backfill: "İlk aktarım",
};

export default async function AdminActivityPage() {
  await requireAdminPermission("activity", "view");
  const logs = await prisma.adminAuditLog.findMany({ orderBy: { createdAt: "desc" }, take: 200 });

  return (
    <AdminShell eyebrow="Denetlenebilir içerik" title="İşlem geçmişi" description="Panel üzerinden yapılan son 200 içerik işlemi burada tutulur. Bu kayıt değişikliğin ne zaman ve hangi içerik türünde yapıldığını izlemek içindir.">
      <div className="overflow-hidden rounded-3xl border border-border bg-surface-1/55">
        <div className="overflow-x-auto">
          <table className="w-full min-w-[720px] text-left text-sm">
            <thead className="border-b border-border text-[10px] uppercase tracking-[0.15em] text-fg-muted"><tr><th className="px-5 py-4 font-medium">Zaman</th><th className="px-5 py-4 font-medium">Kullanıcı</th><th className="px-5 py-4 font-medium">İşlem</th><th className="px-5 py-4 font-medium">İçerik</th><th className="px-5 py-4 font-medium">Açıklama</th></tr></thead>
            <tbody className="divide-y divide-border">
              {logs.map((log) => (
                <tr key={log.id} className="text-fg-secondary">
                  <td className="whitespace-nowrap px-5 py-4 text-xs">{new Intl.DateTimeFormat("tr-TR", { dateStyle: "medium", timeStyle: "short", timeZone: "Europe/Istanbul" }).format(log.createdAt)}</td>
                  <td className="px-5 py-4 text-xs text-fg-primary">{log.actorUsername ? `@${log.actorUsername}` : "Sistem"}</td>
                  <td className="px-5 py-4"><span className="rounded-full border border-border px-2.5 py-1 text-[9px] uppercase tracking-wider text-ember">{actionLabels[log.action] || log.action}</span></td>
                  <td className="px-5 py-4 text-xs uppercase tracking-wider text-fg-muted">{log.entity}</td>
                  <td className="px-5 py-4 text-fg-primary">{log.summary}</td>
                </tr>
              ))}
              {!logs.length ? <tr><td colSpan={5} className="px-5 py-14 text-center text-fg-muted">Henüz kayıtlı işlem yok.</td></tr> : null}
            </tbody>
          </table>
        </div>
      </div>
    </AdminShell>
  );
}
