use crate::inspect::smp::services::*;

pub(super) fn build_save_side_corpus_evidence(
    analysis: &SmpSaveCompanyChairmanAnalysisReport,
    scan: &super::super::super::family_scan::InfrastructureFamilyScan,
) -> Vec<String> {
    let side_buffer = analysis.placed_structure_dynamic_side_buffer.as_ref();
    let bridge_like_name_pair_count = scan.bridge_like_name_pair_count;
    let tunnel_like_name_pair_count = scan.tunnel_like_name_pair_count;
    let track_cap_like_name_pair_count = scan.track_cap_like_name_pair_count;

    vec![
        format!(
            "real side-buffer name families currently count bridge/tunnel/track-cap pairs as {}/{}/{}",
            bridge_like_name_pair_count,
            tunnel_like_name_pair_count,
            track_cap_like_name_pair_count
        ),
        side_buffer
            .and_then(|probe| probe.first_record_child_count_after_owner_shared)
            .map(|child_count| {
                format!(
                    "grounded q.gms bytes now also show the first 0x38a6 record starting immediately after that shared dword with child_count={}, saved_primary_child_byte={}, and first 0x55f1 at offset +0x{:x}",
                    child_count,
                    side_buffer
                        .and_then(|probe| {
                            probe.first_record_saved_primary_child_byte_after_owner_shared_hex
                                .as_deref()
                        })
                        .unwrap_or("0x00"),
                    side_buffer
                        .and_then(|probe| {
                            probe.first_record_first_name_tag_relative_offset_after_owner_shared
                        })
                        .unwrap_or_default()
                )
            })
            .unwrap_or_else(|| {
                "no grounded first-record prelude summary was available after the shared 0x38a6 owner dword".to_string()
            }),
        format!(
            "current save-side probe reports {} embedded 0x55f1 rows with a third decoded string",
            side_buffer
                .map(|probe| probe.decoded_embedded_name_row_with_tertiary_name_count)
                .unwrap_or_default()
        ),
        format!(
            "current save-side probe also reports {} complete 0x55f1/0x55f2/0x55f3 envelopes, dominant 0x55f2 chunk len 0x{:x}, and dominant 0x55f3 span 0x{:x}",
            side_buffer
                .and_then(|probe| probe.payload_envelope_summary.as_ref())
                .map(|summary| summary.row_count_with_complete_0x55f1_0x55f2_0x55f3_envelope)
                .unwrap_or_default(),
            side_buffer
                .and_then(|probe| probe.payload_envelope_summary.as_ref())
                .and_then(|summary| summary.dominant_policy_chunk_len)
                .unwrap_or_default(),
            side_buffer
                .and_then(|probe| probe.payload_envelope_summary.as_ref())
                .and_then(|summary| summary.dominant_profile_chunk_len)
                .unwrap_or_default()
        ),
        format!(
            "current save-side probe reports {} rows with the short 0x06-byte trailing span; dominant short flag pair is {}/{} x{}",
            side_buffer
                .and_then(|probe| probe.payload_envelope_summary.as_ref())
                .and_then(|summary| summary.short_profile_flag_pair_summary.as_ref())
                .map(|summary| summary.row_count_with_0x06_profile_span)
                .unwrap_or_default(),
            side_buffer
                .and_then(|probe| probe.payload_envelope_summary.as_ref())
                .and_then(|summary| summary.short_profile_flag_pair_summary.as_ref())
                .and_then(|summary| summary.dominant_flag_pair.as_ref())
                .map(|pair| pair.first_flag_byte_hex.as_str())
                .unwrap_or("0x00"),
            side_buffer
                .and_then(|probe| probe.payload_envelope_summary.as_ref())
                .and_then(|summary| summary.short_profile_flag_pair_summary.as_ref())
                .and_then(|summary| summary.dominant_flag_pair.as_ref())
                .map(|pair| pair.second_flag_byte_hex.as_str())
                .unwrap_or("0x00"),
            side_buffer
                .and_then(|probe| probe.payload_envelope_summary.as_ref())
                .and_then(|summary| summary.short_profile_flag_pair_summary.as_ref())
                .and_then(|summary| summary.dominant_flag_pair.as_ref())
                .map(|pair| pair.count)
                .unwrap_or_default()
        ),
        format!(
            "current save-side probe reports {} fixed 0x1a policy rows; dominant trailing word is {} x{}",
            side_buffer
                .and_then(|probe| probe.payload_envelope_summary.as_ref())
                .and_then(|summary| summary.fixed_policy_summary.as_ref())
                .map(|summary| summary.row_count_with_0x1a_policy_chunk)
                .unwrap_or_default(),
            side_buffer
                .and_then(|probe| probe.payload_envelope_summary.as_ref())
                .and_then(|summary| summary.fixed_policy_summary.as_ref())
                .and_then(|summary| summary.dominant_trailing_word_hex.as_deref())
                .unwrap_or("0x0000"),
            side_buffer
                .and_then(|probe| probe.payload_envelope_summary.as_ref())
                .and_then(|summary| summary.fixed_policy_summary.as_ref())
                .map(|summary| summary.dominant_trailing_word_count)
                .unwrap_or_default()
        ),
        side_buffer
            .and_then(|probe| probe.payload_envelope_summary.as_ref())
            .and_then(|summary| summary.dominant_profile_span_class_summary.as_ref())
            .map(|summary| {
                format!(
                    "the dominant 0x{:x}-byte post-profile class is now narrowed too: dominant name pair is {:?}/{:?} x{}, dominant compact prefix is {}/{}/{} x{}, and dominant prelude candidate is {}/{} x{} across {} rows",
                    summary.profile_chunk_len_to_next_name_or_end,
                    summary.dominant_primary_name,
                    summary.dominant_secondary_name,
                    summary.dominant_name_pair_count,
                    summary
                        .dominant_prefix_leading_dword_hex
                        .as_deref()
                        .unwrap_or("0x00000000"),
                    summary
                        .dominant_prefix_trailing_word_hex
                        .as_deref()
                        .unwrap_or("0x0000"),
                    summary
                        .dominant_prefix_separator_byte_hex
                        .as_deref()
                        .unwrap_or("0x00"),
                    summary.dominant_prefix_count,
                    summary
                        .dominant_candidate_pattern
                        .as_ref()
                        .map(|pattern| pattern.child_count_candidate_hex.as_str())
                        .unwrap_or("0x0000"),
                    summary
                        .dominant_candidate_pattern
                        .as_ref()
                        .map(|pattern| pattern.saved_primary_child_byte_candidate_hex.as_str())
                        .unwrap_or("0x00"),
                    summary
                        .dominant_candidate_pattern
                        .as_ref()
                        .map(|pattern| pattern.count)
                        .unwrap_or_default(),
                    summary.row_count
                )
            })
            .unwrap_or_else(|| {
                "no dominant post-profile class summary was available for the embedded 0x55f3 spans".to_string()
            }),
        side_buffer
            .and_then(|probe| probe.payload_envelope_summary.as_ref())
            .and_then(|summary| summary.dominant_profile_span_class_summary.as_ref())
            .map(|summary| {
                format!(
                    "the dominant post-profile outliers are now explicit too: name-pair counts={:?}, compact-prefix counts={:?}, candidate-pattern counts={:?}",
                    summary
                        .name_pair_summaries
                        .iter()
                        .map(|entry| format!(
                            "{:?}/{:?}:{}",
                            entry.primary_name, entry.secondary_name, entry.count
                        ))
                        .collect::<Vec<_>>(),
                    summary
                        .compact_prefix_pattern_summaries
                        .iter()
                        .map(|entry| format!(
                            "{}/{}/{}:{}",
                            entry.prefix_leading_dword_hex,
                            entry.prefix_trailing_word_hex,
                            entry.prefix_separator_byte_hex,
                            entry.count
                        ))
                        .collect::<Vec<_>>(),
                    summary
                        .candidate_pattern_summaries
                        .iter()
                        .map(|entry| format!(
                            "{}/{}:{}",
                            entry.child_count_candidate_hex,
                            entry.saved_primary_child_byte_candidate_hex,
                            entry.count
                        ))
                        .collect::<Vec<_>>()
                )
            })
            .unwrap_or_else(|| {
                "no dominant post-profile outlier breakdown was available".to_string()
            }),
        side_buffer
            .and_then(|probe| probe.payload_envelope_summary.as_ref())
            .and_then(|summary| summary.name_prelude_candidate_summary.as_ref())
            .map(|summary| {
                format!(
                    "candidate-pattern correlations now split the remaining prelude classes cleanly too: {:?}",
                    summary
                        .candidate_pattern_correlations
                        .iter()
                        .map(|entry| format!(
                            "{}/{} rows={} dominant-name={:?}/{:?} x{} dominant-prev-span={:?} x{}",
                            entry.child_count_candidate_hex,
                            entry.saved_primary_child_byte_candidate_hex,
                            entry.row_count,
                            entry.dominant_primary_name,
                            entry.dominant_secondary_name,
                            entry.dominant_name_pair_count,
                            entry.dominant_profile_span,
                            entry.dominant_profile_span_count
                        ))
                        .collect::<Vec<_>>()
                )
            })
            .unwrap_or_else(|| {
                "no candidate-pattern correlation summary was available".to_string()
            }),
        side_buffer
            .and_then(|probe| probe.payload_envelope_summary.as_ref())
            .and_then(|summary| summary.name_prelude_candidate_summary.as_ref())
            .map(|summary| {
                format!(
                    "mode-family correlations now also split the candidate patterns directly: {:?}",
                    summary
                        .candidate_pattern_correlations
                        .iter()
                        .map(|entry| format!(
                            "{}/{} rows={} dominant-mode={:?} x{} mode-counts={:?}",
                            entry.child_count_candidate_hex,
                            entry.saved_primary_child_byte_candidate_hex,
                            entry.row_count,
                            entry.dominant_mode_family,
                            entry.dominant_mode_family_count,
                            entry
                                .mode_family_counts
                                .iter()
                                .map(|mode| format!("{}:{}", mode.mode_family, mode.count))
                                .collect::<Vec<_>>()
                        ))
                        .collect::<Vec<_>>()
                )
            })
            .unwrap_or_else(|| {
                "no mode-family correlation summary was available for the prelude candidates"
                    .to_string()
            }),
        side_buffer
            .and_then(|probe| probe.payload_envelope_summary.as_ref())
            .and_then(|summary| summary.name_prelude_candidate_summary.as_ref())
            .map(|summary| {
                format!(
                    "profile-span mode-family correlations now also split the previous 0x55f3 spans directly: {:?}",
                    summary
                        .profile_span_correlations
                        .iter()
                        .map(|entry| format!(
                            "span=0x{:x} rows={} dominant-mode={:?} x{} mode-counts={:?}",
                            entry.previous_profile_chunk_len_to_next_name_or_end,
                            entry.row_count,
                            entry.dominant_mode_family,
                            entry.dominant_mode_family_count,
                            entry
                                .mode_family_counts
                                .iter()
                                .map(|mode| format!("{}:{}", mode.mode_family, mode.count))
                                .collect::<Vec<_>>()
                        ))
                        .collect::<Vec<_>>()
                )
            })
            .unwrap_or_else(|| {
                "no profile-span mode-family correlation summary was available".to_string()
            }),
        side_buffer
            .and_then(|probe| probe.payload_envelope_summary.as_ref())
            .and_then(|summary| summary.name_prelude_candidate_summary.as_ref())
            .map(|summary| {
                format!(
                    "exact compact-prefix correlations now split the residual prelude classes directly: {:?}",
                    summary
                        .compact_prefix_correlations
                        .iter()
                        .map(|entry| format!(
                            "{}/{}/{} rows={} dominant-name={:?}/{:?} x{} dominant-span={:?} x{} dominant-candidate={:?} dominant-mode={:?} x{}",
                            entry.prefix_leading_dword_hex,
                            entry.prefix_trailing_word_hex,
                            entry.prefix_separator_byte_hex,
                            entry.row_count,
                            entry.dominant_primary_name,
                            entry.dominant_secondary_name,
                            entry.dominant_name_pair_count,
                            entry.dominant_profile_span,
                            entry.dominant_profile_span_count,
                            entry
                                .dominant_candidate_pattern
                                .as_ref()
                                .map(|pattern| format!(
                                    "{}/{}:{}",
                                    pattern.child_count_candidate_hex,
                                    pattern.saved_primary_child_byte_candidate_hex,
                                    pattern.count
                                )),
                            entry.dominant_mode_family,
                            entry.dominant_mode_family_count
                        ))
                        .collect::<Vec<_>>()
                )
            })
            .unwrap_or_else(|| {
                "no compact-prefix correlation summary was available for the prelude candidates"
                    .to_string()
            }),
        side_buffer
            .and_then(|probe| probe.payload_envelope_summary.as_ref())
            .and_then(|summary| summary.name_prelude_candidate_summary.as_ref())
            .and_then(|summary| {
                summary.candidate_pattern_correlations.iter().find(|entry| {
                    entry.child_count_candidate == 2
                        && entry.saved_primary_child_byte_candidate == 0xff
                        && entry.dominant_primary_name.as_deref()
                            == Some("BridgeSTWood_Section.3dp")
                        && entry.dominant_secondary_name.as_deref() == Some("Infrastructure")
                })
            })
            .map(|correlation| {
                format!(
                    "the bridge-only two-child class is now grounded save-side too: candidate pattern {}/{} spans {} rows, stays pure {:?}/{:?}, and the dominant prior profile span is {:?} x{}",
                    correlation.child_count_candidate_hex,
                    correlation.saved_primary_child_byte_candidate_hex,
                    correlation.row_count,
                    correlation.dominant_primary_name,
                    correlation.dominant_secondary_name,
                    correlation.dominant_profile_span,
                    correlation.dominant_profile_span_count
                )
            })
            .unwrap_or_else(|| {
                "no grounded pure bridge-only two-child candidate class was available in the prelude correlations".to_string()
            }),
        side_buffer
            .and_then(|probe| probe.payload_envelope_summary.as_ref())
            .and_then(|summary| summary.name_prelude_candidate_summary.as_ref())
            .and_then(|summary| {
                summary
                    .profile_span_correlations
                    .iter()
                    .find(|row| row.previous_profile_chunk_len_to_next_name_or_end == 3)
            })
            .map(|correlation| {
                format!(
                    "current save-side probe now also shows the short 0x03-byte post-profile gaps collapsing cleanly to the next-record prelude: dominant candidate pattern is {}/{} x{} across {} rows, mode counts={:?}, prefix counts={:?}",
                    correlation
                        .dominant_candidate_pattern
                        .as_ref()
                        .map(|pattern| pattern.child_count_candidate_hex.as_str())
                        .unwrap_or("0x0000"),
                    correlation
                        .dominant_candidate_pattern
                        .as_ref()
                        .map(|pattern| pattern.saved_primary_child_byte_candidate_hex.as_str())
                        .unwrap_or("0x00"),
                    correlation
                        .dominant_candidate_pattern
                        .as_ref()
                        .map(|pattern| pattern.count)
                        .unwrap_or_default(),
                    correlation.row_count,
                    correlation
                        .mode_family_counts
                        .iter()
                        .map(|mode| format!("{}:{}", mode.mode_family, mode.count))
                        .collect::<Vec<_>>(),
                    correlation
                        .compact_prefix_pattern_summaries
                        .iter()
                        .map(|prefix| format!(
                            "{}/{}/{}:{}",
                            prefix.prefix_leading_dword_hex,
                            prefix.prefix_trailing_word_hex,
                            prefix.prefix_separator_byte_hex,
                            prefix.count
                        ))
                        .collect::<Vec<_>>()
                )
            })
            .unwrap_or_else(|| {
                "no grounded short post-profile gap correlation was available".to_string()
            }),
        side_buffer
            .and_then(|probe| probe.payload_envelope_summary.as_ref())
            .and_then(|summary| summary.name_prelude_candidate_summary.as_ref())
            .and_then(|summary| {
                summary
                    .profile_span_correlations
                    .iter()
                    .find(|row| row.previous_profile_chunk_len_to_next_name_or_end == 0x27)
            })
            .map(|correlation| {
                format!(
                    "the sparse 0x27 post-profile outlier is explicit too: mode counts={:?}, prefix counts={:?}, sample names={:?}",
                    correlation
                        .mode_family_counts
                        .iter()
                        .map(|mode| format!("{}:{}", mode.mode_family, mode.count))
                        .collect::<Vec<_>>(),
                    correlation
                        .compact_prefix_pattern_summaries
                        .iter()
                        .map(|prefix| format!(
                            "{}/{}/{}:{}",
                            prefix.prefix_leading_dword_hex,
                            prefix.prefix_trailing_word_hex,
                            prefix.prefix_separator_byte_hex,
                            prefix.count
                        ))
                        .collect::<Vec<_>>(),
                    correlation
                        .sample_rows
                        .iter()
                        .map(|row| format!("{:?}/{:?}", row.primary_name, row.secondary_name))
                        .collect::<Vec<_>>()
                )
            })
            .unwrap_or_else(|| "no sparse 0x27 post-profile outlier was available".to_string()),
        side_buffer
            .and_then(|probe| probe.payload_envelope_summary.as_ref())
            .and_then(|summary| summary.name_prelude_candidate_summary.as_ref())
            .and_then(|summary| {
                summary.compact_prefix_correlations.iter().find(|entry| {
                    entry.prefix_leading_dword == 0xff00_00ff
                        && entry.prefix_trailing_word == 0x0001
                        && entry.prefix_separator_byte == 0xff
                })
            })
            .map(|correlation| {
                format!(
                    "the 0xff0000ff/0x0001/0xff compact-prefix class is now explicit: dominant prelude={}/{} dominant-name={:?}/{:?} dominant-span={:?}",
                    correlation
                        .dominant_candidate_pattern
                        .as_ref()
                        .map(|pattern| pattern.child_count_candidate_hex.as_str())
                        .unwrap_or("0x0000"),
                    correlation
                        .dominant_candidate_pattern
                        .as_ref()
                        .map(|pattern| pattern.saved_primary_child_byte_candidate_hex.as_str())
                        .unwrap_or("0x00"),
                    correlation.dominant_primary_name,
                    correlation.dominant_secondary_name,
                    correlation.dominant_profile_span
                )
            })
            .unwrap_or_else(|| {
                "no 0xff0000ff/0x0001/0xff compact-prefix class summary was available"
                    .to_string()
            }),
        side_buffer
            .and_then(|probe| probe.payload_envelope_summary.as_ref())
            .and_then(|summary| summary.name_prelude_candidate_summary.as_ref())
            .and_then(|summary| {
                summary.compact_prefix_correlations.iter().find(|entry| {
                    entry.prefix_leading_dword == 0x0000_55f3
                        && entry.prefix_trailing_word == 0x0001
                        && entry.prefix_separator_byte == 0xff
                })
            })
            .map(|correlation| {
                format!(
                    "the 0x000055f3/0x0001/0xff compact-prefix class is now explicit: dominant prelude={}/{} dominant-name={:?}/{:?} dominant-span={:?}",
                    correlation
                        .dominant_candidate_pattern
                        .as_ref()
                        .map(|pattern| pattern.child_count_candidate_hex.as_str())
                        .unwrap_or("0x0000"),
                    correlation
                        .dominant_candidate_pattern
                        .as_ref()
                        .map(|pattern| pattern.saved_primary_child_byte_candidate_hex.as_str())
                        .unwrap_or("0x00"),
                    correlation.dominant_primary_name,
                    correlation.dominant_secondary_name,
                    correlation.dominant_profile_span
                )
            })
            .unwrap_or_else(|| {
                "no 0x000055f3/0x0001/0xff compact-prefix class summary was available"
                    .to_string()
            }),
        side_buffer
            .and_then(|probe| probe.payload_envelope_summary.as_ref())
            .and_then(|summary| summary.name_prelude_candidate_summary.as_ref())
            .and_then(|summary| {
                summary.compact_prefix_correlations.iter().find(|entry| {
                    entry.prefix_leading_dword == 0xff00_00ff
                        && entry.prefix_trailing_word == 0x0002
                        && entry.prefix_separator_byte == 0xff
                })
            })
            .map(|correlation| {
                format!(
                    "the 0xff0000ff/0x0002/0xff compact-prefix class is now explicit: dominant prelude={}/{} dominant-name={:?}/{:?} dominant-span={:?}",
                    correlation
                        .dominant_candidate_pattern
                        .as_ref()
                        .map(|pattern| pattern.child_count_candidate_hex.as_str())
                        .unwrap_or("0x0000"),
                    correlation
                        .dominant_candidate_pattern
                        .as_ref()
                        .map(|pattern| pattern.saved_primary_child_byte_candidate_hex.as_str())
                        .unwrap_or("0x00"),
                    correlation.dominant_primary_name,
                    correlation.dominant_secondary_name,
                    correlation.dominant_profile_span
                )
            })
            .unwrap_or_else(|| {
                "no 0xff0000ff/0x0002/0xff compact-prefix class summary was available"
                    .to_string()
            }),
        "the current grounded q.gms side-buffer name corpus now maps directly onto those constructor families too: BridgeSTWood_Section.3dp aligns with mode 0x01 Bridge, TunnelSTBrick_Cap/Section.3dp with mode 0x02 Tunnel, BallastCapST_Cap.3dp with mode 0x0a BallastCap, and TrackCapST_Cap.3dp with mode 0x0b TrackCap; only the Overpass mode-0x03 family remains static-only in the current save corpus".to_string(),
    ]
}
