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

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