use super::super::family_scan::InfrastructureFamilyScan;
use super::InfrastructureTraceNarrative;
use super::follow_on::build_route_local_runtime_follow_on_hypothesis;
use super::notes::build_infrastructure_trace_notes;
use crate::inspect::smp::services::*;

mod attach_rebuild;
mod constructor_families;
mod live_entry_directory;
mod payload_writer;
mod save_side_corpus;

pub(super) fn build_infrastructure_trace_narrative(
    analysis: &SmpSaveCompanyChairmanAnalysisReport,
    scan: &InfrastructureFamilyScan,
) -> InfrastructureTraceNarrative {
    let side_buffer = analysis.placed_structure_dynamic_side_buffer.as_ref();
    let alignment = analysis
        .placed_structure_dynamic_side_buffer_alignment
        .as_ref();
    let st_only_name_pair_corpus = scan.st_only_name_pair_corpus;

    let candidate_consumer_hypotheses = vec![
        attach_rebuild::build_attach_rebuild_hypothesis(analysis, scan),
        payload_writer::build_serializer_load_companion_hypothesis(side_buffer.is_some()),
        build_route_local_runtime_follow_on_hypothesis(side_buffer.is_some()),
    ];
    let branches = vec![
        build_service_trace_branch_status(
            "infrastructure_asset_owner_seam",
            if side_buffer.is_some() {
                "grounded_separate_owner_seam"
            } else {
                "blocked_missing_side_buffer_owner_seam"
            },
            &[
                "0x38a5/0x38a6/0x38a7 tagged family",
                "embedded 0x55f1 dual-name rows",
                "compact 6-byte prefix regimes",
            ],
            if side_buffer.is_some() {
                &[]
            } else {
                &["0x38a5 owner seam"]
            },
            &[
                "0x00493be0 infrastructure tagged side-buffer collection load owner",
                "0x0048dcf0 infrastructure tagged child-stream restore outer owner",
            ],
            &[
                "This seam should be treated as infrastructure-asset state rather than as a compact alias of placed-structure triplets.",
            ],
        ),
        build_service_trace_branch_status(
            "placed_structure_triplet_alias",
            if alignment.is_some_and(|probe| probe.overlapping_name_pair_count == 0) {
                "disproved_by_grounded_probe"
            } else {
                "unresolved"
            },
            &[
                "0x36b1 placed-structure triplet corpus",
                "0x38a5 side-buffer name-pair corpus",
            ],
            &[],
            &[],
            &[
                "Grounded q.gms evidence currently shows zero overlap between the side-buffer name-pair corpus and the placed-structure triplet name-pair corpus.",
            ],
        ),
        build_service_trace_branch_status(
            "city_connection_consumer_mapping",
            "blocked_missing_infrastructure_asset_consumer_mapping",
            &[
                "grounded 0x38a5 owner seam",
                "placed-structure triplet seam",
            ],
            &[
                "higher-layer consumer dispatch mapping",
                "compact prefix regime semantics",
            ],
            &[
                "0x0048a1e0 infrastructure child attach helper",
                "0x0048dd50 infrastructure child rebuild loop",
                "0x00490a3c infrastructure payload attach helper",
            ],
            &[
                "The remaining problem is how higher-layer service code consumes this separate seam, not whether the seam exists.",
            ],
        ),
        build_service_trace_branch_status(
            "linked_transit_consumer_mapping",
            "blocked_missing_infrastructure_asset_consumer_mapping",
            &["grounded 0x38a5 owner seam", "company linked-transit latch"],
            &[
                "side-buffer consumer mapping",
                "route or roster rebuild owner path",
            ],
            &[
                "0x00448a70 / 0x00493660 / 0x0048b660 route and world follow-on family",
                "0x004133b0 placed-structure local-runtime refresh outer owner",
            ],
            &[
                "The next slice should target the consumer path above the side-buffer seam rather than another raw save scan.",
            ],
        ),
    ];
    let notes = build_infrastructure_trace_notes(st_only_name_pair_corpus);
    InfrastructureTraceNarrative {
        candidate_consumer_hypotheses,
        branches,
        notes,
    }
}
