import { api as apiClient } from '../auth/http';

import { URL_CONSTANTS } from '@/constants/URLS';
import type {
  GenerateInterchangeDocumentPayload,
  InterchangeDocument,
  InterchangeDocumentFilter,
} from '@/types/interchangeDocument';

const cleanParams = (params: object) =>
  Object.fromEntries(
    Object.entries(params).filter(([, value]) => value !== undefined && value !== '' && value !== null),
  );

export const interchangeDocumentsService = {
  list: (filter?: InterchangeDocumentFilter) =>
    apiClient.get<InterchangeDocument[]>(URL_CONSTANTS.INTERCHANGE_DOCUMENTS.BASE, {
      params: cleanParams(filter ?? {}),
    }),
  getById: (id: string) =>
    apiClient.get<InterchangeDocument>(URL_CONSTANTS.INTERCHANGE_DOCUMENTS.BY_ID(id)),
  generateFromSchedule: (payload: GenerateInterchangeDocumentPayload) =>
    apiClient.post<InterchangeDocument>(
      URL_CONSTANTS.INTERCHANGE_DOCUMENTS.GENERATE_FROM_SCHEDULE,
      payload,
    ),
  acknowledge: (id: string, payload: { acknowledgedBy: string; remarks?: string }) =>
    apiClient.patch<InterchangeDocument>(URL_CONSTANTS.INTERCHANGE_DOCUMENTS.ACKNOWLEDGE(id), payload),
  dispute: (id: string, payload: { remarks: string }) =>
    apiClient.patch<InterchangeDocument>(URL_CONSTANTS.INTERCHANGE_DOCUMENTS.DISPUTE(id), payload),
};
