export interface TrackedSearch { id: string name: string search_params: Record route_summary: string interval_hours: number is_active: boolean next_run_at: string | null last_run_at: string | null run_count: number last_error: string | null expires_at: string | null created_at: string latest_snapshot: PriceSnapshot | null } export interface PriceSnapshot { cheapest_price: number avg_price: number | null median_price: number | null total_results: number recorded_at: string } export interface TopTrip { price: number currency: string bookingToken: string legs: Array<{ from: string; to: string; departure: string; airlines: string[] }> } export interface SearchRun { id: string tracked_search_id: string status: string cheapest_price: number | null total_trips_found: number top_trips: TopTrip[] | null from_cache: boolean error_message: string | null started_at: string | null completed_at: string | null created_at: string } export function useTrackedSearches() { const user = useSupabaseUser() const trackedSearches = ref([]) const loading = ref(false) async function fetchAll() { if (!user.value) return loading.value = true try { const data = await $fetch('/api/tracking') trackedSearches.value = data || [] } catch { trackedSearches.value = [] } finally { loading.value = false } } async function create(params: { name: string searchParams: Record routeSummary: string intervalHours?: number expiresAt?: string }) { const data = await $fetch('/api/tracking', { method: 'POST', body: params }) await fetchAll() return data } async function update(id: string, patch: Partial>) { const data = await $fetch(`/api/tracking/${id}`, { method: 'PATCH', body: patch }) const idx = trackedSearches.value.findIndex(s => s.id === id) if (idx >= 0) { trackedSearches.value[idx] = { ...trackedSearches.value[idx], ...data } } return data } async function remove(id: string) { await $fetch(`/api/tracking/${id}`, { method: 'DELETE' }) trackedSearches.value = trackedSearches.value.filter(s => s.id !== id) } async function getHistory(id: string, days = 30): Promise { return $fetch(`/api/tracking/${id}/history`, { query: { days } }) } async function getRuns(id: string, limit = 20): Promise { return $fetch(`/api/tracking/${id}/runs`, { query: { limit } }) } watch(user, (u) => { if (u) fetchAll() else trackedSearches.value = [] }, { immediate: true }) return { trackedSearches, loading, fetchAll, create, update, remove, getHistory, getRuns } }