import type { ApiEnvelope, DailyMileageRow, DashboardSummary, HistoryLocationRow, MileageSummary, OpsHealth, Page, QualitySummary, QualityIssueRow, RawFrameRow, RealtimeLocationRow, VehicleRealtimeRow, VehicleCoverageRow, VehicleDetail, VehicleRow } from './types'; export type RawFrameQuery = { keyword?: string; vin?: string; protocol?: string; dateFrom?: string; dateTo?: string; fields?: string[]; includeFields?: boolean; limit?: number; offset?: number; }; async function request(path: string, init?: RequestInit): Promise { const response = await fetch(path, init); if (!response.ok) { throw new Error(`request failed ${response.status}`); } const envelope = (await response.json()) as ApiEnvelope; return envelope.data; } export const api = { dashboardSummary: () => request('/api/dashboard/summary'), vehicles: (params = new URLSearchParams()) => request>(`/api/vehicles?${params.toString()}`), vehicleCoverage: (params = new URLSearchParams()) => request>(`/api/vehicles/coverage?${params.toString()}`), vehicleDetail: (params = new URLSearchParams()) => request(`/api/vehicles/detail?${params.toString()}`), vehicleRealtime: (params = new URLSearchParams()) => request>(`/api/realtime/vehicles?${params.toString()}`), realtimeLocations: (params = new URLSearchParams()) => request>(`/api/realtime/locations?${params.toString()}`), historyLocations: (params = new URLSearchParams()) => request>(`/api/history/locations?${params.toString()}`), rawFrames: (params = new URLSearchParams()) => request>(`/api/history/raw-frames?${params.toString()}`), rawFramesQuery: (query: RawFrameQuery) => request>('/api/history/raw-frames/query', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(query) }), mileageSummary: (params = new URLSearchParams()) => request(`/api/mileage/summary?${params.toString()}`), dailyMileage: (params = new URLSearchParams()) => request>(`/api/mileage/daily?${params.toString()}`), qualitySummary: (params = new URLSearchParams()) => request(`/api/quality/summary?${params.toString()}`), qualityIssues: (params = new URLSearchParams()) => request>(`/api/quality/issues?${params.toString()}`), opsHealth: () => request('/api/ops/health') };