import { apiClient } from '@/lib/api-client';
import type { AuditLogFilters, AuditLogsResponse } from './types';

export function buildAuditLogSearchParams(filters: AuditLogFilters) {
  const searchParams = new URLSearchParams();

  if (filters.page) searchParams.set('page', String(filters.page));
  if (filters.limit) searchParams.set('limit', String(filters.limit));
  if (filters.search) searchParams.set('search', filters.search);
  if (filters.user) searchParams.set('user', filters.user);
  if (filters.module) searchParams.set('module', filters.module);
  if (filters.action) searchParams.set('action', filters.action);
  if (filters.dateFrom) searchParams.set('dateFrom', filters.dateFrom);
  if (filters.dateTo) searchParams.set('dateTo', filters.dateTo);
  if (filters.sort) searchParams.set('sort', filters.sort);

  return searchParams;
}

export async function getAuditLogs(filters: AuditLogFilters): Promise<AuditLogsResponse> {
  const searchParams = buildAuditLogSearchParams(filters);
  return apiClient<AuditLogsResponse>(`/audit-logs?${searchParams.toString()}`);
}
