import { apiGet, apiPost } from './apiClient';

export type AuditEvent = {
  id: string;
  timestamp: string;
  type: string;
  userId: string | null;
  userName: string | null;
  details: any;
  source?: string;
};

export function logAuditEvent(type: string, payload: Record<string, any> = {}) {
  return apiPost('/audit/event', {
    type,
    ...payload
  });
}

export function getAuditEvents(params: Record<string, any> = {}) {
  const query = new URLSearchParams();
  Object.entries(params).forEach(([key, value]) => {
    if (value === undefined || value === null || value === '') return;
    query.set(key, String(value));
  });
  const suffix = query.toString() ? `?${query}` : '';
  return apiGet(`/audit/events${suffix}`) as Promise<AuditEvent[]>;
}

export function getAuditSummary(date?: string) {
  const suffix = date ? `?date=${encodeURIComponent(date)}` : '';
  return apiGet(`/audit/summary${suffix}`) as Promise<Record<string, number>>;
}
