// Copyright (C) 2025 Christian Mauduit <ufoot@ufoot.org>

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;

use crate::Fighter;

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Armies {
    fighters: HashMap<Uuid, Fighter>,
}

impl Armies {
    pub fn new() -> Self {
        Self {
            fighters: HashMap::new(),
        }
    }

    pub fn add_fighter(&mut self, fighter: Fighter) -> Uuid {
        let id = Uuid::new_v4();
        self.fighters.insert(id, fighter);
        id
    }

    pub fn remove_fighter(&mut self, id: &Uuid) -> Option<Fighter> {
        self.fighters.remove(id)
    }

    pub fn get_fighter(&self, id: &Uuid) -> Option<&Fighter> {
        self.fighters.get(id)
    }

    pub fn get_fighter_mut(&mut self, id: &Uuid) -> Option<&mut Fighter> {
        self.fighters.get_mut(id)
    }

    pub fn fighter_count(&self) -> usize {
        self.fighters.len()
    }

    pub fn iter(&self) -> impl Iterator<Item = (&Uuid, &Fighter)> {
        self.fighters.iter()
    }

    pub fn iter_mut(&mut self) -> impl Iterator<Item = (&Uuid, &mut Fighter)> {
        self.fighters.iter_mut()
    }
}

impl Default for Armies {
    fn default() -> Self {
        Self::new()
    }
}
