// import { withHeaders } from "@/record-management/services/api/withHeaders";
import axios from "axios";
import { getEnvUrl } from "@/shared/config/env";

// types/collection-query.dto.ts
export interface CollectionQueryDTO {
  /** Columns to select */
  s?: string; // Example: "id,name,email"

  /** Where conditions */
  w?: string; // Example: "status:=:active|age:>:18"

  /** Limit (take) */
  t?: number;

  /** Offset (skip) */
  sk?: number;

  /** Order by */
  o?: string; // Example: "created_at:DESC,name:ASC"

  /** Include related entities */
  i?: string; // Example: "roles,profile"

  /** Group by */
  g?: string; // Example: "status,department"

  /** Having conditions (used with group by) */
  h?: string; // Example: "count:>:5"

  /** Return count only */
  c?: boolean;
}

export interface AuditLog {
  id: string;
  user: string;
  action: string;
  message: string;
  timestamp: string;
}

export interface CollectionResponse {
  items: AuditLog[];
  total?: number;
}



export const fetchAuditLogs = async (
  query: CollectionQueryDTO
): Promise<CollectionResponse> => {
  // Filter out empty or undefined fields
  const filteredEntries = Object.entries(query).filter(
    ([, value]) => value !== undefined && value !== null && value !== ""
  );

  // Convert to URLSearchParams
  const params = new URLSearchParams();
  for (const [key, value] of filteredEntries) {
    params.append(key, String(value));
  }
  const baseUrl = getEnvUrl("VITE_AUDIT_API_URL");
  const url = `${baseUrl}/audits/get-audits?${params.toString()}`;

  const response = await axios.get(url);
  const data = response.data;

  // Normalize response shape
  const items = Array.isArray(data) ? data : data.items || data.data || [];

  return {
    items,
    total: data.total || items.length,
  };
};
