import { useMutation, useQueryClient } from '@tanstack/react-query';
import { ticketsApi } from '../apis';
import { ticketKeys } from '../keys';

/**
 * Close an open ticket (REQ-063 — gated behind `TICKET_LIFECYCLE_ENABLED`, the caller's job). Invalidates
 * the admin thread + every admin queue page so the ticket leaves the open worklist immediately.
 */
export function useCloseTicket(ticketId: number) {
  const queryClient = useQueryClient();
  return useMutation<void, unknown, void>({
    mutationFn: () => ticketsApi.closeTicket(ticketId),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ticketKeys.adminDetail(ticketId) });
      queryClient.invalidateQueries({ queryKey: ticketKeys.adminLists() });
    },
  });
}
