import { useQuery } from '@tanstack/react-query';
import { ticketsApi } from '../apis';
import { ticketKeys } from '../keys';
import { TICKETS_GC_TIME, TICKET_THREAD_STALE_TIME } from '../constants';
import { useTicketViewer } from './useTicketViewer';

/**
 * The full admin ticket thread (b15 `GET /admin/tickets/{id}`) — header + participants + messages,
 * **internal notes INCLUDED**. A single cached `adminDetail(id)` entry (the contract returns the whole
 * thread in one call). The viewer id drives which bubbles are "mine"; `useTicketViewer` yields the admin
 * "me" under the admin console (real path uses the authenticated id, mock falls back to the admin id), and
 * the mock defaults to an admin viewer if none is passed. `usePostAdminMessage` mutates this same entry.
 */
export function useAdminTicket(ticketId: number | null) {
  const { userId } = useTicketViewer();
  return useQuery({
    queryKey: ticketKeys.adminDetail(ticketId ?? -1),
    queryFn: () => ticketsApi.getAdminTicket(ticketId as number, userId),
    enabled: ticketId != null && ticketId > 0,
    staleTime: TICKET_THREAD_STALE_TIME,
    gcTime: TICKETS_GC_TIME,
  });
}
