/** Service for bus stop-related API calls */
import { apiClient } from './apiClient'
import type { BusStop, Route } from '@/types'

export const busStopsService = {
    /** Get all bus stops */
    async getAllBusStops(): Promise<BusStop[]> {
        const response = await apiClient.get<BusStop[]>('/api/bus-stops')
        return response.data
    },

    /** Get a single bus stop by ID */
    async getBusStopById(id: string): Promise<BusStop> {
        const response = await apiClient.get<BusStop>(`/api/bus-stops/${id}`)
        return response.data
    },

    /** Get all routes passing through a bus stop */
    async getBusStopRoutes(stopId: string): Promise<Route[]> {
        const response = await apiClient.get<Route[]>(`/api/bus-stops/${stopId}/routes`)
        return response.data
    },

    /** 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(data: import('@/types').BusStopCreate): Promise<BusStop> {
        const response = await apiClient.post<BusStop>('/api/bus-stops', data)
        return response.data
    },

    /** Update a bus stop (Admin) */
    async updateBusStop(id: string, data: import('@/types').BusStopUpdate): Promise<BusStop> {
        const response = await apiClient.put<BusStop>(`/api/bus-stops/${id}`, data)
        return response.data
    },

    /** Delete a bus stop (Admin) */
    async deleteBusStop(id: string): Promise<void> {
        await apiClient.delete(`/api/bus-stops/${id}`)
    }
}

