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

/**
 * Assign a ticket to a staff owner (REQ-063 — gated behind `TICKET_LIFECYCLE_ENABLED`, the caller's job;
 * today the only caller is "assign to me"). Invalidates the admin thread + queue on success.
 */
export function useAssignTicket(ticketId: number) {
  const queryClient = useQueryClient();
  return useMutation<void, unknown, { ownerUserId: number }>({
    mutationFn: ({ ownerUserId }) => ticketsApi.assignTicket(ticketId, ownerUserId),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ticketKeys.adminDetail(ticketId) });
      queryClient.invalidateQueries({ queryKey: ticketKeys.adminLists() });
    },
  });
}
