import { apiGet } from "@/app/lib/clientApi"; export interface AuditEvent { id: number; companyId: number; /** Server-local ISO datetime, e.g. "2026-11-30T15:52:00.5". */ occurredAt: string; actor: string; module: string | null; action: string; httpMethod: string; pathTemplate: string; targetId: string | null; outcome: "SUCCESS" | "FAILURE"; httpStatus: number; clientIp: string | null; /** JSON string: {"": {before, after}} or {"fields": [...]}; null when nothing was recorded. */ changes: string | null; } export interface AuditLogParams { actor?: string; module?: string; action?: string; outcome?: string; /** "yyyy-MM-dd". */ from?: string; to?: string; /** 1-based. */ page: number; pageSize: number; companyId?: number; } const BASE = "/api/audit-log"; export async function searchAuditLog(params: AuditLogParams): Promise<{ items: AuditEvent[]; total: number }> { const qs = new URLSearchParams(); for (const key of ["actor", "module", "action", "outcome", "from", "to"] as const) if (params[key]) qs.set(key, params[key]); if (params.companyId) qs.set("companyId", String(params.companyId)); qs.set("page", String(Math.max(0, params.page - 1))); qs.set("size", String(params.pageSize)); const raw = await apiGet<{ content?: AuditEvent[]; totalElements?: number } | null>(`${BASE}?${qs}`); const items = Array.isArray(raw?.content) ? raw.content : []; return { items, total: typeof raw?.totalElements === "number" ? raw.totalElements : items.length }; } export const getAuditEvent = (id: string) => apiGet(`${BASE}/${encodeURIComponent(id)}`);