import { useQuery } from '@tanstack/react-query'
import { library, type DuplicateGroupsResponse } from '../services/api'

export const DUPLICATE_GROUPS_QUERY_KEY = ['library', 'duplicates'] as const

/**
 * Duplicate groups computed by the backend regroup_duplicates task. The
 * cache is shared between DuplicatesView and the Settings panel stats so
 * the user only ever pays for one fetch per refresh.
 */
export function useDuplicateGroupsQuery() {
  return useQuery<DuplicateGroupsResponse>({
    queryKey: DUPLICATE_GROUPS_QUERY_KEY,
    queryFn: () => library.duplicates.groups(),
    staleTime: 30_000,
  })
}
