use crate::inspect::smp::structures::*;

pub(super) struct DynamicSideBufferEvidenceInputs<'a> {
    pub(super) owner_shared_dword: u32,
    pub(super) first_record_child_count_after_owner_shared: Option<u16>,
    pub(super) first_record_saved_primary_child_byte_after_owner_shared_hex: Option<&'a str>,
    pub(super) first_record_first_name_tag_relative_offset_after_owner_shared: Option<usize>,
    pub(super) first_embedded_primary_name: &'a str,
    pub(super) first_embedded_secondary_name: &'a str,
    pub(super) first_embedded_tertiary_name: Option<&'a String>,
    pub(super) embedded_name_tag_count: usize,
    pub(super) embedded_name_row_count: usize,
    pub(super) prefix_leading_dword_matching_embedded_profile_tag_count: usize,
    pub(super) decoded_embedded_name_row_count: usize,
    pub(super) unique_embedded_name_pair_count: usize,
    pub(super) decoded_embedded_name_row_with_tertiary_name_count: usize,
    pub(super) row_count_with_complete_0x55f1_0x55f2_0x55f3_envelope: usize,
    pub(super) row_count_missing_policy_tag_before_next_name: usize,
    pub(super) row_count_missing_profile_tag_after_policy: usize,
    pub(super) dominant_policy_chunk_len: Option<(usize, usize)>,
    pub(super) dominant_profile_chunk_len: Option<(usize, usize)>,
    pub(super) short_profile_flag_pair_summary:
        Option<&'a SmpSavePlacedStructureDynamicSideBufferShortProfileFlagPairSummary>,
    pub(super) fixed_policy_summary:
        Option<&'a SmpSavePlacedStructureDynamicSideBufferFixedPolicySummary>,
    pub(super) live_entry_prelude_summary:
        Option<&'a SmpSavePlacedStructureDynamicSideBufferLiveEntryPreludeSummary>,
    pub(super) dominant_compact_prefix_pattern:
        Option<&'a SmpSavePlacedStructureDynamicSideBufferPrefixPatternSummary>,
}

pub(super) fn build_dynamic_side_buffer_evidence(
    inputs: DynamicSideBufferEvidenceInputs<'_>,
) -> Vec<String> {
    vec![
        "exact little-endian u32 tag family 0x38a5/0x38a6/0x38a7 appears as a separate save-side tagged collection on grounded saves".to_string(),
        format!(
            "direct disassembly now shows 0x00493be0 consuming shared owner-local dword 0x{:08x} from the 0x38a6 stream before iterating live infrastructure records",
            inputs.owner_shared_dword
        ),
        format!(
            "grounded 0x38a6 record stream then starts the first infrastructure payload with child_count={}, saved_primary_child_byte={}, and first 0x55f1 at relative offset {:?} after that shared dword",
            inputs.first_record_child_count_after_owner_shared.unwrap_or_default(),
            inputs
                .first_record_saved_primary_child_byte_after_owner_shared_hex
                .unwrap_or("0x00"),
            inputs.first_record_first_name_tag_relative_offset_after_owner_shared
        ),
        "records payload begins with a compact 6-byte prefix plus one separator byte before the first embedded 0x55f1 name row".to_string(),
        "first embedded 0x55f1 row decodes with placed-structure-style dual names, which makes this the strongest current candidate for the separate placed-structure dynamic side-buffer owner seam".to_string(),
        format!(
            "grounded first embedded names are {:?}/{:?}/{:?} with {} embedded 0x55f1 name rows in the tagged records span",
            Some(inputs.first_embedded_primary_name),
            Some(inputs.first_embedded_secondary_name),
            inputs.first_embedded_tertiary_name,
            inputs.embedded_name_tag_count
        ),
        format!(
            "{} of {} embedded name rows use compact leading dword 0x{:08x}, matching the placed-structure embedded profile tag",
            inputs.prefix_leading_dword_matching_embedded_profile_tag_count,
            inputs.embedded_name_row_count,
            u32::from(SAVE_REGION_RECORD_PROFILE_TAG)
        ),
        format!(
            "{} decoded embedded name rows collapse into {} unique placed-structure name pairs; {} rows also expose a third embedded 0x55f1 string",
            inputs.decoded_embedded_name_row_count,
            inputs.unique_embedded_name_pair_count,
            inputs.decoded_embedded_name_row_with_tertiary_name_count
        ),
        format!(
            "{} of {} embedded 0x55f1 rows currently have a complete 0x55f1/0x55f2/0x55f3 envelope before the next name row; {} rows are still missing 0x55f2 and {} rows have 0x55f2 without a later 0x55f3",
            inputs.row_count_with_complete_0x55f1_0x55f2_0x55f3_envelope,
            inputs.embedded_name_row_count,
            inputs.row_count_missing_policy_tag_before_next_name,
            inputs.row_count_missing_profile_tag_after_policy
        ),
        inputs
            .dominant_policy_chunk_len
            .map(|(policy_chunk_len, count)| {
                format!(
                    "dominant embedded 0x55f2 policy chunk length is 0x{policy_chunk_len:x} bytes across {count} rows"
                )
            })
            .unwrap_or_else(|| "no dominant embedded 0x55f2 policy chunk length was available".to_string()),
        inputs
            .dominant_profile_chunk_len
            .map(|(profile_chunk_len, count)| {
                format!(
                    "dominant embedded 0x55f3 payload-to-next-name span is 0x{profile_chunk_len:x} bytes across {count} rows"
                )
            })
            .unwrap_or_else(|| "no dominant embedded 0x55f3 payload-to-next-name span was available".to_string()),
        inputs
            .short_profile_flag_pair_summary
            .and_then(|summary| summary.dominant_flag_pair.as_ref())
            .map(|pair| {
                format!(
                    "direct disassembly now bounds the short trailing lane through 0x52ebd0/0x52ec50 as two serialized flag bytes folded into [this+0x20] bits 0x20/0x40; grounded 0x06-byte rows currently favor {}/{} across {} rows ({} rows total with the short span)",
                    pair.first_flag_byte_hex,
                    pair.second_flag_byte_hex,
                    pair.count,
                    inputs
                        .short_profile_flag_pair_summary
                        .map(|summary| summary.row_count_with_0x06_profile_span)
                        .unwrap_or_default()
                )
            })
            .unwrap_or_else(|| "no dominant short trailing flag-byte pair was available".to_string()),
        inputs
            .fixed_policy_summary
            .map(|summary| {
                format!(
                    "direct disassembly now bounds the fixed 0x55f2 lane through 0x455870/0x455930 as six serialized dwords plus one trailing u16; grounded rows currently keep trailing word {} across {} of {} fixed-policy rows",
                    summary
                        .dominant_trailing_word_hex
                        .as_deref()
                        .unwrap_or("0x0000"),
                    summary.dominant_trailing_word_count,
                    summary.row_count_with_0x1a_policy_chunk
                )
            })
            .unwrap_or_else(|| "no fixed 0x55f2 policy summary was available".to_string()),
        inputs
            .live_entry_prelude_summary
            .map(|summary| {
                format!(
                    "decoded {} live-entry directory rows with payload pointers inside the records span; dominant child count={} x{}, dominant saved primary-child byte={} x{}, dominant first 0x55f1 offset={} x{}, and {} rows start the first child callback immediately at payload +0x3",
                    summary.rows_with_payload_pointer_inside_records_span,
                    summary.dominant_child_count.unwrap_or_default(),
                    summary.dominant_child_count_count,
                    summary
                        .dominant_saved_primary_child_byte_hex
                        .as_deref()
                        .unwrap_or("0x00"),
                    summary.dominant_saved_primary_child_byte_count,
                    summary
                        .dominant_first_name_tag_relative_offset
                        .unwrap_or_default(),
                    summary.dominant_first_name_tag_relative_offset_count,
                    summary.rows_with_first_name_tag_at_offset_3
                )
            })
            .unwrap_or_else(|| "no live-entry prelude summary was available".to_string()),
        inputs
            .dominant_compact_prefix_pattern
            .map(|pattern| {
                format!(
                    "dominant compact prefix pattern {} / {} / {} occurs {} times; section-like rows={}, cap-like rows={}, first names={:?}/{:?}",
                    pattern.prefix_leading_dword_hex,
                    pattern.prefix_trailing_word_hex,
                    pattern.prefix_separator_byte_hex,
                    pattern.count,
                    pattern.section_like_primary_name_count,
                    pattern.cap_like_primary_name_count,
                    pattern.first_primary_name,
                    pattern.first_secondary_name
                )
            })
            .unwrap_or_else(|| "no dominant compact prefix pattern summary was available".to_string()),
    ]
}
