import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { sourceService } from '../services/sourceService'
import type { SourceFilters, SourceStatus, TermsStatus } from '../domain/dataSource'

const STALE_SOURCES = 30_000

export function useDataSources(filters?: SourceFilters) {
  return useQuery({
    queryKey: ['data-sources', filters ?? {}],
    queryFn: () => sourceService.getSources(filters),
    staleTime: STALE_SOURCES,
    select: (res) => res.data ?? [],
  })
}

export function useDataSource(id: string | null) {
  return useQuery({
    queryKey: ['data-source', id],
    queryFn: () => sourceService.getSource(id!),
    enabled: id !== null,
    staleTime: STALE_SOURCES,
    select: (res) => res.data ?? null,
  })
}

export function useConnectorRuns(sourceId: string | null) {
  return useQuery({
    queryKey: ['connector-runs', sourceId],
    queryFn: () => sourceService.getConnectorRuns(sourceId!),
    enabled: sourceId !== null,
    staleTime: STALE_SOURCES,
    select: (res) => res.data ?? [],
  })
}

export function useTriggerMockRun() {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: (sourceId: string) => sourceService.triggerMockRun(sourceId),
    onSuccess: (_data, sourceId) => {
      queryClient.invalidateQueries({ queryKey: ['data-sources'] })
      queryClient.invalidateQueries({ queryKey: ['data-source', sourceId] })
      queryClient.invalidateQueries({ queryKey: ['connector-runs', sourceId] })
    },
  })
}

export function useUpdateSourceStatus() {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: ({ id, status }: { id: string; status: SourceStatus }) =>
      sourceService.updateSourceStatus(id, status),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['data-sources'] })
      queryClient.invalidateQueries({ queryKey: ['data-source'] })
    },
  })
}

export function useMarkTermsStatus() {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: ({ id, termsStatus }: { id: string; termsStatus: TermsStatus }) =>
      sourceService.markTermsStatus(id, termsStatus),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['data-sources'] })
      queryClient.invalidateQueries({ queryKey: ['data-source'] })
    },
  })
}
