use std::collections::BTreeMap;

use crate::derived::runtime_annual_finance_news_family_candidate_label;
use crate::event::news::RuntimeAnnualFinanceNewsEvent;
use crate::state::{RuntimeCompanyAnnualFinancePolicyAction, RuntimeState};

pub(super) fn record_annual_finance_news(
    state: &mut RuntimeState,
    company_id: u32,
    action: RuntimeCompanyAnnualFinancePolicyAction,
    finance_news_family_candidates: &mut BTreeMap<u32, String>,
    annual_finance_news_events: &mut Vec<RuntimeAnnualFinanceNewsEvent>,
) {
    let Some(label) = runtime_annual_finance_news_family_candidate_label(
        action,
        state.service_state.annual_bond_last_retired_principal_total,
        state.service_state.annual_bond_last_issued_principal_total,
        state.service_state.annual_stock_repurchase_last_share_count,
        state.service_state.annual_stock_issue_last_share_count,
    ) else {
        return;
    };

    state
        .service_state
        .annual_finance_last_news_family_candidates
        .insert(company_id, label.to_string());
    finance_news_family_candidates.insert(company_id, label.to_string());
    let news_event = RuntimeAnnualFinanceNewsEvent {
        company_id,
        selector_label: label.to_string(),
        action_label: crate::derived::runtime_company_annual_finance_policy_action_label(action)
            .to_string(),
        retired_principal_total: state.service_state.annual_bond_last_retired_principal_total,
        issued_principal_total: state.service_state.annual_bond_last_issued_principal_total,
        repurchased_share_count: state.service_state.annual_stock_repurchase_last_share_count,
        issued_share_count: state.service_state.annual_stock_issue_last_share_count,
    };
    state
        .service_state
        .annual_finance_last_news_events
        .push(news_event.clone());
    annual_finance_news_events.push(news_event);
}
