import { CriteriaDTO } from '@erp/core';
import { useDataSource } from "@erp/core/hooks";
import { DefaultError, QueryKey, useQuery } from "@tanstack/react-query";
import { CustomerInvoicesPage } from '../schemas';

export const CUSTOMER_INVOICES_QUERY_KEY = (criteria?: CriteriaDTO): QueryKey =>
  ["customer_invoices", criteria] as const;

type InvoicesQueryOptions = {
  enabled?: boolean;
  criteria?: CriteriaDTO
};

// Obtener todas las facturas
export const useInvoicesQuery = (options?: InvoicesQueryOptions) => {
  const dataSource = useDataSource();
  const enabled = options?.enabled ?? true;
  const criteria = options?.criteria ?? {};

  return useQuery<CustomerInvoicesPage, DefaultError>({
    queryKey: CUSTOMER_INVOICES_QUERY_KEY(criteria),
    queryFn: async ({ signal }) => {
      return await dataSource.getList<CustomerInvoicesPage>("customer-invoices", {
        params: criteria,
        signal,
      });
    },
    enabled
  });
};
