import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import * as conciliacionApi from '@/lib/api/conciliacion';
import { useContribuyenteStore } from '@/stores/contribuyente-store';

export function useCfdisConConciliacion(params: {
  tipo: string;
  fechaInicio?: string;
  fechaFin?: string;
  regimen?: string;
}) {
  const { selectedContribuyenteId } = useContribuyenteStore();

  return useQuery({
    queryKey: ['conciliacion', params, selectedContribuyenteId],
    queryFn: () => conciliacionApi.getCfdisConConciliacion({
      ...params,
      contribuyenteId: selectedContribuyenteId || undefined,
    }),
    enabled: !!params.tipo,
  });
}

export function useConciliar() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: conciliacionApi.conciliar,
    onSuccess: () => qc.invalidateQueries({ queryKey: ['conciliacion'] }),
  });
}

export function useDesconciliar() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: conciliacionApi.desconciliar,
    onSuccess: () => qc.invalidateQueries({ queryKey: ['conciliacion'] }),
  });
}
