import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import * as api from '../api/addons';

export function useMyAddons(contribuyenteId?: string) {
  return useQuery({
    queryKey: ['my-addons', contribuyenteId ?? 'all'],
    queryFn: () => api.listMyAddons(contribuyenteId),
  });
}

export function useSubscribeAddon() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: api.subscribeAddon,
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['my-addons'] });
    },
  });
}

export function useCancelAddon() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: api.cancelAddon,
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['my-addons'] });
    },
  });
}
