use crate::state::RuntimeState;

pub(super) struct PackedEventBlockedOutcomeCounts {
    pub packed_event_blocked_missing_company_context_count: usize,
    pub packed_event_blocked_missing_selection_context_count: usize,
    pub packed_event_blocked_missing_company_role_context_count: usize,
    pub packed_event_blocked_missing_player_context_count: usize,
    pub packed_event_blocked_missing_player_selection_context_count: usize,
    pub packed_event_blocked_missing_player_role_context_count: usize,
    pub packed_event_blocked_missing_chairman_context_count: usize,
    pub packed_event_blocked_chairman_target_scope_count: usize,
    pub packed_event_blocked_missing_condition_context_count: usize,
    pub packed_event_blocked_missing_player_condition_context_count: usize,
    pub packed_event_blocked_company_condition_scope_disabled_count: usize,
    pub packed_event_blocked_player_condition_scope_count: usize,
    pub packed_event_blocked_territory_condition_scope_count: usize,
    pub packed_event_blocked_missing_territory_context_count: usize,
    pub packed_event_blocked_named_territory_binding_count: usize,
    pub packed_event_blocked_unmapped_ordinary_condition_count: usize,
    pub packed_event_blocked_unmapped_world_condition_count: usize,
    pub packed_event_blocked_missing_compact_control_count: usize,
    pub packed_event_blocked_shell_owned_descriptor_count: usize,
    pub packed_event_blocked_evidence_blocked_descriptor_count: usize,
    pub packed_event_blocked_variant_or_scope_blocked_descriptor_count: usize,
    pub packed_event_blocked_unmapped_real_descriptor_count: usize,
    pub packed_event_blocked_unmapped_world_descriptor_count: usize,
    pub packed_event_blocked_territory_access_variant_count: usize,
    pub packed_event_blocked_territory_access_scope_count: usize,
    pub packed_event_blocked_missing_train_context_count: usize,
    pub packed_event_blocked_missing_train_territory_context_count: usize,
    pub packed_event_blocked_missing_locomotive_catalog_context_count: usize,
    pub packed_event_blocked_confiscation_variant_count: usize,
    pub packed_event_blocked_retire_train_variant_count: usize,
    pub packed_event_blocked_retire_train_scope_count: usize,
    pub packed_event_blocked_structural_only_count: usize,
}

pub(super) fn build_packed_event_blocked_outcome_counts(
    state: &RuntimeState,
) -> PackedEventBlockedOutcomeCounts {
    PackedEventBlockedOutcomeCounts {
        packed_event_blocked_missing_company_context_count: count_import_outcomes(
            state,
            "blocked_missing_company_context",
        ),
        packed_event_blocked_missing_selection_context_count: count_import_outcomes(
            state,
            "blocked_missing_selection_context",
        ),
        packed_event_blocked_missing_company_role_context_count: count_import_outcomes(
            state,
            "blocked_missing_company_role_context",
        ),
        packed_event_blocked_missing_player_context_count: count_import_outcomes(
            state,
            "blocked_missing_player_context",
        ),
        packed_event_blocked_missing_player_selection_context_count: count_import_outcomes(
            state,
            "blocked_missing_player_selection_context",
        ),
        packed_event_blocked_missing_player_role_context_count: count_import_outcomes(
            state,
            "blocked_missing_player_role_context",
        ),
        packed_event_blocked_missing_chairman_context_count: count_import_outcomes(
            state,
            "blocked_missing_chairman_context",
        ),
        packed_event_blocked_chairman_target_scope_count: count_import_outcomes(
            state,
            "blocked_chairman_target_scope",
        ),
        packed_event_blocked_missing_condition_context_count: count_import_outcomes(
            state,
            "blocked_missing_condition_context",
        ),
        packed_event_blocked_missing_player_condition_context_count: count_import_outcomes(
            state,
            "blocked_missing_player_condition_context",
        ),
        packed_event_blocked_company_condition_scope_disabled_count: count_import_outcomes(
            state,
            "blocked_company_condition_scope_disabled",
        ),
        packed_event_blocked_player_condition_scope_count: count_import_outcomes(
            state,
            "blocked_player_condition_scope",
        ),
        packed_event_blocked_territory_condition_scope_count: count_import_outcomes(
            state,
            "blocked_territory_condition_scope",
        ),
        packed_event_blocked_missing_territory_context_count: count_import_outcomes(
            state,
            "blocked_missing_territory_context",
        ),
        packed_event_blocked_named_territory_binding_count: count_import_outcomes(
            state,
            "blocked_named_territory_binding",
        ),
        packed_event_blocked_unmapped_ordinary_condition_count: count_import_outcomes(
            state,
            "blocked_unmapped_ordinary_condition",
        ),
        packed_event_blocked_unmapped_world_condition_count: count_import_outcomes(
            state,
            "blocked_unmapped_world_condition",
        ),
        packed_event_blocked_missing_compact_control_count: count_import_outcomes(
            state,
            "blocked_missing_compact_control",
        ),
        packed_event_blocked_shell_owned_descriptor_count: count_import_outcomes(
            state,
            "blocked_shell_owned_descriptor",
        ),
        packed_event_blocked_evidence_blocked_descriptor_count: count_import_outcomes(
            state,
            "blocked_evidence_blocked_descriptor",
        ),
        packed_event_blocked_variant_or_scope_blocked_descriptor_count: count_import_outcomes(
            state,
            "blocked_variant_or_scope_blocked_descriptor",
        ),
        packed_event_blocked_unmapped_real_descriptor_count: count_import_outcomes(
            state,
            "blocked_unmapped_real_descriptor",
        ),
        packed_event_blocked_unmapped_world_descriptor_count: count_import_outcomes(
            state,
            "blocked_unmapped_world_descriptor",
        ),
        packed_event_blocked_territory_access_variant_count: count_import_outcomes(
            state,
            "blocked_territory_access_variant",
        ),
        packed_event_blocked_territory_access_scope_count: count_import_outcomes(
            state,
            "blocked_territory_access_scope",
        ),
        packed_event_blocked_missing_train_context_count: count_import_outcomes(
            state,
            "blocked_missing_train_context",
        ),
        packed_event_blocked_missing_train_territory_context_count: count_import_outcomes(
            state,
            "blocked_missing_train_territory_context",
        ),
        packed_event_blocked_missing_locomotive_catalog_context_count: count_import_outcomes(
            state,
            "blocked_missing_locomotive_catalog_context",
        ),
        packed_event_blocked_confiscation_variant_count: count_import_outcomes(
            state,
            "blocked_confiscation_variant",
        ),
        packed_event_blocked_retire_train_variant_count: count_import_outcomes(
            state,
            "blocked_retire_train_variant",
        ),
        packed_event_blocked_retire_train_scope_count: count_import_outcomes(
            state,
            "blocked_retire_train_scope",
        ),
        packed_event_blocked_structural_only_count: count_import_outcomes(
            state,
            "blocked_structural_only",
        ),
    }
}

fn count_import_outcomes(state: &RuntimeState, outcome: &str) -> usize {
    state
        .packed_event_collection
        .as_ref()
        .map(|summary| {
            summary
                .records
                .iter()
                .filter(|record| record.import_outcome.as_deref() == Some(outcome))
                .count()
        })
        .unwrap_or(0)
}
