import { useDataSource, useQueryKey } from "@erp/core/hooks";
import { IListCustomerInvoicesResponseDTO } from "@erp/customerInvoices/common/dto";

export type UseCustomerInvoicesListParams = Omit<IGetListDataProviderParams, "filters" | "resource"> & {
  status?: string;
  enabled?: boolean;
  queryOptions?: Record<string, unknown>;
};

export type UseCustomerInvoicesListResponse = UseListQueryResult<
  IListResponseDTO<IListCustomerInvoicesResponseDTO>,
  unknown
>;

export type UseCustomerInvoicesGetParamsType = {
  enabled?: boolean;
  queryOptions?: Record<string, unknown>;
};

export type UseCustomerInvoicesReportParamsType = {
  enabled?: boolean;
  queryOptions?: Record<string, unknown>;
};

export const useCustomerInvoices = () => {
  const actions = {
    /**
     * Hook para obtener la lista de facturas
     * @param params - Parámetros para la consulta de la lista de facturas
     * @returns - Respuesta de la consulta de la lista de facturas
     */
    useList: (params: UseCustomerInvoicesListParams): UseCustomerInvoicesListResponse => {
      const dataSource = useDataSource();
      const keys = useQueryKey();

      const {
        pagination,
        status = "draft",
        quickSearchTerm = undefined,
        enabled = true,
        queryOptions,
      } = params;

      return useList({
        queryKey: keys().data().resource("customerInvoices").action("list").params(params).get(),
        queryFn: () => {
          return dataSource.getList({
            resource: "customerInvoices",
            quickSearchTerm,
            filters:
              status !== "all"
                ? [
                    {
                      field: "status",
                      operator: "eq",
                      value: status,
                    },
                  ]
                : [
                    {
                      field: "status",
                      operator: "ne",
                      value: "archived",
                    },
                  ],
            pagination,
          });
        },
        enabled,
        queryOptions,
      });
    },
  };
  return actions;
};
