import type { PageParams } from '@/lib/api/types';
import type { EarningsState, PayoutBatchFilters } from './types';

/**
 * React Query key factory for the payouts domain (hierarchical, per the `services/{domain}` pattern).
 *
 * The **state filter and page are part of the key** so switching the earnings tab or paging never refetches
 * data already in cache (reverting to a viewed tab is a cache hit); the summary, each earnings tab, each
 * history page, and each payout detail all key independently.
 */
export const payoutKeys = {
  all: ['payouts'] as const,

  earningsSummary: () => [...payoutKeys.all, 'earnings_summary'] as const,

  earningsLists: () => [...payoutKeys.all, 'earnings'] as const,
  /** `state` is `'all'` for the unfiltered tab, else the `EarningsState`; `page` keeps pages separate. */
  earningsList: (state: EarningsState | 'all', page: number) =>
    [...payoutKeys.earningsLists(), state, page] as const,

  historyLists: () => [...payoutKeys.all, 'history'] as const,
  history: (page: number) => [...payoutKeys.historyLists(), page] as const,

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

  // Admin batch actions. The filter object + page params are part of the key (so each filter/page caches
  // separately); mutations invalidate the `adminBatchLists` / `adminBatchDetails` prefixes.
  adminBatchLists: () => [...payoutKeys.all, 'admin_batches'] as const,
  adminBatches: (filters: PayoutBatchFilters, params: PageParams) =>
    [...payoutKeys.adminBatchLists(), filters, params] as const,

  adminBatchDetails: () => [...payoutKeys.all, 'admin_batch_detail'] as const,
  adminBatchDetail: (batchId: number, page: number) =>
    [...payoutKeys.adminBatchDetails(), batchId, page] as const,
};
