import { inject, resource } from '@angular/core';
import {
  FetchRemissionReturnReceipts,
  RemissionReturnReceiptService,
} from '@isa/remission/data-access';
import { subDays } from 'date-fns';

/**
 * Resource for fetching completed remission return receipts.
 * It retrieves receipts that are marked as completed within a specified date range.
 * @param {Function} params - Function that returns parameters for fetching receipts
 * @param {Object} params.returncompleted - Boolean indicating if the return is completed
 * @param {Date} params.start - Start date for filtering receipts
 * @return {Resource} Angular resource that manages the completed receipts data
 */
export const completedRemissionReturnsResource = (
  params: () => FetchRemissionReturnReceipts = () => ({
    returncompleted: true,
    start: subDays(new Date(), 7),
  }),
) => {
  const remissionReturnReceiptService = inject(RemissionReturnReceiptService);
  return resource({
    params,
    loader: ({ abortSignal, params }) => {
      return remissionReturnReceiptService.fetchRemissionReturnReceipts(
        params,
        abortSignal,
      );
    },
  });
};

/**
 * Resource for fetching incomplete remission return receipts.
 * It retrieves receipts that are not marked as completed.
 * @param {Function} params - Function that returns parameters for fetching receipts
 * @param {Object} params.returncompleted - Boolean indicating if the return is completed
 * @return {Resource} Angular resource that manages the incomplete receipts data
 */
export const incompletedRemissionReturnsResource = (
  params: () => FetchRemissionReturnReceipts = () => ({
    returncompleted: false,
  }),
) => {
  const remissionReturnReceiptService = inject(RemissionReturnReceiptService);
  return resource({
    params,
    loader: ({ abortSignal, params }) => {
      return remissionReturnReceiptService.fetchRemissionReturnReceipts(
        params,
        abortSignal,
      );
    },
  });
};
