use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use crate::state::{
    RuntimeCompanyControllerKind, RuntimeCompanyMarketState, RuntimeTrackPieceCounts,
};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SmpLoadedCompanyRosterEntry {
    pub company_id: u32,
    pub active: bool,
    #[serde(default)]
    pub controller_kind: RuntimeCompanyControllerKind,
    pub current_cash: i64,
    pub debt: u64,
    #[serde(default)]
    pub credit_rating_score: Option<i64>,
    #[serde(default)]
    pub prime_rate: Option<i64>,
    #[serde(default)]
    pub available_track_laying_capacity: Option<u32>,
    #[serde(default)]
    pub track_piece_counts: RuntimeTrackPieceCounts,
    #[serde(default)]
    pub linked_chairman_profile_id: Option<u32>,
    #[serde(default)]
    pub book_value_per_share: i64,
    #[serde(default)]
    pub investor_confidence: i64,
    #[serde(default)]
    pub management_attitude: i64,
    #[serde(default)]
    pub takeover_cooldown_year: Option<u32>,
    #[serde(default)]
    pub merger_cooldown_year: Option<u32>,
    #[serde(default)]
    pub preferred_locomotive_engine_type_raw_u8: Option<u8>,
    #[serde(default)]
    pub market_state: Option<RuntimeCompanyMarketState>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SmpLoadedCompanyRoster {
    pub source_kind: String,
    pub semantic_family: String,
    pub observed_entry_count: usize,
    #[serde(default)]
    pub selected_company_id: Option<u32>,
    pub entries: Vec<SmpLoadedCompanyRosterEntry>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SmpLoadedChairmanProfileEntry {
    pub profile_id: u32,
    pub name: String,
    pub active: bool,
    #[serde(default)]
    pub current_cash: i64,
    #[serde(default)]
    pub linked_company_id: Option<u32>,
    #[serde(default)]
    pub company_holdings: BTreeMap<u32, u32>,
    #[serde(default)]
    pub holdings_value_total: i64,
    #[serde(default)]
    pub net_worth_total: i64,
    #[serde(default)]
    pub purchasing_power_total: i64,
    #[serde(default)]
    pub personality_byte_0x291: Option<u8>,
    #[serde(default)]
    pub issue_opinion_terms_raw_i32: Vec<i32>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SmpLoadedChairmanProfileTable {
    pub source_kind: String,
    pub semantic_family: String,
    pub observed_entry_count: usize,
    #[serde(default)]
    pub selected_chairman_profile_id: Option<u32>,
    pub entries: Vec<SmpLoadedChairmanProfileEntry>,
}
