import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import type { Vehicle, VehicleStatus } from '@/types'
import { mockVehicles } from '@/mock'

export const useVehicleStore = defineStore('vehicle', () => {
  const vehicles = ref<Vehicle[]>([...mockVehicles])
  const filterStatus = ref<VehicleStatus | 'all'>('all')

  const filteredVehicles = computed(() => {
    if (filterStatus.value === 'all') return vehicles.value
    return vehicles.value.filter((v) => v.status === filterStatus.value)
  })

  const availableVehicles = computed(() => vehicles.value.filter((v) => v.status === 'available'))
  const inUseCount = computed(() => vehicles.value.filter((v) => v.status === 'in_use').length)
  const maintenanceCount = computed(() => vehicles.value.filter((v) => v.status === 'maintenance').length)

  function setFilter(status: VehicleStatus | 'all') {
    filterStatus.value = status
  }

  function updateVehicleStatus(vehicleId: string, status: VehicleStatus) {
    const vehicle = vehicles.value.find((v) => v.id === vehicleId)
    if (vehicle) {
      vehicle.status = status
    }
  }

  return {
    vehicles,
    filterStatus,
    filteredVehicles,
    availableVehicles,
    inUseCount,
    maintenanceCount,
    setFilter,
    updateVehicleStatus,
  }
})
