/** Service for taxi-related API calls */
import { supabase } from '@/supabase'
import type { Taxi } from '@/types'

export interface TaxiFilters {
    corregimiento?: string
    shifts?: string[] // Soporte para múltiples turnos
    english_speaking?: boolean
    is_accessible?: boolean // Filtro de accesibilidad
    is_active?: boolean
}

export const taxisService = {
    /** Get all taxis with optional filters */
    async getAllTaxis(filters?: TaxiFilters): Promise<Taxi[]> {
        let query = supabase.from('taxis').select('*')

        if (filters?.corregimiento) query = query.eq('corregimiento', filters.corregimiento)
        if (filters?.shifts && filters.shifts.length > 0) {
            query = query.overlaps('shifts', filters.shifts) // Busca si hay solapamiento de turnos
        }
        if (filters?.is_accessible !== undefined) query = query.eq('is_accessible', filters.is_accessible)
        if (filters?.english_speaking !== undefined) query = query.eq('english_speaking', filters.english_speaking)
        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 Taxi[]
    },

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