use super::super::super::blockers::{packed_record_import_blocker, runtime_import_blocker_outcome};
use super::super::condition_targets::packed_record_condition_scope_import_blocker;
use super::super::decode::lowered_record_decoded_actions;
use super::variants::{
    record_has_unsupported_chairman_target_scope, record_has_unsupported_confiscation_variant,
    record_has_unsupported_retire_train_scope, record_has_unsupported_retire_train_variant,
    record_has_unsupported_territory_access_scope, record_has_unsupported_territory_access_variant,
};
use super::{
    real_grouped_row_is_evidence_blocked_descriptor_family,
    real_grouped_row_is_shell_owned_descriptor_family,
    real_grouped_row_is_unsupported_executable_descriptor_variant,
    real_grouped_row_is_variant_or_scope_blocked_descriptor_family,
    real_grouped_row_is_world_state_family, record_has_world_state_condition_rows,
};
use crate::documents::{RuntimeImportBlocker, RuntimeImportContext};
use crate::inspect::smp::events::SmpLoadedPackedEventRecordSummary;

pub(crate) fn determine_packed_event_import_outcome(
    record: &SmpLoadedPackedEventRecordSummary,
    runtime_context: &RuntimeImportContext,
    imported: bool,
) -> String {
    if imported {
        return "imported".to_string();
    }
    if record.decode_status == "unsupported_framing" {
        return "blocked_unsupported_decode".to_string();
    }
    if record.payload_family == "real_packed_v1" {
        if record.compact_control.is_none() {
            return "blocked_missing_compact_control".to_string();
        }
        if !record.executable_import_ready {
            if let Err(blocker) = lowered_record_decoded_actions(record, runtime_context) {
                if matches!(
                    blocker,
                    RuntimeImportBlocker::MissingLocomotiveCatalogContext
                        | RuntimeImportBlocker::MissingChairmanContext
                        | RuntimeImportBlocker::ChairmanTargetScope
                ) {
                    return runtime_import_blocker_outcome(blocker).to_string();
                }
            }
            if record_has_unsupported_chairman_target_scope(record) {
                return "blocked_chairman_target_scope".to_string();
            }
            if record_has_unsupported_territory_access_scope(record) {
                return "blocked_territory_access_scope".to_string();
            }
            if record_has_unsupported_territory_access_variant(record) {
                return "blocked_territory_access_variant".to_string();
            }
            if record_has_unsupported_confiscation_variant(record) {
                return "blocked_confiscation_variant".to_string();
            }
            if record_has_unsupported_retire_train_scope(record) {
                return "blocked_retire_train_scope".to_string();
            }
            if record_has_unsupported_retire_train_variant(record) {
                return "blocked_retire_train_variant".to_string();
            }
            if record
                .standalone_condition_rows
                .iter()
                .any(|row| row.raw_condition_id >= 0)
            {
                if record_has_world_state_condition_rows(record) {
                    return "blocked_unmapped_world_condition".to_string();
                } else {
                    return "blocked_unmapped_ordinary_condition".to_string();
                }
            }
            if record
                .grouped_effect_rows
                .iter()
                .any(real_grouped_row_is_shell_owned_descriptor_family)
            {
                return "blocked_shell_owned_descriptor".to_string();
            }
            if record
                .grouped_effect_rows
                .iter()
                .any(real_grouped_row_is_evidence_blocked_descriptor_family)
            {
                return "blocked_evidence_blocked_descriptor".to_string();
            }
            if record
                .grouped_effect_rows
                .iter()
                .any(real_grouped_row_is_variant_or_scope_blocked_descriptor_family)
            {
                return "blocked_variant_or_scope_blocked_descriptor".to_string();
            }
            if record
                .grouped_effect_rows
                .iter()
                .any(real_grouped_row_is_unsupported_executable_descriptor_variant)
            {
                return "blocked_variant_or_scope_blocked_descriptor".to_string();
            }
            if record
                .grouped_effect_rows
                .iter()
                .any(real_grouped_row_is_world_state_family)
            {
                return "blocked_unmapped_world_descriptor".to_string();
            }
            return "blocked_unmapped_real_descriptor".to_string();
        }
        if let Some(blocker) = packed_record_condition_scope_import_blocker(record, runtime_context)
        {
            return runtime_import_blocker_outcome(blocker).to_string();
        }
        if let Some(blocker) = packed_record_import_blocker(record, runtime_context) {
            return runtime_import_blocker_outcome(blocker).to_string();
        }
        return "blocked_unsupported_decode".to_string();
    }
    if let Some(blocker) = packed_record_import_blocker(record, runtime_context) {
        return runtime_import_blocker_outcome(blocker).to_string();
    }
    "blocked_unsupported_decode".to_string()
}
