import { useQuery } from '@tanstack/react-query'
import { useArrInstances, type ArrInstance } from '../stores/arr-instances-store'
import { radarrClient } from '../api/radarr'
import { sonarrClient } from '../api/sonarr'

const STALE = 5 * 60 * 1000

/**
 * React Query bindings for the Sonarr / Radarr clients. Hooks return
 * `null` when no instance is configured for the requested kind+tier so
 * callers can render seerr-style "configure to enable" affordances.
 */

export function useArrSystemStatus(instance: ArrInstance | null | undefined) {
  return useQuery({
    queryKey: ['arr', 'status', instance?.id],
    queryFn: () =>
      instance
        ? instance.kind === 'radarr'
          ? radarrClient(instance).systemStatus()
          : sonarrClient(instance).systemStatus()
        : Promise.resolve(null),
    enabled: !!instance,
    staleTime: STALE,
    retry: 0,
  })
}

export function useRadarrLibrary(tier: 'default' | '4k' = 'default') {
  const instance = useArrInstances(s => s.pick('radarr', tier))
  return useQuery({
    queryKey: ['radarr', 'library', instance?.id],
    queryFn: () => (instance ? radarrClient(instance).movies() : Promise.resolve(null)),
    enabled: !!instance,
    staleTime: STALE,
  })
}

export function useSonarrLibrary(tier: 'default' | '4k' = 'default') {
  const instance = useArrInstances(s => s.pick('sonarr', tier))
  return useQuery({
    queryKey: ['sonarr', 'library', instance?.id],
    queryFn: () => (instance ? sonarrClient(instance).series() : Promise.resolve(null)),
    enabled: !!instance,
    staleTime: STALE,
  })
}

export function useRadarrQueue(tier: 'default' | '4k' = 'default') {
  const instance = useArrInstances(s => s.pick('radarr', tier))
  return useQuery({
    queryKey: ['radarr', 'queue', instance?.id],
    queryFn: () => (instance ? radarrClient(instance).queue() : Promise.resolve(null)),
    enabled: !!instance,
    staleTime: 30 * 1000,
    refetchInterval: 60 * 1000,
  })
}

export function useSonarrQueue(tier: 'default' | '4k' = 'default') {
  const instance = useArrInstances(s => s.pick('sonarr', tier))
  return useQuery({
    queryKey: ['sonarr', 'queue', instance?.id],
    queryFn: () => (instance ? sonarrClient(instance).queue() : Promise.resolve(null)),
    enabled: !!instance,
    staleTime: 30 * 1000,
    refetchInterval: 60 * 1000,
  })
}

export function useArrProfiles(instance: ArrInstance | null | undefined) {
  return useQuery({
    queryKey: ['arr', 'profiles', instance?.id],
    queryFn: () => {
      if (!instance) return Promise.resolve(null)
      if (instance.kind === 'radarr') return radarrClient(instance).qualityProfiles()
      return sonarrClient(instance).qualityProfiles()
    },
    enabled: !!instance,
    staleTime: 60 * 60 * 1000,
  })
}

export function useArrRootFolders(instance: ArrInstance | null | undefined) {
  return useQuery({
    queryKey: ['arr', 'rootfolders', instance?.id],
    queryFn: () => {
      if (!instance) return Promise.resolve(null)
      if (instance.kind === 'radarr') return radarrClient(instance).rootFolders()
      return sonarrClient(instance).rootFolders()
    },
    enabled: !!instance,
    staleTime: 60 * 60 * 1000,
  })
}

export function useSonarrLanguageProfiles(instance: ArrInstance | null | undefined) {
  return useQuery({
    queryKey: ['sonarr', 'language-profiles', instance?.id],
    queryFn: () => (instance && instance.kind === 'sonarr' ? sonarrClient(instance).languageProfiles() : Promise.resolve(null)),
    enabled: !!instance && instance.kind === 'sonarr',
    staleTime: 60 * 60 * 1000,
  })
}
