import {
  CreatePurchaseRequestPayload,
  Purchase,
  UpdatePurchaseRequestPayload,
  CreateStaffApprovalRequestPayload,
  UpdateStaffApprovalRequestPayload,
  CreateManagerApprovalRequestPayload,
  CreateAcceptApprovalRequestPayload,
  DeletePurchaseRequestItemPayload,
} from '@/types/api/purchase/purchase';
import { BaseApiService } from '@/services/api/base';
import { BaseApiResponse } from '@/types/api/api-general';

const basePurchaseApi = new BaseApiService<
  Purchase,
  CreatePurchaseRequestPayload,
  UpdatePurchaseRequestPayload
>('/purchases');

export const PurchaseApi = {
  basePath: basePurchaseApi.basePath,
  header: basePurchaseApi.header,
  getAllFetcher: basePurchaseApi.getAllFetcher.bind(basePurchaseApi),
  getSingle: basePurchaseApi.getSingle.bind(basePurchaseApi),
  create: basePurchaseApi.create.bind(basePurchaseApi),
  update: basePurchaseApi.update.bind(basePurchaseApi),
  delete: basePurchaseApi.delete.bind(basePurchaseApi),
  customRequest: basePurchaseApi.customRequest.bind(basePurchaseApi),

  staffApproval: {
    create: async (
      purchaseRequestId: number,
      payload: CreateStaffApprovalRequestPayload
    ): Promise<BaseApiResponse<{ message: string }> | undefined> => {
      return await basePurchaseApi.customRequest<
        BaseApiResponse<{ message: string }>
      >(`${purchaseRequestId}/approvals/staff`, {
        method: 'POST',
        payload,
      });
    },

    update: async (
      purchaseRequestId: number,
      payload: UpdateStaffApprovalRequestPayload
    ): Promise<BaseApiResponse<{ message: string }> | undefined> => {
      return await basePurchaseApi.customRequest<
        BaseApiResponse<{ message: string }>
      >(`${purchaseRequestId}/approvals/staff`, {
        method: 'POST',
        payload,
      });
    },
  },

  managerApproval: {
    create: async (
      purchaseRequestId: number,
      payload: CreateManagerApprovalRequestPayload
    ): Promise<BaseApiResponse<{ message: string }> | undefined> => {
      return await basePurchaseApi.customRequest<
        BaseApiResponse<{ message: string }>
      >(`${purchaseRequestId}/approvals/manager`, {
        method: 'POST',
        payload,
      });
    },
  },

  acceptApproval: {
    create: async (
      purchaseRequestId: number,
      payload: CreateAcceptApprovalRequestPayload
    ): Promise<BaseApiResponse<{ message: string }> | undefined> => {
      const formData = new FormData();

      formData.append('action', payload.action);

      if (payload.notes) {
        formData.append('notes', payload.notes);
      }

      if (payload.items) {
        formData.append('items', JSON.stringify(payload.items));
      }

      if (payload.travel_documents) {
        payload.travel_documents.forEach((file) => {
          formData.append('travel_documents', file);
        });
      }

      return await basePurchaseApi.customRequest<
        BaseApiResponse<{ message: string }>
      >(`${purchaseRequestId}/receipts`, {
        method: 'POST',
        payload: formData as unknown as Record<string, unknown>,
      });
    },
  },

  items: {
    delete: async (
      purchaseRequestId: number,
      payload: DeletePurchaseRequestItemPayload
    ): Promise<BaseApiResponse<{ message: string }> | undefined> => {
      return await basePurchaseApi.customRequest<
        BaseApiResponse<{ message: string }>
      >(`${purchaseRequestId}/items`, {
        method: 'DELETE',
        payload,
      });
    },
  },
};
