/**
 * Audit log hooks.
 */

import { useQuery } from '@tanstack/react-query';
import { apiGet } from './client';
import { PaginatedResponse } from './types';

export interface AuditLogEntry {
  id: string;
  timestamp: string;
  user: string;
  action: string;
  entity: string;
  entity_id?: string;
  details?: string;
}

export function useAuditLog(page = 1, pageSize = 25, filters?: { user?: string; action?: string; entity?: string; dateFrom?: string; dateTo?: string }) {
  const params = new URLSearchParams({ page: String(page), page_size: String(pageSize) });
  if (filters?.user) params.set('user', filters.user);
  if (filters?.action) params.set('action', filters.action);
  if (filters?.entity) params.set('entity', filters.entity);
  if (filters?.dateFrom) params.set('date_from', filters.dateFrom);
  if (filters?.dateTo) params.set('date_to', filters.dateTo);
  return useQuery({
    queryKey: ['auditLog', page, pageSize, filters],
    queryFn: () =>
      apiGet<PaginatedResponse<AuditLogEntry>>(`/audit-log?${params.toString()}`),
    retry: false,
  });
}
