import { redirect } from "next/navigation";

import { getSession } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { ShellPage } from "@/components/page-templates";
import { TablePlaceholder } from "@/components/placeholders";

function toTime(value: Date) {
  return new Intl.DateTimeFormat("id-ID", {
    day: "2-digit",
    month: "short",
    year: "numeric",
    hour: "2-digit",
    minute: "2-digit"
  }).format(value);
}

export default async function TenantAuditLogPage() {
  const session = await getSession();
  if (!session) {
    redirect("/login");
  }

  const audits = await prisma.auditLog.findMany({
    where: { tenantId: session.tenantId },
    include: { actorUser: true },
    orderBy: { createdAt: "desc" }
  });

  return (
    <ShellPage shell="admin" title="Audit Log" description="User activity, message action, dan campaign action logs.">
      <TablePlaceholder
        title="Audit events"
        columns={["Time", "Actor", "Module", "Action", "Entity"]}
        rows={audits.map((audit) => [
          toTime(audit.createdAt),
          audit.actorUser?.fullName ?? "System",
          audit.entityType,
          audit.action,
          audit.entityId
        ])}
      />
    </ShellPage>
  );
}
