const API_BASE = '/api'; async function request(path, options = {}) { const token = localStorage.getItem('delamain_token'); const headers = { 'Content-Type': 'application/json' }; if (token) headers['Authorization'] = `Bearer ${token}`; const resp = await fetch(`${API_BASE}${path}`, { ...options, headers, }); if (!resp.ok && resp.status !== 400 && resp.status !== 403 && resp.status !== 404) { const body = await resp.json().catch(() => ({})); throw new Error(body.error || `Request failed: ${resp.status}`); } return resp.json(); } const api = { request, login: (username, password) => request('/login', { method: 'POST', body: JSON.stringify({ username, password }) }), createUser: (username, password) => request('/users', { method: 'POST', body: JSON.stringify({ username, password }) }), createTrip: (tripData) => request('/trips', { method: 'POST', body: JSON.stringify(tripData) }), getTrip: (tripId) => request(`/trips/${tripId}`), getAllTrips: () => request('/trips'), getVersions: (tripId) => request(`/trips/${tripId}/versions`), createPackage: (tripId) => request('/packages', { method: 'POST', body: JSON.stringify({ trip_id: tripId }) }), estimate: (fromX, fromY, toX, toY, cls) => request('/estimate', { method: 'POST', body: JSON.stringify({ from_x: fromX, from_y: fromY, to_x: toX, to_y: toY, class: cls }), }), bookRide: (payload) => request('/trips', { method: 'POST', body: JSON.stringify(payload) }), };