/** Service for bus stop-related API calls */ import { supabase } from '@/supabase' import type { BusStop, Route } from '@/types' export const busStopsService = { /** Get all bus stops */ async getAllBusStops(): Promise { const { data, error } = await supabase.from('bus_stops').select('*') if (error) throw new Error(error.message) return data as BusStop[] }, /** Get a single bus stop by ID */ async getBusStopById(id: string): Promise { const { data, error } = await supabase.from('bus_stops').select('*').eq('id', id).single() if (error) throw new Error(error.message) return data as BusStop }, /** Get all routes passing through a bus stop */ async getBusStopRoutes(stopId: string): Promise { const { data, error } = await supabase .from('route_stops') .select('routes(*)') .eq('stop_id', stopId) if (error) throw new Error(error.message) // Extract the nested strictly typed route object automatically connected by Supabase relationships return (data || []).map((row: any) => row.routes) as Route[] }, /** Get estimated next bus arrivals (Mock Data) */ async getNextBusArrivals(_stopId: string): Promise<{ routeName: string; arrivalTime: string }[]> { // Mock delay to simulate network request await new Promise(resolve => setTimeout(resolve, 500)); // Generate some random mock arrivals const mockArrivals = [ { routeName: "Ruta Boquete - David", arrivalTime: "5 min" }, { routeName: "Ruta David - Boquete", arrivalTime: "12 min" }, { routeName: "Ruta Circular", arrivalTime: "25 min" } ]; // Randomly return 1-3 arrivals return mockArrivals.slice(0, Math.floor(Math.random() * 3) + 1); }, /** Create a new bus stop (Admin) */ async createBusStop(currentData: import('@/types').BusStopCreate): Promise { const { data, error } = await supabase .from('bus_stops') .insert([currentData]) .select() .single() if (error) throw new Error(error.message) return data as BusStop }, /** Update a bus stop (Admin) */ async updateBusStop(id: string, currentData: import('@/types').BusStopUpdate): Promise { const { data, error } = await supabase .from('bus_stops') .update(currentData) .eq('id', id) .select() .single() if (error) throw new Error(error.message) return data as BusStop }, /** Delete a bus stop (Admin) */ async deleteBusStop(id: string): Promise { const { error } = await supabase.from('bus_stops').delete().eq('id', id) if (error) throw new Error(error.message) } }