import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import type { Personnel, PersonnelStatus } from '@/types'
import { mockPersonnel } from '@/mock'

export const usePersonnelStore = defineStore('personnel', () => {
  const personnel = ref<Personnel[]>([...mockPersonnel])
  const filterStatus = ref<PersonnelStatus | 'all'>('all')

  const filteredPersonnel = computed(() => {
    if (filterStatus.value === 'all') return personnel.value
    return personnel.value.filter((p) => p.status === filterStatus.value)
  })

  const onDutyStaff = computed(() => personnel.value.filter((p) => p.status === 'working'))
  const idleStaff = computed(() => personnel.value.filter((p) => p.status === 'idle'))
  const emergencyTeam = computed(() => personnel.value.filter((p) => p.status === 'standby'))
  const restingStaff = computed(() => personnel.value.filter((p) => p.status === 'rest'))

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

  function updatePersonnelStatus(personnelId: string, status: PersonnelStatus) {
    const person = personnel.value.find((p) => p.id === personnelId)
    if (person) {
      person.status = status
    }
  }

  return {
    personnel,
    filterStatus,
    filteredPersonnel,
    onDutyStaff,
    idleStaff,
    emergencyTeam,
    restingStaff,
    setFilter,
    updatePersonnelStatus,
  }
})
