import apiClient from "./apiClient";
export const getCaseCount = () => apiClient.get("/api/case/case/stats/");
// export const getCases = () => apiClient.get("/api/case/cases/");
export const getCases = (params = {}) =>
  apiClient.get("/api/case/cases/", { params });
export const getCaseById = (id) => apiClient.get(`/api/case/cases/${id}/`);
export const getLeadAttorneys = () => apiClient.get("/api/lead-attorneys/");
export const getLeadConsultants = () => apiClient.get("/api/lead-consultants/");

/**
 * Create a new case (supports file upload via FormData)
 * @param {FormData} formData - Case form data
 */
export const createCase = (formData) =>
  apiClient.post("/api/case/cases/", formData, {
    headers: { "Content-Type": "multipart/form-data" },
  });

/**
 * Edit an existing case (PATCH supports file upload via FormData)
 * @param {number|string} id - Case ID
 * @param {object} data - Key/value object of input fields
 */
export const editCaseById = (id, data) => {
  const formData = new FormData();

  Object.keys(data).forEach((key) => {
    if (data[key] !== null && data[key] !== undefined) {
      formData.append(key, data[key]);
    }
  });

  return apiClient.patch(`/api/case/cases/${id}/`, formData, {
    headers: { "Content-Type": "multipart/form-data" },
  });
};

export const getJurisdiction = () =>
  apiClient.get("/api/case/case/jurisdiction/");
export const getCourtSystem = () =>
  apiClient.get("/api/case/case/courtsystem/");

// export const completedCases = () => apiClient.get("/api/case/cases/completed/");
// export const priorityCases = () => apiClient.get("/api/case/cases/high-priority/");
// export const archivedCases = () => apiClient.get("/api/case/cases/archived/");
// export const completedCases = (params = {}) =>
//   apiClient.get("/api/case/cases/completed/", { params });

// export const priorityCases = (params = {}) =>
//   apiClient.get("/api/case/cases/high-priority/", { params });

// export const archivedCases = (params = {}) =>
//   apiClient.get("/api/case/cases/archived/", { params });

export const archiveCase = (id, payload) =>
  apiClient.patch(`/api/case/cases/${id}/`, payload);

export const assignJurorToCase = (caseId, payload) =>
  apiClient.post(`/api/case/cases/${caseId}/jurors/`, payload);

export const alreadyAssignedToCase = (caseId) =>
  apiClient.get(`/api/case/cases/${caseId}/jurors/`);

export const getAllJurorsToCase = (caseId) =>
  apiClient.get(`/api/case/cases/${caseId}/jurors/`);

export const getAllJurorsToCaseView = (caseId) =>
  apiClient.get(`/api/case/cases/${caseId}/assigned-jurors/`);

export const getAllUnassignJurorsToCase = (caseId) =>
  apiClient.get(`/api/case/cases/${caseId}/unassigned-jurors/`);

export const removeJurorFromCase = (caseId, jurorId) =>
  apiClient.delete(`/api/case/cases/${caseId}/jurors/${jurorId}/`);

export const getJurorById = (caseId, jurorId) =>
  apiClient.get(`/api/case/cases/${caseId}/jurors/${jurorId}/`);

export const getMaritalStatus = () =>
  apiClient.get("/api/juror/marital-status/");

export const getCaseAnalyst = (caseId) =>
  apiClient.get(`/api/cases/${caseId}/analysts/`);

export const getCaseAttorney = (caseId) =>
  apiClient.get(`/api/cases/${caseId}/attorneys/`);

export const getCaseAnalystSearch = (caseId, q = "") => {
  return apiClient.get(`/api/cases/${caseId}/analysts/`, {
    params: { q },
  });
};

export const getCaseAnalystAssignedList = (caseId, q = "") => {
  return apiClient.get(`/api/case/cases/${caseId}/analyst-summary/`, {
    params: { q },
  });
};

export const getCaseAttorneySearch = (caseId, q = "") => {
  return apiClient.get(`/api/cases/${caseId}/attorneys/`, {
    params: { q },
  });
};

export const assignCaseAnalyst = (payload) =>
  apiClient.post(`/api/assign-analysts-sync/`, payload);

export const assignCaseAttorney = (payload) =>
  apiClient.post(`/api/assign-attorneys-sync/`, payload);

export const getDocumentsCase = (caseId) =>
  apiClient.get(`/api/case/cases/${caseId}/documents/`);

export const uploadDocument = (caseId, formData) =>
  apiClient.post(`/api/case/cases/${caseId}/documents/`, formData, {
    headers: { "Content-Type": "multipart/form-data" },
  });

export const deleteDocument = (caseId, documentId) =>
  apiClient.delete(`/api/case/cases/${caseId}/documents/${documentId}/`);

export const caseAnalystList = (caseId) =>
  apiClient.get(`/api/case/cases/${caseId}/assigned-analysts`);

export const AssignJurorToAnalyst = (caseId, payload) =>
  apiClient.post(`/api/case/cases/${caseId}/analyst-jurors/`, payload);

export const UnassignJurorToAnalyst = (caseId, analystId, payload) =>
  apiClient.delete(
    `/api/case/cases/${caseId}/analyst-jurors/analysts/${analystId}/jurors/`,
    payload,
  );
