import { useQuery } from "@tanstack/react-query";
import { checkCodeDetailDuplicate, checkCodeInfoDuplicate } from "@/lib/api/commonCode";
import { queryKeys } from "@/lib/queryKeys";

/**
 * 그룹(code_info) 중복 검사
 */
export function useCheckCodeInfoDuplicate(
  field: "code_info" | "code_name" | "code_name_eng",
  value: string,
  excludeCode?: string,
  enabled = true,
) {
  return useQuery({
    queryKey: queryKeys.validation.codeInfoDuplicate(field, value, excludeCode),
    queryFn: () => checkCodeInfoDuplicate(field, value, excludeCode),
    enabled: enabled && !!value && value.trim().length > 0,
    staleTime: 0,
    retry: false,
    select: (data) => data.data,
    meta: { errorPolicy: "silent" },
  });
}

/**
 * 디테일(code_detail) 중복 검사
 */
export function useCheckCodeDetailDuplicate(
  codeInfo: string,
  field: "code_value" | "code_name" | "code_name_eng",
  value: string,
  excludeId?: number | string,
  enabled = true,
) {
  return useQuery({
    queryKey: queryKeys.validation.codeDetailDuplicate(codeInfo, field, value, excludeId),
    queryFn: () => checkCodeDetailDuplicate(codeInfo, field, value, excludeId),
    enabled: enabled && !!codeInfo && !!value && value.trim().length > 0,
    staleTime: 0,
    retry: false,
    select: (data) => data.data,
    meta: { errorPolicy: "silent" },
  });
}
