import { queryOptions } from "@tanstack/react-query";
import { jsonQuery } from "../apiAdapter";

export const auditLogOptions = (filters: {
  search?: string;
  action?: string;
  entityType?: string;
  dateFrom?: string;
  dateTo?: string;
  page?: number;
}) =>
  queryOptions({
    queryKey: ["audit-log", filters],
    queryFn: () => {
      const params = new URLSearchParams();
      if (filters.search) params.set("search", filters.search);
      if (filters.action) params.set("action", filters.action);
      if (filters.entityType) params.set("entity_type", filters.entityType);
      if (filters.dateFrom) params.set("date_from", filters.dateFrom);
      if (filters.dateTo) params.set("date_to", filters.dateTo);
      if (filters.page) params.set("page", String(filters.page));
      const qs = params.toString();
      return jsonQuery<{
        data: Record<string, unknown>[];
        pagination: Record<string, unknown>;
      }>(`/api/admin/audit-log${qs ? `?${qs}` : ""}`);
    },
  });
