use crate::inspect::smp::structures::*;
use std::collections::BTreeSet;

pub(in crate::inspect::smp) fn summarize_placed_structure_dynamic_side_buffer_alignment(
    side_buffer: &SmpSavePlacedStructureDynamicSideBufferProbe,
    triplets: &SmpSavePlacedStructureRecordTripletProbe,
) -> SmpSavePlacedStructureDynamicSideBufferAlignmentProbe {
    let triplet_name_pairs = triplets
        .entries
        .iter()
        .map(|entry| (entry.primary_name.clone(), entry.secondary_name.clone()))
        .collect::<BTreeSet<_>>();
    let matched_name_pair_samples = side_buffer
        .name_pair_summaries
        .iter()
        .filter(|summary| {
            triplet_name_pairs
                .contains(&(summary.primary_name.clone(), summary.secondary_name.clone()))
        })
        .take(5)
        .cloned()
        .collect::<Vec<_>>();
    let unmatched_side_buffer_name_pair_samples = side_buffer
        .name_pair_summaries
        .iter()
        .filter(|summary| {
            !triplet_name_pairs
                .contains(&(summary.primary_name.clone(), summary.secondary_name.clone()))
        })
        .take(5)
        .cloned()
        .collect::<Vec<_>>();
    let side_buffer_rows_with_matching_triplet_name_pair_count = side_buffer
        .name_pair_summaries
        .iter()
        .filter(|summary| {
            triplet_name_pairs
                .contains(&(summary.primary_name.clone(), summary.secondary_name.clone()))
        })
        .map(|summary| summary.count)
        .sum::<usize>();
    let unique_side_buffer_name_pair_count = side_buffer.name_pair_summaries.len();
    let overlapping_name_pair_count = side_buffer
        .name_pair_summaries
        .iter()
        .filter(|summary| {
            triplet_name_pairs
                .contains(&(summary.primary_name.clone(), summary.secondary_name.clone()))
        })
        .count();
    SmpSavePlacedStructureDynamicSideBufferAlignmentProbe {
        unique_side_buffer_name_pair_count,
        unique_triplet_name_pair_count: triplet_name_pairs.len(),
        overlapping_name_pair_count,
        side_buffer_row_count: side_buffer.decoded_embedded_name_row_count,
        side_buffer_rows_with_matching_triplet_name_pair_count,
        side_buffer_rows_without_matching_triplet_name_pair_count: side_buffer
            .decoded_embedded_name_row_count
            .saturating_sub(side_buffer_rows_with_matching_triplet_name_pair_count),
        triplet_name_pairs_without_side_buffer_match_count: triplet_name_pairs
            .len()
            .saturating_sub(overlapping_name_pair_count),
        matched_name_pair_samples,
        unmatched_side_buffer_name_pair_samples,
        evidence: vec![
            "placed-structure dynamic side-buffer alignment compares decoded 0x38a5 embedded name pairs against the grounded 0x36b1 triplet name-pair corpus".to_string(),
            format!(
                "{} of {} decoded side-buffer rows currently reuse name pairs already present in the placed-structure triplet owner seam",
                side_buffer_rows_with_matching_triplet_name_pair_count,
                side_buffer.decoded_embedded_name_row_count
            ),
            format!(
                "{} of {} unique side-buffer name pairs overlap the grounded triplet name-pair corpus",
                overlapping_name_pair_count,
                unique_side_buffer_name_pair_count
            ),
        ],
    }
}

#[derive(Clone, Copy)]
pub(in crate::inspect::smp) struct IndexedCollectionHeaderSummary {
    pub(in crate::inspect::smp) metadata_tag_offset: usize,
    pub(in crate::inspect::smp) records_tag_offset: usize,
    pub(in crate::inspect::smp) close_tag_offset: usize,
    pub(in crate::inspect::smp) direct_collection_flag: u32,
    pub(in crate::inspect::smp) direct_record_stride: u32,
    pub(in crate::inspect::smp) live_id_bound: u32,
    pub(in crate::inspect::smp) live_record_count: u32,
    pub(in crate::inspect::smp) header_words:
        [u32; INDEXED_COLLECTION_SERIALIZED_HEADER_DWORD_COUNT],
}
