import { api } from './api'; import type { FahrzeugBuchungListItem, FahrzeugBuchung, Fahrzeug, CreateBuchungInput, MaintenanceWindow, } from '../types/booking.types'; // --------------------------------------------------------------------------- // Response shapes from the backend // --------------------------------------------------------------------------- interface ApiResponse { success: boolean; data: T; } // --------------------------------------------------------------------------- // Booking API service // --------------------------------------------------------------------------- export const bookingApi = { // ------------------------------------------------------------------------- // Calendar / listing // ------------------------------------------------------------------------- getCalendarRange( from: Date, to: Date, fahrzeugId?: string ): Promise<{ bookings: FahrzeugBuchungListItem[]; maintenanceWindows: MaintenanceWindow[] }> { return api .get>( '/api/bookings/calendar', { params: { from: from.toISOString(), to: to.toISOString(), ...(fahrzeugId ? { fahrzeugId } : {}), }, } ) .then((r) => r.data.data); }, getUpcoming(limit = 20): Promise { return api .get>('/api/bookings/upcoming', { params: { limit }, }) .then((r) => r.data.data); }, // ------------------------------------------------------------------------- // Availability check // ------------------------------------------------------------------------- checkAvailability( fahrzeugId: string, from: Date, to: Date ): Promise<{ available: boolean; reason?: string; ausserDienstVon?: string; ausserDienstBis?: string }> { return api .get>('/api/bookings/availability', { params: { fahrzeugId, from: from.toISOString(), to: to.toISOString(), }, }) .then((r) => r.data.data); }, // ------------------------------------------------------------------------- // Single booking // ------------------------------------------------------------------------- getById(id: string): Promise { return api .get>(`/api/bookings/${id}`) .then((r) => r.data.data); }, // ------------------------------------------------------------------------- // CRUD // ------------------------------------------------------------------------- create(data: CreateBuchungInput): Promise { return api .post>('/api/bookings', data) .then((r) => r.data.data); }, update(id: string, data: Partial): Promise { return api .patch>(`/api/bookings/${id}`, data) .then((r) => r.data.data); }, cancel(id: string, abgesagt_grund: string): Promise { return api .patch(`/api/bookings/${id}/cancel`, { abgesagt_grund }) .then(() => undefined); }, // ------------------------------------------------------------------------- // iCal // ------------------------------------------------------------------------- getCalendarToken(): Promise<{ token: string; subscribeUrl: string }> { return api .get>( '/api/bookings/calendar-token' ) .then((r) => r.data.data); }, }; // --------------------------------------------------------------------------- // Vehicle helper (shared with booking page) // --------------------------------------------------------------------------- export function fetchVehicles(): Promise { return api .get>('/api/vehicles') .then((r) => r.data.data); }