use super::context::ValidationContext;
use crate::state::RuntimeState;

pub(super) fn validate_service_state(
    state: &RuntimeState,
    context: &ValidationContext,
) -> Result<(), String> {
    for company_id in state.service_state.company_market_state.keys() {
        if !context.seen_company_ids.contains(company_id) {
            return Err(format!(
                "service_state.company_market_state references unknown company_id {}",
                company_id
            ));
        }
    }
    for company_id in state.service_state.company_periodic_side_latch_state.keys() {
        if !context.seen_company_ids.contains(company_id) {
            return Err(format!(
                "service_state.company_periodic_side_latch_state references unknown company_id {}",
                company_id
            ));
        }
    }
    if let Some(override_state) = &state
        .service_state
        .active_periodic_route_preference_override
    {
        if !context
            .seen_company_ids
            .contains(&override_state.company_id)
        {
            return Err(format!(
                "service_state.active_periodic_route_preference_override references unknown company_id {}",
                override_state.company_id
            ));
        }
    }
    if let Some(override_state) = &state.service_state.last_periodic_route_preference_override {
        if !context
            .seen_company_ids
            .contains(&override_state.company_id)
        {
            return Err(format!(
                "service_state.last_periodic_route_preference_override references unknown company_id {}",
                override_state.company_id
            ));
        }
    }
    for company_id in state.service_state.annual_finance_last_actions.keys() {
        if !context.seen_company_ids.contains(company_id) {
            return Err(format!(
                "service_state.annual_finance_last_actions references unknown company_id {}",
                company_id
            ));
        }
    }
    for company_id in state
        .service_state
        .annual_finance_last_news_family_candidates
        .keys()
    {
        if !context.seen_company_ids.contains(company_id) {
            return Err(format!(
                "service_state.annual_finance_last_news_family_candidates references unknown company_id {}",
                company_id
            ));
        }
    }
    for news_event in &state.service_state.annual_finance_last_news_events {
        if !context.seen_company_ids.contains(&news_event.company_id) {
            return Err(format!(
                "service_state.annual_finance_last_news_events references unknown company_id {}",
                news_event.company_id
            ));
        }
    }
    for site_id in state
        .service_state
        .near_city_acquisition_last_news_family_candidates
        .keys()
    {
        if !context.seen_near_city_site_ids.contains(site_id) {
            return Err(format!(
                "service_state.near_city_acquisition_last_news_family_candidates references unknown site_id {}",
                site_id
            ));
        }
    }
    for news_event in &state.service_state.near_city_acquisition_last_news_events {
        if !context.seen_company_ids.contains(&news_event.company_id) {
            return Err(format!(
                "service_state.near_city_acquisition_last_news_events references unknown company_id {}",
                news_event.company_id
            ));
        }
        if !context
            .seen_near_city_site_ids
            .contains(&news_event.site_id)
        {
            return Err(format!(
                "service_state.near_city_acquisition_last_news_events references unknown site_id {}",
                news_event.site_id
            ));
        }
    }
    for chairman_profile_id in state
        .service_state
        .chairman_issue_opinion_terms_raw_i32
        .keys()
    {
        if !context
            .seen_chairman_profile_ids
            .contains(chairman_profile_id)
        {
            return Err(format!(
                "service_state.chairman_issue_opinion_terms_raw_i32 references unknown chairman_profile_id {}",
                chairman_profile_id
            ));
        }
    }
    for chairman_profile_id in state.service_state.chairman_personality_raw_u8.keys() {
        if !context
            .seen_chairman_profile_ids
            .contains(chairman_profile_id)
        {
            return Err(format!(
                "service_state.chairman_personality_raw_u8 references unknown chairman_profile_id {}",
                chairman_profile_id
            ));
        }
    }

    Ok(())
}
