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

export const routesService = {
    /** Get all routes with optional filtering */
    async getAllRoutes(filters?: { originCity?: string, destinationCity?: string }): Promise<Route[]> {
        const response = await apiClient.get<Route[]>('/api/routes', {
            params: {
                origin_city: filters?.originCity,
                destination_city: filters?.destinationCity
            }
        })
        return response.data
    },

    /** Get a single route by ID */
    async getRouteById(id: string): Promise<Route> {
        const response = await apiClient.get<Route>(`/api/routes/${id}`)
        return response.data
    },

    /** Get all stops for a route */
    async getRouteStops(routeId: string): Promise<BusStop[]> {
        const response = await apiClient.get<BusStop[]>(`/api/routes/${routeId}/stops`)
        return response.data
    },

    /** Create a new route (Admin) */
    async createRoute(data: import('@/types').RouteCreate): Promise<Route> {
        const response = await apiClient.post<Route>('/api/routes', data)
        return response.data
    },

    /** Update a route (Admin) */
    async updateRoute(id: string, data: import('@/types').RouteUpdate): Promise<Route> {
        const response = await apiClient.put<Route>(`/api/routes/${id}`, data)
        return response.data
    },

    /** Delete a route (Admin) */
    async deleteRoute(id: string): Promise<void> {
        await apiClient.delete(`/api/routes/${id}`)
    },

    /** Add a stop to a route (Admin) */
    async addStopToRoute(routeId: string, data: import('@/types').RouteStopCreate): Promise<void> {
        await apiClient.post(`/api/routes/${routeId}/stops`, data)
    },

    /** Update a stop on a route (Admin) - including reorder */
    async updateRouteStop(routeId: string, stopId: string, data: import('@/types').RouteStopUpdate): Promise<void> {
        await apiClient.put(`/api/routes/${routeId}/stops/${stopId}`, data)
    },

    /** Remove a stop from a route (Admin) */
    async removeStopFromRoute(routeId: string, stopId: string): Promise<void> {
        await apiClient.delete(`/api/routes/${routeId}/stops/${stopId}`)
    }
}

