import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' import { reminderService } from '../services/reminderService' import type { Reminder } from '../domain/reminder' import { useToastStore } from '../stores/toastStore' export function useReminders() { return useQuery({ queryKey: ['reminders'], queryFn: reminderService.getAll, }) } export function useReminder(id: string) { return useQuery({ queryKey: ['reminder', id], queryFn: () => reminderService.getById(id), enabled: !!id, }) } export function useReminderInsights() { return useQuery({ queryKey: ['reminder-insights'], queryFn: reminderService.getInsights, }) } export function useCompleteReminder() { const queryClient = useQueryClient() return useMutation({ mutationFn: ({ id, note }: { id: string; note?: string }) => reminderService.complete(id, note), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['reminders'] }) queryClient.invalidateQueries({ queryKey: ['reminder-insights'] }) }, onError: () => { useToastStore.getState().showToast('Erinnerung konnte nicht abgeschlossen werden.', 'error') }, }) } export function useDismissReminder() { const queryClient = useQueryClient() return useMutation({ mutationFn: ({ id, note }: { id: string; note?: string }) => reminderService.dismiss(id, note), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['reminders'] }) queryClient.invalidateQueries({ queryKey: ['reminder-insights'] }) }, onError: () => { useToastStore.getState().showToast('Erinnerung konnte nicht verworfen werden.', 'error') }, }) } export function useSnoozeReminder() { const queryClient = useQueryClient() return useMutation({ mutationFn: ({ id, until }: { id: string; until: string }) => reminderService.snooze(id, until), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['reminders'] }) queryClient.invalidateQueries({ queryKey: ['reminder-insights'] }) }, onError: () => { useToastStore.getState().showToast('Erinnerung konnte nicht verschoben werden.', 'error') }, }) } export function useCreateReminder() { const queryClient = useQueryClient() return useMutation({ mutationFn: (data: Omit) => reminderService.create(data), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['reminders'] }) queryClient.invalidateQueries({ queryKey: ['reminder-insights'] }) }, onError: () => { useToastStore.getState().showToast('Reminder konnte nicht erstellt werden.', 'error') }, }) } export function useUpdateReminder() { const queryClient = useQueryClient() return useMutation({ mutationFn: ({ id, data }: { id: string; data: Partial }) => reminderService.update(id, data), onSuccess: (_result, { id }) => { queryClient.invalidateQueries({ queryKey: ['reminders'] }) queryClient.invalidateQueries({ queryKey: ['reminder', id] }) }, onError: () => { useToastStore.getState().showToast('Erinnerung konnte nicht aktualisiert werden.', 'error') }, }) }