/**
 * React Query Key Factory
 * 일관된 쿼리 키 관리를 위한 팩토리 함수들
 *
 * 공통코드 키 구조 (2026-05-15 마스터-디테일 재작성 기준):
 *   codeInfo   — 그룹 마스터
 *   codeDetail — 트리 노드
 */

type FilterShape = { active?: boolean; search?: string };

export const queryKeys = {
  // code_info (그룹 마스터)
  codeInfo: {
    all: ["code_info"] as const,
    lists: () => [...queryKeys.codeInfo.all, "list"] as const,
    list: (filters?: FilterShape) => [...queryKeys.codeInfo.lists(), filters] as const,
    infiniteList: (filters?: FilterShape) =>
      [...queryKeys.codeInfo.all, "infiniteList", filters] as const,
    details: () => [...queryKeys.codeInfo.all, "detail"] as const,
    detail: (codeInfo: string) => [...queryKeys.codeInfo.details(), codeInfo] as const,
  },

  // code_detail (디테일 트리 노드)
  codeDetail: {
    all: ["code_detail"] as const,
    trees: () => [...queryKeys.codeDetail.all, "tree"] as const,
    tree: (codeInfo: string, filters?: FilterShape) =>
      [...queryKeys.codeDetail.trees(), codeInfo, filters] as const,
    details: () => [...queryKeys.codeDetail.all, "detail"] as const,
    detail: (codeDetailId: number | string) =>
      [...queryKeys.codeDetail.details(), codeDetailId] as const,
  },

  // 옵션 (화면관리 등 연계용)
  options: {
    all: ["options"] as const,
    byCodeInfo: (codeInfo: string) => [...queryKeys.options.all, codeInfo] as const,
  },

  // 중복 검사
  validation: {
    all: ["validation"] as const,
    codeInfoDuplicate: (field: string, value: string, excludeCode?: string) =>
      [...queryKeys.validation.all, "code_info", field, value, excludeCode] as const,
    codeDetailDuplicate: (
      codeInfo: string,
      field: string,
      value: string,
      excludeId?: number | string,
    ) =>
      [...queryKeys.validation.all, "code_detail", codeInfo, field, value, excludeId] as const,
  },
} as const;
