/** Pinia store for taxi management */
import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { Taxi } from '@/types'
import { taxisService, type TaxiFilters } from '@/services/taxisService'

export const useTaxiStore = defineStore('taxi', () => {
    const taxis = ref<Taxi[]>([])
    const isLoading = ref(false)
    const error = ref<string | null>(null)
    const filters = ref<TaxiFilters>({})

    async function loadTaxis(newFilters?: TaxiFilters) {
        isLoading.value = true
        error.value = null
        if (newFilters) {
            filters.value = newFilters
        }
        try {
            taxis.value = await taxisService.getAllTaxis(filters.value)
        } catch (e) {
            error.value = e instanceof Error ? e.message : 'Failed to load taxis'
            console.error('Error loading taxis:', e)
        } finally {
            isLoading.value = false
        }
    }

    function setFilters(newFilters: TaxiFilters) {
        filters.value = newFilters
        loadTaxis()
    }

    return {
        taxis,
        isLoading,
        error,
        filters,
        loadTaxis,
        setFilters,
    }
})

