import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import type { Schedule } from '@/types'
import { mockSchedules } from '@/mock'

export const useScheduleStore = defineStore('schedule', () => {
  const schedules = ref<Schedule[]>([...mockSchedules])
  const selectedDate = ref<string>('')

  const filteredSchedules = computed(() => {
    if (!selectedDate.value) return schedules.value
    return schedules.value.filter((s) => s.date === selectedDate.value)
  })

  const todaySchedules = computed(() => {
    const today = new Date().toISOString().split('T')[0]
    return schedules.value.filter((s) => s.date === today)
  })

  function setSelectedDate(date: string) {
    selectedDate.value = date
  }

  function addSchedule(schedule: Schedule) {
    schedules.value.push(schedule)
  }

  function cancelSchedule(scheduleId: string) {
    const schedule = schedules.value.find((s) => s.id === scheduleId)
    if (schedule) {
      schedule.status = 'cancelled'
    }
  }

  return {
    schedules,
    selectedDate,
    filteredSchedules,
    todaySchedules,
    setSelectedDate,
    addSchedule,
    cancelSchedule,
  }
})
