use super::atlas::build_infrastructure_atlas_inputs;
use super::family_scan::analyze_infrastructure_name_pairs;
use super::hypotheses::build_infrastructure_trace_narrative;
use crate::inspect::smp::services::*;

pub(in crate::inspect::smp) fn build_infrastructure_asset_trace_report(
    analysis: &SmpSaveCompanyChairmanAnalysisReport,
) -> SmpInfrastructureAssetTraceReport {
    let scan = analyze_infrastructure_name_pairs(analysis);
    let atlas = build_infrastructure_atlas_inputs();
    let narrative = build_infrastructure_trace_narrative(analysis, &scan);
    let side_buffer = analysis.placed_structure_dynamic_side_buffer.as_ref();
    let alignment = analysis
        .placed_structure_dynamic_side_buffer_alignment
        .as_ref();

    SmpInfrastructureAssetTraceReport {
        profile_family: analysis.profile_family.clone(),
        placed_structure_collection_header_present: analysis
            .placed_structure_collection_header
            .is_some(),
        placed_structure_record_triplet_count: analysis
            .placed_structure_record_triplets
            .as_ref()
            .map(|probe| probe.record_count)
            .unwrap_or_default(),
        side_buffer_present: side_buffer.is_some(),
        side_buffer_decoded_embedded_name_row_count: side_buffer
            .map(|probe| probe.decoded_embedded_name_row_count)
            .unwrap_or_default(),
        side_buffer_unique_name_pair_count: side_buffer
            .map(|probe| probe.unique_embedded_name_pair_count)
            .unwrap_or_default(),
        bridge_like_name_pair_count: scan.bridge_like_name_pair_count,
        tunnel_like_name_pair_count: scan.tunnel_like_name_pair_count,
        track_cap_like_name_pair_count: scan.track_cap_like_name_pair_count,
        triplet_alignment_overlap_count: alignment
            .map(|probe| probe.overlapping_name_pair_count)
            .unwrap_or_default(),
        atlas_candidate_consumers: atlas.atlas_candidate_consumers,
        known_owner_bridge_fields: atlas.known_owner_bridge_fields,
        known_bridge_helpers: atlas.known_bridge_helpers,
        next_owner_questions: atlas.next_owner_questions,
        candidate_consumer_hypotheses: narrative.candidate_consumer_hypotheses,
        branches: narrative.branches,
        notes: narrative.notes,
    }
}
