/** Pinia store for shuttle management (Intercity/Tourism) */
import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { Shuttle } from '@/types'
import { shuttlesService, type ShuttleFilters } from '@/services/shuttlesService'

export const useShuttleStore = defineStore('shuttle', () => {
    const shuttles = ref<Shuttle[]>([])
    const isLoading = ref(false)
    const error = ref<string | null>(null)
    const filters = ref<ShuttleFilters>({})

    async function loadShuttles(newFilters?: ShuttleFilters) {
        isLoading.value = true
        error.value = null
        if (newFilters) {
            filters.value = newFilters
        }
        try {
            shuttles.value = await shuttlesService.getAllShuttles(filters.value)
        } catch (e) {
            error.value = e instanceof Error ? e.message : 'Failed to load shuttles'
            console.error('Error loading shuttles:', e)
        } finally {
            isLoading.value = false
        }
    }

    return {
        shuttles,
        isLoading,
        error,
        filters,
        loadShuttles,
    }
})
