import { api } from './api';

export interface AuditEvent {
  id: string;
  actorId?: string | null;
  actorName?: string | null;
  action: string;
  entityType: string;
  entityId: string;
  productId?: string | null;
  projectId?: string | null;
  versionId?: string | null;
  scope?: unknown;
  before?: unknown;
  after?: unknown;
  metadata?: unknown;
  createdAt: string;
}

export interface AuditQuery {
  actorId?: string;
  entityType?: string;
  entityId?: string;
  productId?: string;
  projectId?: string;
  versionId?: string;
  dateFrom?: string;
  dateTo?: string;
  take?: string;
}

export function buildAuditQueryPath(query: AuditQuery = {}) {
  const params = new URLSearchParams();
  for (const [key, value] of Object.entries(query)) {
    if (typeof value === 'string' && value.trim()) params.set(key, value.trim());
  }
  const search = params.toString();
  return search ? `/audit?${search}` : '/audit';
}

export function listAuditEvents(query: AuditQuery = {}) {
  return api.get<AuditEvent[]>(buildAuditQueryPath(query));
}
