import { fetchWithAuth } from "./client";

function buildQuery(params: Record<string, string | number | boolean | undefined>): string {
  const qs = Object.entries(params)
    .filter(([, v]) => v !== undefined)
    .map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`)
    .join("&");
  return qs ? `?${qs}` : "";
}

export const detectionApi = {
  listTasks: (token?: string, params?: { skip?: number; limit?: number; status?: string }) =>
    fetchWithAuth(`/api/v1/detection/tasks${buildQuery(params || {})}`, {}, token),

  createTask: (data: Record<string, unknown>, token?: string) =>
    fetchWithAuth("/api/v1/detection/tasks", { method: "POST", body: JSON.stringify(data) }, token),

  updateTask: (taskId: string, data: Record<string, unknown>, token?: string) =>
    fetchWithAuth(`/api/v1/detection/tasks/${taskId}`, { method: "PUT", body: JSON.stringify(data) }, token),

  deleteTask: (taskId: string, token?: string) =>
    fetchWithAuth(`/api/v1/detection/tasks/${taskId}`, { method: "DELETE" }, token),

  triggerTask: (taskId: string, token?: string) =>
    fetchWithAuth(`/api/v1/detection/tasks/${taskId}/trigger`, { method: "POST" }, token),
};
