import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { pipelineService } from '../services/pipelineService'
import { usePipelineStore } from '../stores/pipelineStore'
import { useToastStore } from '../stores/toastStore'
import type { PipelineStage, PendingPipelineItem } from '../domain/pipeline'

export const PIPELINE_QUERY_KEY = ['pipeline', 'items'] as const

export function usePipelineItems() {
  return useQuery({
    queryKey: PIPELINE_QUERY_KEY,
    queryFn: () => pipelineService.getAll(),
    select: (res) => res.data ?? [],
    staleTime: Infinity,
  })
}

export function useAddToPipeline() {
  const queryClient = useQueryClient()
  const { closeSavedDialog } = usePipelineStore()

  return useMutation({
    mutationFn: ({ pending, notes }: { pending: PendingPipelineItem; notes?: string }) =>
      pipelineService.confirmSaved(pending, notes),
    onSuccess: (res) => {
      queryClient.invalidateQueries({ queryKey: PIPELINE_QUERY_KEY })
      closeSavedDialog()
      if (res.data === 'duplicate') {
        useToastStore.getState().showToast('Bereits in der Pipeline vorhanden.', 'warning')
      } else {
        useToastStore.getState().showToast('Zur Pipeline (Gemerkt) hinzugefügt.')
      }
    },
    onError: () => {
      useToastStore.getState().showToast('Fehler beim Hinzufügen zur Pipeline.', 'error')
    },
  })
}

export function useMoveStage() {
  const queryClient = useQueryClient()

  return useMutation({
    mutationFn: ({ id, stage }: { id: string; stage: PipelineStage }) =>
      pipelineService.moveStage(id, stage),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: PIPELINE_QUERY_KEY })
    },
    onError: () => {
      useToastStore.getState().showToast('Fehler beim Verschieben.', 'error')
    },
  })
}

export function useUpdateNotes() {
  const queryClient = useQueryClient()

  return useMutation({
    mutationFn: ({ id, notes }: { id: string; notes: string }) =>
      pipelineService.updateNotes(id, notes),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: PIPELINE_QUERY_KEY })
    },
  })
}

export function useLoseItem() {
  const queryClient = useQueryClient()

  return useMutation({
    mutationFn: (id: string) => pipelineService.loseItem(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: PIPELINE_QUERY_KEY })
    },
    onError: () => {
      useToastStore.getState().showToast('Fehler beim Ablehnen.', 'error')
    },
  })
}

export function useLinkInquiry() {
  const queryClient = useQueryClient()

  return useMutation({
    mutationFn: ({ id, inquiryId }: { id: string; inquiryId: string }) =>
      pipelineService.linkInquiry(id, inquiryId),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: PIPELINE_QUERY_KEY })
    },
  })
}
