mod annual_finance;
mod bond_policy;
mod distress;
mod dividend_policy;
mod equity_actions;
mod inputs;
mod market;
mod model;
mod periodic;

use crate::state::RuntimeState;
use crate::summary::RuntimeSummary;

use annual_finance::fill_selected_company_annual_finance_fields;
use bond_policy::fill_selected_company_bond_policy_fields;
use distress::fill_selected_company_distress_fields;
use dividend_policy::fill_selected_company_dividend_policy_fields;
use equity_actions::fill_selected_company_equity_action_fields;
use inputs::SelectedCompanyInputs;
use market::fill_selected_company_market_fields;
pub(super) use model::SelectedCompanySummaryFields;
use periodic::fill_selected_company_periodic_fields;

pub(super) fn build_selected_company_summary_fields(
    state: &RuntimeState,
) -> SelectedCompanySummaryFields {
    let inputs = SelectedCompanyInputs::from_state(state);
    let mut fields = SelectedCompanySummaryFields::default();
    fill_selected_company_market_fields(&mut fields, &inputs);
    fill_selected_company_periodic_fields(&mut fields, &inputs);
    fill_selected_company_distress_fields(&mut fields, &inputs);
    fill_selected_company_bond_policy_fields(&mut fields, &inputs);
    fill_selected_company_equity_action_fields(&mut fields, &inputs);
    fill_selected_company_dividend_policy_fields(&mut fields, &inputs);
    fill_selected_company_annual_finance_fields(&mut fields, &inputs);
    fields
}

pub(super) fn apply_selected_company_summary_fields(
    summary: &mut RuntimeSummary,
    fields: SelectedCompanySummaryFields,
) {
    let SelectedCompanySummaryFields {
        selected_company_outstanding_shares,
        selected_company_bond_count,
        selected_company_largest_live_bond_principal,
        selected_company_highest_coupon_live_bond_principal,
        selected_company_assigned_share_pool,
        selected_company_unassigned_share_pool,
        selected_company_cached_share_price,
        selected_company_cached_share_price_value_f32_text,
        selected_company_recent_per_share_cache_absolute_counter,
        selected_company_recent_per_share_cached_value_f64_text,
        selected_company_recent_per_share_subscore_value_f32_text,
        selected_company_mutable_support_scalar_value_f32_text,
        selected_company_stat_band_root_0cfb_count,
        selected_company_stat_band_root_0d7f_count,
        selected_company_stat_band_root_1c47_count,
        selected_company_last_dividend_year,
        selected_company_years_since_founding,
        selected_company_years_since_last_bankruptcy,
        selected_company_years_since_last_dividend,
        selected_company_current_partial_year_weight_numerator,
        selected_company_current_issue_absolute_counter,
        selected_company_prior_issue_absolute_counter,
        selected_company_current_issue_age_absolute_counter_delta,
        selected_company_periodic_side_latch_preferred_locomotive_engine_type_raw_u8,
        selected_company_periodic_side_latch_city_connection_latch,
        selected_company_periodic_side_latch_linked_transit_latch,
        selected_company_linked_transit_route_anchor_entry_id,
        selected_company_linked_transit_route_anchor_fallback_counts,
        selected_company_periodic_service_base_route_preference_raw_u8,
        selected_company_periodic_service_effective_route_preference_raw_u8,
        selected_company_periodic_service_electric_route_preference_override_active,
        selected_company_periodic_service_route_quality_multiplier_basis_points,
        active_periodic_route_preference_override_company_id,
        active_periodic_route_preference_override_effective_raw_u8,
        last_periodic_route_preference_override_company_id,
        last_periodic_route_preference_override_effective_raw_u8,
        selected_company_chairman_bonus_year,
        selected_company_chairman_bonus_amount,
        selected_company_creditor_pressure_recent_bad_net_profit_year_count,
        selected_company_creditor_pressure_recent_peak_revenue,
        selected_company_creditor_pressure_recent_three_year_net_profit_total,
        selected_company_creditor_pressure_cash_floor,
        selected_company_creditor_pressure_cash_plus_slot_12_total,
        selected_company_creditor_pressure_share_price_floor,
        selected_company_creditor_pressure_share_price_scalar,
        selected_company_creditor_pressure_current_fuel_cost,
        selected_company_creditor_pressure_current_fuel_cost_floor,
        selected_company_creditor_pressure_eligible_for_bankruptcy_branch,
        selected_company_deep_distress_current_cash,
        selected_company_deep_distress_recent_first_three_net_profit_years,
        selected_company_deep_distress_cash_floor,
        selected_company_deep_distress_net_profit_floor,
        selected_company_deep_distress_eligible_for_bankruptcy_fallback,
        selected_company_annual_bond_linked_transit_latch,
        selected_company_annual_bond_live_bond_count,
        selected_company_annual_bond_live_bond_principal_total,
        selected_company_annual_bond_matured_live_bond_count,
        selected_company_annual_bond_matured_live_bond_principal_total,
        selected_company_annual_bond_next_live_bond_maturity_year,
        selected_company_annual_bond_live_bond_coupon_burden_total,
        selected_company_annual_bond_current_cash,
        selected_company_annual_bond_cash_after_full_repayment,
        selected_company_annual_bond_issue_cash_floor,
        selected_company_annual_bond_issue_principal_step,
        selected_company_annual_bond_proposed_issue_bond_count,
        selected_company_annual_bond_proposed_issue_total_principal,
        selected_company_annual_bond_proposed_issue_years_to_maturity,
        selected_company_annual_bond_eligible_for_issue_branch,
        selected_company_stock_repurchase_city_connection_latch,
        selected_company_stock_repurchase_building_density_growth_setting,
        selected_company_stock_repurchase_linked_chairman_profile_id,
        selected_company_stock_repurchase_linked_chairman_personality_raw_u8,
        selected_company_stock_repurchase_batch_size,
        selected_company_stock_repurchase_factor_basis_points,
        selected_company_stock_repurchase_current_cash,
        selected_company_stock_repurchase_stock_value_gate_cash_floor,
        selected_company_stock_repurchase_support_adjusted_share_price_scalar,
        selected_company_stock_repurchase_affordability_cash_floor,
        selected_company_stock_repurchase_unassigned_share_pool,
        selected_company_stock_repurchase_eligible_for_single_batch,
        selected_company_stock_issue_live_bond_count,
        selected_company_stock_issue_initial_batch_size,
        selected_company_stock_issue_trimmed_batch_size,
        selected_company_stock_issue_share_pressure_basis_points,
        selected_company_stock_issue_pressured_share_price_scalar,
        selected_company_stock_issue_pressured_proceeds,
        selected_company_stock_issue_book_value_per_share_floor_applied,
        selected_company_stock_issue_price_to_book_ratio_basis_points,
        selected_company_stock_issue_current_cash,
        selected_company_stock_issue_highest_coupon_live_bond_principal,
        selected_company_stock_issue_highest_coupon_live_bond_rate_basis_points,
        selected_company_stock_issue_current_issue_age_absolute_counter_delta,
        selected_company_stock_issue_current_issue_cooldown_floor,
        selected_company_stock_issue_minimum_price_to_book_ratio_basis_points,
        selected_company_stock_issue_passes_share_price_floor,
        selected_company_stock_issue_passes_proceeds_floor,
        selected_company_stock_issue_passes_cash_gate,
        selected_company_stock_issue_passes_issue_cooldown_gate,
        selected_company_stock_issue_passes_coupon_price_to_book_gate,
        selected_company_stock_issue_eligible_for_double_tranche,
        selected_company_dividend_weighted_recent_net_profit_total,
        selected_company_dividend_weighted_recent_net_profit_average,
        selected_company_dividend_current_cash,
        selected_company_dividend_tiny_unassigned_share_cash_supplement_branch,
        selected_company_dividend_tentative_target_per_share_tenths,
        selected_company_dividend_current_per_share_tenths,
        selected_company_dividend_growth_adjusted_current_per_share_tenths,
        selected_company_dividend_board_approved_ceiling_tenths,
        selected_company_dividend_proposed_per_share_tenths,
        selected_company_dividend_eligible_for_adjustment_branch,
        selected_company_annual_finance_policy_action,
        selected_company_annual_finance_news_family_candidate,
        selected_company_annual_finance_last_news_selector,
        annual_finance_last_news_event_count,
        selected_company_annual_finance_policy_creditor_pressure_bankruptcy_eligible,
        selected_company_annual_finance_policy_deep_distress_bankruptcy_fallback_eligible,
        selected_company_annual_finance_policy_bond_issue_eligible,
        selected_company_annual_finance_policy_stock_repurchase_eligible,
        selected_company_annual_finance_policy_stock_issue_eligible,
        selected_company_annual_finance_policy_dividend_adjustment_eligible,
    } = fields;

    summary.selected_company_outstanding_shares = selected_company_outstanding_shares;
    summary.selected_company_bond_count = selected_company_bond_count;
    summary.selected_company_largest_live_bond_principal =
        selected_company_largest_live_bond_principal;
    summary.selected_company_highest_coupon_live_bond_principal =
        selected_company_highest_coupon_live_bond_principal;
    summary.selected_company_assigned_share_pool = selected_company_assigned_share_pool;
    summary.selected_company_unassigned_share_pool = selected_company_unassigned_share_pool;
    summary.selected_company_cached_share_price = selected_company_cached_share_price;
    summary.selected_company_cached_share_price_value_f32_text =
        selected_company_cached_share_price_value_f32_text;
    summary.selected_company_recent_per_share_cache_absolute_counter =
        selected_company_recent_per_share_cache_absolute_counter;
    summary.selected_company_recent_per_share_cached_value_f64_text =
        selected_company_recent_per_share_cached_value_f64_text;
    summary.selected_company_recent_per_share_subscore_value_f32_text =
        selected_company_recent_per_share_subscore_value_f32_text;
    summary.selected_company_mutable_support_scalar_value_f32_text =
        selected_company_mutable_support_scalar_value_f32_text;
    summary.selected_company_stat_band_root_0cfb_count = selected_company_stat_band_root_0cfb_count;
    summary.selected_company_stat_band_root_0d7f_count = selected_company_stat_band_root_0d7f_count;
    summary.selected_company_stat_band_root_1c47_count = selected_company_stat_band_root_1c47_count;
    summary.selected_company_last_dividend_year = selected_company_last_dividend_year;
    summary.selected_company_years_since_founding = selected_company_years_since_founding;
    summary.selected_company_years_since_last_bankruptcy =
        selected_company_years_since_last_bankruptcy;
    summary.selected_company_years_since_last_dividend = selected_company_years_since_last_dividend;
    summary.selected_company_current_partial_year_weight_numerator =
        selected_company_current_partial_year_weight_numerator;
    summary.selected_company_current_issue_absolute_counter =
        selected_company_current_issue_absolute_counter;
    summary.selected_company_prior_issue_absolute_counter =
        selected_company_prior_issue_absolute_counter;
    summary.selected_company_current_issue_age_absolute_counter_delta =
        selected_company_current_issue_age_absolute_counter_delta;
    summary.selected_company_periodic_side_latch_preferred_locomotive_engine_type_raw_u8 =
        selected_company_periodic_side_latch_preferred_locomotive_engine_type_raw_u8;
    summary.selected_company_periodic_side_latch_city_connection_latch =
        selected_company_periodic_side_latch_city_connection_latch;
    summary.selected_company_periodic_side_latch_linked_transit_latch =
        selected_company_periodic_side_latch_linked_transit_latch;
    summary.selected_company_linked_transit_route_anchor_entry_id =
        selected_company_linked_transit_route_anchor_entry_id;
    summary.selected_company_linked_transit_route_anchor_fallback_counts =
        selected_company_linked_transit_route_anchor_fallback_counts;
    summary.selected_company_periodic_service_base_route_preference_raw_u8 =
        selected_company_periodic_service_base_route_preference_raw_u8;
    summary.selected_company_periodic_service_effective_route_preference_raw_u8 =
        selected_company_periodic_service_effective_route_preference_raw_u8;
    summary.selected_company_periodic_service_electric_route_preference_override_active =
        selected_company_periodic_service_electric_route_preference_override_active;
    summary.selected_company_periodic_service_route_quality_multiplier_basis_points =
        selected_company_periodic_service_route_quality_multiplier_basis_points;
    summary.active_periodic_route_preference_override_company_id =
        active_periodic_route_preference_override_company_id;
    summary.active_periodic_route_preference_override_effective_raw_u8 =
        active_periodic_route_preference_override_effective_raw_u8;
    summary.last_periodic_route_preference_override_company_id =
        last_periodic_route_preference_override_company_id;
    summary.last_periodic_route_preference_override_effective_raw_u8 =
        last_periodic_route_preference_override_effective_raw_u8;
    summary.selected_company_chairman_bonus_year = selected_company_chairman_bonus_year;
    summary.selected_company_chairman_bonus_amount = selected_company_chairman_bonus_amount;
    summary.selected_company_creditor_pressure_recent_bad_net_profit_year_count =
        selected_company_creditor_pressure_recent_bad_net_profit_year_count;
    summary.selected_company_creditor_pressure_recent_peak_revenue =
        selected_company_creditor_pressure_recent_peak_revenue;
    summary.selected_company_creditor_pressure_recent_three_year_net_profit_total =
        selected_company_creditor_pressure_recent_three_year_net_profit_total;
    summary.selected_company_creditor_pressure_cash_floor =
        selected_company_creditor_pressure_cash_floor;
    summary.selected_company_creditor_pressure_cash_plus_slot_12_total =
        selected_company_creditor_pressure_cash_plus_slot_12_total;
    summary.selected_company_creditor_pressure_share_price_floor =
        selected_company_creditor_pressure_share_price_floor;
    summary.selected_company_creditor_pressure_share_price_scalar =
        selected_company_creditor_pressure_share_price_scalar;
    summary.selected_company_creditor_pressure_current_fuel_cost =
        selected_company_creditor_pressure_current_fuel_cost;
    summary.selected_company_creditor_pressure_current_fuel_cost_floor =
        selected_company_creditor_pressure_current_fuel_cost_floor;
    summary.selected_company_creditor_pressure_eligible_for_bankruptcy_branch =
        selected_company_creditor_pressure_eligible_for_bankruptcy_branch;
    summary.selected_company_deep_distress_current_cash =
        selected_company_deep_distress_current_cash;
    summary.selected_company_deep_distress_recent_first_three_net_profit_years =
        selected_company_deep_distress_recent_first_three_net_profit_years;
    summary.selected_company_deep_distress_cash_floor = selected_company_deep_distress_cash_floor;
    summary.selected_company_deep_distress_net_profit_floor =
        selected_company_deep_distress_net_profit_floor;
    summary.selected_company_deep_distress_eligible_for_bankruptcy_fallback =
        selected_company_deep_distress_eligible_for_bankruptcy_fallback;
    summary.selected_company_annual_bond_linked_transit_latch =
        selected_company_annual_bond_linked_transit_latch;
    summary.selected_company_annual_bond_live_bond_count =
        selected_company_annual_bond_live_bond_count;
    summary.selected_company_annual_bond_live_bond_principal_total =
        selected_company_annual_bond_live_bond_principal_total;
    summary.selected_company_annual_bond_matured_live_bond_count =
        selected_company_annual_bond_matured_live_bond_count;
    summary.selected_company_annual_bond_matured_live_bond_principal_total =
        selected_company_annual_bond_matured_live_bond_principal_total;
    summary.selected_company_annual_bond_next_live_bond_maturity_year =
        selected_company_annual_bond_next_live_bond_maturity_year;
    summary.selected_company_annual_bond_live_bond_coupon_burden_total =
        selected_company_annual_bond_live_bond_coupon_burden_total;
    summary.selected_company_annual_bond_current_cash = selected_company_annual_bond_current_cash;
    summary.selected_company_annual_bond_cash_after_full_repayment =
        selected_company_annual_bond_cash_after_full_repayment;
    summary.selected_company_annual_bond_issue_cash_floor =
        selected_company_annual_bond_issue_cash_floor;
    summary.selected_company_annual_bond_issue_principal_step =
        selected_company_annual_bond_issue_principal_step;
    summary.selected_company_annual_bond_proposed_issue_bond_count =
        selected_company_annual_bond_proposed_issue_bond_count;
    summary.selected_company_annual_bond_proposed_issue_total_principal =
        selected_company_annual_bond_proposed_issue_total_principal;
    summary.selected_company_annual_bond_proposed_issue_years_to_maturity =
        selected_company_annual_bond_proposed_issue_years_to_maturity;
    summary.selected_company_annual_bond_eligible_for_issue_branch =
        selected_company_annual_bond_eligible_for_issue_branch;
    summary.selected_company_stock_repurchase_city_connection_latch =
        selected_company_stock_repurchase_city_connection_latch;
    summary.selected_company_stock_repurchase_building_density_growth_setting =
        selected_company_stock_repurchase_building_density_growth_setting;
    summary.selected_company_stock_repurchase_linked_chairman_profile_id =
        selected_company_stock_repurchase_linked_chairman_profile_id;
    summary.selected_company_stock_repurchase_linked_chairman_personality_raw_u8 =
        selected_company_stock_repurchase_linked_chairman_personality_raw_u8;
    summary.selected_company_stock_repurchase_batch_size =
        selected_company_stock_repurchase_batch_size;
    summary.selected_company_stock_repurchase_factor_basis_points =
        selected_company_stock_repurchase_factor_basis_points;
    summary.selected_company_stock_repurchase_current_cash =
        selected_company_stock_repurchase_current_cash;
    summary.selected_company_stock_repurchase_stock_value_gate_cash_floor =
        selected_company_stock_repurchase_stock_value_gate_cash_floor;
    summary.selected_company_stock_repurchase_support_adjusted_share_price_scalar =
        selected_company_stock_repurchase_support_adjusted_share_price_scalar;
    summary.selected_company_stock_repurchase_affordability_cash_floor =
        selected_company_stock_repurchase_affordability_cash_floor;
    summary.selected_company_stock_repurchase_unassigned_share_pool =
        selected_company_stock_repurchase_unassigned_share_pool;
    summary.selected_company_stock_repurchase_eligible_for_single_batch =
        selected_company_stock_repurchase_eligible_for_single_batch;
    summary.selected_company_stock_issue_live_bond_count =
        selected_company_stock_issue_live_bond_count;
    summary.selected_company_stock_issue_initial_batch_size =
        selected_company_stock_issue_initial_batch_size;
    summary.selected_company_stock_issue_trimmed_batch_size =
        selected_company_stock_issue_trimmed_batch_size;
    summary.selected_company_stock_issue_share_pressure_basis_points =
        selected_company_stock_issue_share_pressure_basis_points;
    summary.selected_company_stock_issue_pressured_share_price_scalar =
        selected_company_stock_issue_pressured_share_price_scalar;
    summary.selected_company_stock_issue_pressured_proceeds =
        selected_company_stock_issue_pressured_proceeds;
    summary.selected_company_stock_issue_book_value_per_share_floor_applied =
        selected_company_stock_issue_book_value_per_share_floor_applied;
    summary.selected_company_stock_issue_price_to_book_ratio_basis_points =
        selected_company_stock_issue_price_to_book_ratio_basis_points;
    summary.selected_company_stock_issue_current_cash = selected_company_stock_issue_current_cash;
    summary.selected_company_stock_issue_highest_coupon_live_bond_principal =
        selected_company_stock_issue_highest_coupon_live_bond_principal;
    summary.selected_company_stock_issue_highest_coupon_live_bond_rate_basis_points =
        selected_company_stock_issue_highest_coupon_live_bond_rate_basis_points;
    summary.selected_company_stock_issue_current_issue_age_absolute_counter_delta =
        selected_company_stock_issue_current_issue_age_absolute_counter_delta;
    summary.selected_company_stock_issue_current_issue_cooldown_floor =
        selected_company_stock_issue_current_issue_cooldown_floor;
    summary.selected_company_stock_issue_minimum_price_to_book_ratio_basis_points =
        selected_company_stock_issue_minimum_price_to_book_ratio_basis_points;
    summary.selected_company_stock_issue_passes_share_price_floor =
        selected_company_stock_issue_passes_share_price_floor;
    summary.selected_company_stock_issue_passes_proceeds_floor =
        selected_company_stock_issue_passes_proceeds_floor;
    summary.selected_company_stock_issue_passes_cash_gate =
        selected_company_stock_issue_passes_cash_gate;
    summary.selected_company_stock_issue_passes_issue_cooldown_gate =
        selected_company_stock_issue_passes_issue_cooldown_gate;
    summary.selected_company_stock_issue_passes_coupon_price_to_book_gate =
        selected_company_stock_issue_passes_coupon_price_to_book_gate;
    summary.selected_company_stock_issue_eligible_for_double_tranche =
        selected_company_stock_issue_eligible_for_double_tranche;
    summary.selected_company_dividend_weighted_recent_net_profit_total =
        selected_company_dividend_weighted_recent_net_profit_total;
    summary.selected_company_dividend_weighted_recent_net_profit_average =
        selected_company_dividend_weighted_recent_net_profit_average;
    summary.selected_company_dividend_current_cash = selected_company_dividend_current_cash;
    summary.selected_company_dividend_tiny_unassigned_share_cash_supplement_branch =
        selected_company_dividend_tiny_unassigned_share_cash_supplement_branch;
    summary.selected_company_dividend_tentative_target_per_share_tenths =
        selected_company_dividend_tentative_target_per_share_tenths;
    summary.selected_company_dividend_current_per_share_tenths =
        selected_company_dividend_current_per_share_tenths;
    summary.selected_company_dividend_growth_adjusted_current_per_share_tenths =
        selected_company_dividend_growth_adjusted_current_per_share_tenths;
    summary.selected_company_dividend_board_approved_ceiling_tenths =
        selected_company_dividend_board_approved_ceiling_tenths;
    summary.selected_company_dividend_proposed_per_share_tenths =
        selected_company_dividend_proposed_per_share_tenths;
    summary.selected_company_dividend_eligible_for_adjustment_branch =
        selected_company_dividend_eligible_for_adjustment_branch;
    summary.selected_company_annual_finance_policy_action =
        selected_company_annual_finance_policy_action;
    summary.selected_company_annual_finance_news_family_candidate =
        selected_company_annual_finance_news_family_candidate;
    summary.selected_company_annual_finance_last_news_selector =
        selected_company_annual_finance_last_news_selector;
    summary.annual_finance_last_news_event_count = annual_finance_last_news_event_count;
    summary.selected_company_annual_finance_policy_creditor_pressure_bankruptcy_eligible =
        selected_company_annual_finance_policy_creditor_pressure_bankruptcy_eligible;
    summary.selected_company_annual_finance_policy_deep_distress_bankruptcy_fallback_eligible =
        selected_company_annual_finance_policy_deep_distress_bankruptcy_fallback_eligible;
    summary.selected_company_annual_finance_policy_bond_issue_eligible =
        selected_company_annual_finance_policy_bond_issue_eligible;
    summary.selected_company_annual_finance_policy_stock_repurchase_eligible =
        selected_company_annual_finance_policy_stock_repurchase_eligible;
    summary.selected_company_annual_finance_policy_stock_issue_eligible =
        selected_company_annual_finance_policy_stock_issue_eligible;
    summary.selected_company_annual_finance_policy_dividend_adjustment_eligible =
        selected_company_annual_finance_policy_dividend_adjustment_eligible;
}
