'use client';

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { paymentsApi } from '@/lib/api';

export function useSupplementaryCharges(filters: { bookingRef?: string; status?: string }) {
  return useQuery({
    queryKey: ['supplementary-charges', filters],
    queryFn: () => paymentsApi.supplementary.getAll(filters),
  });
}

export function useCreateSupplementaryCharge(onSuccess: () => void) {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: { bookingRef: string; amountMinor: number; reason: string; notes?: string }) =>
      paymentsApi.supplementary.create(data),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['supplementary-charges'] });
      onSuccess();
    },
  });
}

export function useMarkSupplementaryPaid() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ id, providerTxnId }: { id: string; providerTxnId?: string }) =>
      paymentsApi.supplementary.markPaid(id, providerTxnId),
    onSuccess: () => qc.invalidateQueries({ queryKey: ['supplementary-charges'] }),
  });
}

export function useWaiveSupplementaryCharge() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ id, notes }: { id: string; notes?: string }) =>
      paymentsApi.supplementary.waive(id, notes),
    onSuccess: () => qc.invalidateQueries({ queryKey: ['supplementary-charges'] }),
  });
}

export function useResendSupplementaryLink() {
  return useMutation({
    mutationFn: (id: string) => paymentsApi.supplementary.resend(id),
  });
}
