/** Pinia store for schedule management */
import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { BusSchedule } from '@/types'
import { schedulesService } from '@/services/schedulesService'

export const useScheduleStore = defineStore('schedule', () => {
    const schedules = ref<BusSchedule[]>([])
    const isLoading = ref(false)
    const error = ref<string | null>(null)

    async function loadRouteSchedules(routeId: string) {
        isLoading.value = true
        error.value = null
        try {
            schedules.value = await schedulesService.getRouteSchedules(routeId)
        } catch (e) {
            error.value = e instanceof Error ? e.message : 'Failed to load schedules'
            console.error('Error loading schedules:', e)
        } finally {
            isLoading.value = false
        }
    }

    async function loadStopSchedules(stopId: string) {
        isLoading.value = true
        error.value = null
        try {
            schedules.value = await schedulesService.getStopSchedules(stopId)
        } catch (e) {
            error.value = e instanceof Error ? e.message : 'Failed to load schedules'
            console.error('Error loading schedules:', e)
        } finally {
            isLoading.value = false
        }
    }

    return {
        schedules,
        isLoading,
        error,
        loadRouteSchedules,
        loadStopSchedules,
    }
})

