import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { exposeService } from '../services/exposeService' import { aiService } from '../services/aiService' import type { ExposeDraft } from '../domain/expose' import type { Property } from '../domain/property' import type { ExposeTextInput } from '../services/ai/IAIService' import { STALE_EXPOSE_LEADS } from '../lib/constants' import { useToastStore } from '../stores/toastStore' export function useExposeDraft(leadId: string, property: Property | undefined) { return useQuery({ queryKey: ['expose-draft', leadId, property?.id], queryFn: async () => (await exposeService.getOrCreateDraft(leadId, property!)).data, enabled: !!leadId && !!property, staleTime: STALE_EXPOSE_LEADS, }) } export function useSaveExposeDraft() { const queryClient = useQueryClient() return useMutation({ mutationFn: async (draft: ExposeDraft) => (await exposeService.save(draft)).data, onSuccess: (draft) => { queryClient.setQueryData(['expose-draft', draft.leadId, draft.propertyId], draft) }, onError: () => { useToastStore.getState().showToast('Exposé konnte nicht gespeichert werden.', 'error') }, }) } export function useGenerateExpose() { const queryClient = useQueryClient() return useMutation({ mutationFn: async (draft: ExposeDraft) => (await exposeService.generate(draft)).data, onSuccess: (draft) => { queryClient.setQueryData(['expose-draft', draft.leadId, draft.propertyId], draft) useToastStore.getState().showToast('Exposé erstellt — bereit für den Export.', 'success') }, onError: (err: Error) => { useToastStore.getState().showToast(err.message || 'Exposé konnte nicht erstellt werden.', 'error') }, }) } /** * KI-Textentwurf für einen Abschnitt. * * Fehlen belegte Angaben, liefert der Dienst keinen Text, sondern die Liste der * Lücken — die Oberfläche sagt das dann so, statt einen Text zu zeigen, der * Angaben behauptet, die niemand geprüft hat (§8.5.2, §9.6). */ export function useGenerateExposeText() { return useMutation({ mutationFn: async (input: ExposeTextInput) => (await aiService.generateExposeText(input)).data, onError: () => { useToastStore.getState().showToast('Textentwurf konnte nicht erzeugt werden — bitte manuell erfassen.', 'error') }, }) }