import { defineStore } from 'pinia'
import type { ExtendedCharacter, Zone } from '@/types'

export const useZoneStore = defineStore('zone', {
  state: () => ({
    zone: null as Zone | null,
    characters: [] as ExtendedCharacter[]
  }),
  getters: {
    getCharacterById: (state) => {
      return (id: number) => state.characters.find(char => char.id === id)
    },
    getCharacterCount: (state) => {
      return state.characters.length
    },
    isZoneSet: (state) => {
      return state.zone !== null
    }
  },
  actions: {
    setZone(zone: Zone | null) {
      this.zone = zone
    },
    setCharacters(characters: ExtendedCharacter[]) {
      this.characters = characters
    },
    addCharacter(character: ExtendedCharacter) {
      this.characters.push(character)
    },
    updateCharacter(updatedCharacter: ExtendedCharacter) {
      const index = this.characters.findIndex(char => char.id === updatedCharacter.id)
      if (index !== -1) {
        this.characters[index] = { ...this.characters[index], ...updatedCharacter }
      }
    },
    removeCharacter(character_id: number) {
      this.characters = this.characters.filter(char => char.id !== character_id)
    },
    reset() {
      this.zone = null
      this.characters = []
    }
  }
})