/**
 * Ключи TanStack Query в одном месте. Инвалидация работает по префиксу, поэтому ключи строятся
 * иерархически: `qk.channels.detail(id)` начинается с `qk.channels.all`, и сброс списка каналов
 * заодно сбрасывает все их подветки. Раньше ключи были строковыми литералами по всему коду —
 * опечатка в одном месте тихо ломала инвалидацию в другом.
 */
export const qk = {
  auth: {
    registration: ['auth', 'registration'] as const,
  },

  air: {
    channels: ['air', 'channels'] as const,
    features: ['air', 'features'] as const,
    epg: (slug: string | null) => ['air', 'epg', slug] as const,
  },

  channels: {
    all: ['admin', 'channels'] as const,
    detail: (id: string) => ['admin', 'channels', id] as const,
    template: (id: string) => ['admin', 'channels', id, 'template'] as const,
    schedule: (id: string) => ['admin', 'channels', id, 'schedule'] as const,
    scheduleDay: (id: string, from: string) => ['admin', 'channels', id, 'schedule', from] as const,
    issues: (id: string) => ['admin', 'channels', id, 'issues'] as const,
    diff: (id: string) => ['admin', 'channels', id, 'diff'] as const,
    preview: (id: string, days: number) => ['admin', 'channels', id, 'preview', days] as const,
    /** План автосборки — свой на каждую комбинацию профиля и режима. */
    gridPlan: (id: string, profile: string, mode: string) =>
      ['admin', 'channels', id, 'grid-plan', profile, mode] as const,
  },

  junctions: {
    all: ['admin', 'junctions'] as const,
  },

  bumpers: {
    all: ['admin', 'bumpers'] as const,
  },

  gridProfiles: {
    all: ['admin', 'grid-profiles'] as const,
  },

  entries: {
    trace: (id: string) => ['admin', 'entries', id, 'trace'] as const,
  },

  shows: {
    all: ['admin', 'shows'] as const,
    byGenre: (genreId: string | null) => ['admin', 'shows', { genreId }] as const,
    detail: (id: string) => ['admin', 'shows', id] as const,
    /** Где шоу задействовано: группы, коллекции, каналы. */
    usage: (id: string) => ['admin', 'shows', id, 'usage'] as const,
  },

  groups: {
    all: ['admin', 'groups'] as const,
    detail: (id: string) => ['admin', 'groups', id] as const,
    suggestions: ['admin', 'groups', 'suggestions'] as const,
    /** Подходящее по сохранённому правилу, но ещё не добавленное — проверяется при открытии группы. */
    pending: (id: string) => ['admin', 'groups', id, 'pending'] as const,
  },

  collections: {
    all: ['admin', 'collections'] as const,
    detail: (id: string) => ['admin', 'collections', id] as const,
    suggestions: ['admin', 'collections', 'suggestions'] as const,
  },

  genres: {
    all: ['admin', 'genres'] as const,
  },

  interstitials: {
    all: ['admin', 'interstitials'] as const,
    blocks: ['admin', 'interstitials', 'blocks'] as const,
  },

  images: {
    byCategory: (category: string) => ['admin', 'images', category] as const,
  },

  media: {
    all: ['admin', 'media'] as const,
    stats: ['admin', 'media', 'stats'] as const,
    manual: ['admin', 'media', 'manual'] as const,
    ready: ['admin', 'media', 'ready', 'all'] as const,
    list: (filter: string, page: number, sortKey: string, sortDesc: boolean) =>
      ['admin', 'media', filter, page, sortKey, sortDesc] as const,
  },

  users: {
    all: ['admin', 'users'] as const,
    list: (
      page: number,
      search: string,
      roleId: string | null,
      sortKey: string,
      sortDesc: boolean,
    ) => ['admin', 'users', page, search, roleId, sortKey, sortDesc] as const,
  },

  roles: {
    all: ['admin', 'roles'] as const,
  },

  settings: {
    all: ['admin', 'settings'] as const,
  },

  storage: {
    all: ['admin', 'storage'] as const,
  },

  metadata: {
    providers: ['admin', 'metadata', 'providers'] as const,
  },
}
