import { AuditTrailBoard } from '../../../../components/audit-trail-board';
import { DashboardShell } from '../../../../components/dashboard-shell';
import { requireAuthToken } from '../../../../lib/auth';
import { fetchAuditTrail } from '../../../../lib/api';

export default async function AuditTrailPage() {
  const token = await requireAuthToken();
  const auditTrail = await fetchAuditTrail(token, { page: 1, limit: 50 });
  const normalizedEntries = auditTrail.items.map((entry) => ({
    id: entry.id,
    timestamp: entry.createdAt,
    adminUser: entry.actorName,
    actionType: entry.actionType,
    module: entry.module,
    ipAddress: entry.ipAddress || '-',
    severity: entry.severity,
    details: entry.details,
  }));

  return (
    <DashboardShell currentPath="/dashboard/settings/audit-trail">
      <AuditTrailBoard
        initialEntries={normalizedEntries}
        initialTotal={auditTrail.total}
        initialPage={auditTrail.page}
        initialPageSize={auditTrail.pageSize}
        initialTotalPages={auditTrail.totalPages}
      />
    </DashboardShell>
  );
}
