use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RuntimeCompanyAnnualFinanceState {
    pub company_id: u32,
    pub outstanding_shares: u32,
    pub bond_count: u8,
    #[serde(default)]
    pub largest_live_bond_principal: Option<u32>,
    #[serde(default)]
    pub highest_coupon_live_bond_principal: Option<u32>,
    #[serde(default)]
    pub live_bond_coupon_burden_total: Option<i64>,
    pub assigned_share_pool: u32,
    pub unassigned_share_pool: u32,
    #[serde(default)]
    pub cached_share_price: Option<i64>,
    pub chairman_salary_baseline: u32,
    pub chairman_salary_current: u32,
    pub chairman_bonus_year: u32,
    pub chairman_bonus_amount: i32,
    pub founding_year: u32,
    pub last_bankruptcy_year: u32,
    pub last_dividend_year: u32,
    #[serde(default)]
    pub years_since_founding: Option<u32>,
    #[serde(default)]
    pub years_since_last_bankruptcy: Option<u32>,
    #[serde(default)]
    pub years_since_last_dividend: Option<u32>,
    #[serde(default)]
    pub current_partial_year_weight_numerator: Option<i64>,
    #[serde(default)]
    pub trailing_full_year_year_words: Vec<u32>,
    #[serde(default)]
    pub trailing_full_year_net_profits: Vec<i64>,
    #[serde(default)]
    pub trailing_full_year_revenues: Vec<i64>,
    #[serde(default)]
    pub trailing_full_year_fuel_costs: Vec<i64>,
    #[serde(default)]
    pub current_issue_absolute_counter: Option<u32>,
    #[serde(default)]
    pub prior_issue_absolute_counter: Option<u32>,
    #[serde(default)]
    pub current_issue_age_absolute_counter_delta: Option<i64>,
    pub current_issue_calendar_word: u32,
    #[serde(default)]
    pub current_issue_calendar_word_2: u32,
    pub prior_issue_calendar_word: u32,
    #[serde(default)]
    pub prior_issue_calendar_word_2: u32,
    #[serde(default)]
    pub preferred_locomotive_engine_type_raw_u8: Option<u8>,
    pub city_connection_latch: bool,
    pub linked_transit_latch: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RuntimeCompanyAnnualCreditorPressureState {
    pub company_id: u32,
    #[serde(default)]
    pub annual_mode_active: Option<bool>,
    #[serde(default)]
    pub bankruptcy_allowed: Option<bool>,
    #[serde(default)]
    pub years_since_last_bankruptcy: Option<u32>,
    #[serde(default)]
    pub years_since_founding: Option<u32>,
    pub recent_bad_net_profit_year_count: u32,
    #[serde(default)]
    pub recent_peak_revenue: Option<i64>,
    #[serde(default)]
    pub recent_three_year_net_profit_total: Option<i64>,
    #[serde(default)]
    pub pressure_ladder_cash_floor: Option<i64>,
    #[serde(default)]
    pub current_cash_plus_slot_12_total: Option<i64>,
    #[serde(default)]
    pub support_adjusted_share_price_floor: Option<i64>,
    #[serde(default)]
    pub support_adjusted_share_price_scalar: Option<i64>,
    #[serde(default)]
    pub current_fuel_cost: Option<i64>,
    #[serde(default)]
    pub current_fuel_cost_floor: Option<i64>,
    pub eligible_for_bankruptcy_branch: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RuntimeCompanyAnnualDeepDistressState {
    pub company_id: u32,
    #[serde(default)]
    pub bankruptcy_allowed: Option<bool>,
    #[serde(default)]
    pub years_since_founding: Option<u32>,
    #[serde(default)]
    pub years_since_last_bankruptcy: Option<u32>,
    #[serde(default)]
    pub current_cash: Option<i64>,
    pub recent_first_three_net_profit_years: Vec<i64>,
    #[serde(default)]
    pub deep_distress_cash_floor: Option<i64>,
    #[serde(default)]
    pub deep_distress_net_profit_floor: Option<i64>,
    pub eligible_for_bankruptcy_fallback: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RuntimeCompanyAnnualStockRepurchaseState {
    pub company_id: u32,
    #[serde(default)]
    pub annual_mode_active: Option<bool>,
    #[serde(default)]
    pub stock_issue_and_buyback_allowed: Option<bool>,
    pub city_connection_latch: bool,
    #[serde(default)]
    pub building_density_growth_setting: Option<u32>,
    #[serde(default)]
    pub linked_chairman_profile_id: Option<u32>,
    #[serde(default)]
    pub linked_chairman_personality_raw_u8: Option<u8>,
    #[serde(default)]
    pub repurchase_batch_size: Option<u32>,
    #[serde(default)]
    pub repurchase_factor_basis_points: Option<i64>,
    #[serde(default)]
    pub current_cash: Option<i64>,
    #[serde(default)]
    pub stock_value_gate_cash_floor: Option<i64>,
    #[serde(default)]
    pub support_adjusted_share_price_scalar: Option<i64>,
    #[serde(default)]
    pub affordability_cash_floor: Option<i64>,
    #[serde(default)]
    pub unassigned_share_pool: Option<u32>,
    pub eligible_for_single_batch_repurchase: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RuntimeCompanyAnnualStockIssueState {
    pub company_id: u32,
    #[serde(default)]
    pub annual_mode_active: Option<bool>,
    #[serde(default)]
    pub stock_issue_and_buyback_allowed: Option<bool>,
    #[serde(default)]
    pub bond_issue_and_repayment_allowed: Option<bool>,
    #[serde(default)]
    pub years_since_founding: Option<u32>,
    #[serde(default)]
    pub live_bond_count: Option<u8>,
    #[serde(default)]
    pub initial_issue_batch_size: Option<u32>,
    #[serde(default)]
    pub trimmed_issue_batch_size: Option<u32>,
    #[serde(default)]
    pub share_pressure_basis_points: Option<i64>,
    #[serde(default)]
    pub pressured_support_adjusted_share_price_scalar: Option<i64>,
    #[serde(default)]
    pub pressured_proceeds: Option<i64>,
    #[serde(default)]
    pub book_value_per_share_floor_applied: Option<i64>,
    #[serde(default)]
    pub price_to_book_ratio_basis_points: Option<i64>,
    #[serde(default)]
    pub current_cash: Option<i64>,
    #[serde(default)]
    pub highest_coupon_live_bond_principal: Option<u32>,
    #[serde(default)]
    pub highest_coupon_live_bond_rate_basis_points: Option<i64>,
    #[serde(default)]
    pub current_issue_age_absolute_counter_delta: Option<i64>,
    #[serde(default)]
    pub current_issue_cooldown_floor: Option<i64>,
    #[serde(default)]
    pub minimum_price_to_book_ratio_basis_points: Option<i64>,
    #[serde(default)]
    pub passes_share_price_floor: Option<bool>,
    #[serde(default)]
    pub passes_proceeds_floor: Option<bool>,
    #[serde(default)]
    pub passes_cash_gate: Option<bool>,
    #[serde(default)]
    pub passes_issue_cooldown_gate: Option<bool>,
    #[serde(default)]
    pub passes_coupon_price_to_book_gate: Option<bool>,
    pub eligible_for_double_tranche_issue: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RuntimeCompanyAnnualBondPolicyState {
    pub company_id: u32,
    #[serde(default)]
    pub annual_mode_active: Option<bool>,
    #[serde(default)]
    pub bond_issue_and_repayment_allowed: Option<bool>,
    pub linked_transit_latch: bool,
    #[serde(default)]
    pub live_bond_count: Option<u8>,
    #[serde(default)]
    pub live_bond_principal_total: Option<u32>,
    #[serde(default)]
    pub matured_live_bond_count: Option<u32>,
    #[serde(default)]
    pub matured_live_bond_principal_total: Option<u32>,
    #[serde(default)]
    pub next_live_bond_maturity_year: Option<u32>,
    #[serde(default)]
    pub live_bond_coupon_burden_total: Option<i64>,
    #[serde(default)]
    pub current_cash: Option<i64>,
    #[serde(default)]
    pub cash_after_full_repayment: Option<i64>,
    #[serde(default)]
    pub issue_cash_floor: Option<i64>,
    #[serde(default)]
    pub issue_principal_step: Option<u32>,
    #[serde(default)]
    pub proposed_issue_bond_count: Option<u32>,
    #[serde(default)]
    pub proposed_issue_total_principal: Option<u32>,
    #[serde(default)]
    pub proposed_issue_years_to_maturity: Option<u32>,
    pub eligible_for_bond_issue_branch: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RuntimeCompanyAnnualDividendPolicyState {
    pub company_id: u32,
    #[serde(default)]
    pub annual_mode_active: Option<bool>,
    #[serde(default)]
    pub dividend_adjustment_allowed: Option<bool>,
    #[serde(default)]
    pub years_since_last_dividend: Option<u32>,
    #[serde(default)]
    pub years_since_founding: Option<u32>,
    #[serde(default)]
    pub outstanding_shares: Option<u32>,
    #[serde(default)]
    pub unassigned_share_pool: Option<u32>,
    #[serde(default)]
    pub weighted_recent_net_profit_total: Option<i64>,
    #[serde(default)]
    pub weighted_recent_net_profit_average: Option<i64>,
    #[serde(default)]
    pub current_cash: Option<i64>,
    pub tiny_unassigned_share_cash_supplement_branch: bool,
    #[serde(default)]
    pub tentative_target_dividend_per_share_tenths: Option<i64>,
    #[serde(default)]
    pub current_dividend_per_share_tenths: Option<i64>,
    #[serde(default)]
    pub building_density_growth_setting: Option<u32>,
    #[serde(default)]
    pub growth_adjusted_current_dividend_per_share_tenths: Option<i64>,
    #[serde(default)]
    pub board_approved_dividend_rate_ceiling_tenths: Option<i64>,
    #[serde(default)]
    pub proposed_dividend_per_share_tenths: Option<i64>,
    pub eligible_for_dividend_adjustment_branch: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, PartialOrd, Ord)]
#[serde(rename_all = "snake_case")]
pub enum RuntimeCompanyAnnualFinancePolicyAction {
    #[default]
    None,
    CreditorPressureBankruptcy,
    DeepDistressBankruptcyFallback,
    BondIssue,
    StockRepurchase,
    StockIssue,
    DividendAdjustment,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RuntimeCompanyAnnualFinancePolicyState {
    pub company_id: u32,
    pub action: RuntimeCompanyAnnualFinancePolicyAction,
    pub creditor_pressure_bankruptcy_eligible: bool,
    pub deep_distress_bankruptcy_fallback_eligible: bool,
    pub bond_issue_eligible: bool,
    pub stock_repurchase_eligible: bool,
    pub stock_issue_eligible: bool,
    pub dividend_adjustment_eligible: bool,
}
