/** Service for shuttle-related API calls (Intercity/Tourism) */
import { supabase } from '@/supabase'
import type { Shuttle } from '@/types'

export interface ShuttleFilters {
    origin?: string
    destination?: string
    company_name?: string
    trip_type?: string
    is_active?: boolean
}

export const shuttlesService = {
    /** Get all shuttles with optional filters */
    async getAllShuttles(filters?: ShuttleFilters): Promise<Shuttle[]> {
        let query = supabase.from('shuttles').select('*')

        if (filters?.origin) query = query.eq('origin', filters.origin)
        if (filters?.destination) query = query.eq('destination', filters.destination)
        if (filters?.company_name) query = query.eq('company_name', filters.company_name)
        if (filters?.trip_type) query = query.eq('trip_type', filters.trip_type)
        if (filters?.is_active !== undefined) query = query.eq('is_active', filters.is_active)

        const { data, error } = await query
        if (error) throw new Error(error.message)
        return data as Shuttle[]
    },

    /** Get a single shuttle by ID */
    async getShuttleById(id: string): Promise<Shuttle> {
        const { data, error } = await supabase.from('shuttles').select('*').eq('id', id).single()
        if (error) throw new Error(error.message)
        return data as Shuttle
    },
}
