import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import api from '@/services/api'

export interface FilterValues {
  jahr?: number
  fallgruppe?: string
  has_icd?: string
  search?: string
}

export interface FilterPreset {
  id: number
  name: string
  filters: FilterValues
  created_at: string
}

export function useFilterPresets() {
  return useQuery({
    queryKey: ['filter-presets'],
    queryFn: () => api.get<FilterPreset[]>('/filter-presets/').then(r => r.data),
  })
}

export function useCreateFilterPreset() {
  const qc = useQueryClient()
  return useMutation({
    mutationFn: (data: { name: string; filters: FilterValues }) =>
      api.post<FilterPreset>('/filter-presets/', data).then(r => r.data),
    onSuccess: () => qc.invalidateQueries({ queryKey: ['filter-presets'] }),
  })
}

export function useDeleteFilterPreset() {
  const qc = useQueryClient()
  return useMutation({
    mutationFn: (id: number) => api.delete(`/filter-presets/${id}`),
    onSuccess: () => qc.invalidateQueries({ queryKey: ['filter-presets'] }),
  })
}
