import type { PageParams } from '@/lib/api/types';
import type { AdminTicketFilters, TicketListParams } from './types';

/**
 * React Query key factory for the tickets domain (hierarchical, per the `services/{domain}` pattern).
 *
 * The **filter object keys the list** so paging/filtering caches independently. There is a single
 * **`detail(id)`** for a ticket: the b15 contract returns the whole thread (header + participants +
 * messages) in one `GET /tickets/{id}` call — there is **no server pagination of messages** — so the
 * thread is not a separate cache entry; `useTicketThread` is a `select` over `detail(id)` (mirroring the f8
 * `useBookingSessions = select over detail` precedent). `usePostMessage` optimistically mutates `detail(id)`
 * and invalidates `lists()`/`detail(id)` on settle.
 */
export const ticketKeys = {
  all: ['tickets'] as const,

  lists: () => [...ticketKeys.all, 'list'] as const,
  list: (params: TicketListParams) => [...ticketKeys.lists(), params] as const,

  details: () => [...ticketKeys.all, 'detail'] as const,
  detail: (ticketId: number) => [...ticketKeys.details(), ticketId] as const,

  // Admin lens — a separate subtree so the internal-carrying admin caches never collide with the user
  // caches above (and invalidating one never touches the other). Filters + page key the global queue.
  adminLists: () => [...ticketKeys.all, 'admin', 'list'] as const,
  adminList: (filters: AdminTicketFilters, params: PageParams) =>
    [...ticketKeys.adminLists(), filters, params] as const,

  adminDetails: () => [...ticketKeys.all, 'admin', 'detail'] as const,
  adminDetail: (ticketId: number) => [...ticketKeys.adminDetails(), ticketId] as const,
};
