use crate::state::RuntimeState;

pub(super) struct PackedEventCollectionCounts {
    pub packed_event_collection_present: bool,
    pub packed_event_record_count: usize,
    pub packed_event_decoded_record_count: usize,
    pub packed_event_imported_runtime_record_count: usize,
    pub packed_event_parity_only_record_count: usize,
    pub packed_event_unsupported_record_count: usize,
}

pub(super) fn build_packed_event_collection_counts(
    state: &RuntimeState,
) -> PackedEventCollectionCounts {
    PackedEventCollectionCounts {
        packed_event_collection_present: state.packed_event_collection.is_some(),
        packed_event_record_count: state
            .packed_event_collection
            .as_ref()
            .map(|summary| summary.live_record_count)
            .unwrap_or(0),
        packed_event_decoded_record_count: state
            .packed_event_collection
            .as_ref()
            .map(|summary| summary.decoded_record_count)
            .unwrap_or(0),
        packed_event_imported_runtime_record_count: state
            .packed_event_collection
            .as_ref()
            .map(|summary| summary.imported_runtime_record_count)
            .unwrap_or(0),
        packed_event_parity_only_record_count: state
            .packed_event_collection
            .as_ref()
            .map(|summary| {
                summary
                    .records
                    .iter()
                    .filter(|record| record.decode_status == "parity_only")
                    .count()
            })
            .unwrap_or(0),
        packed_event_unsupported_record_count: state
            .packed_event_collection
            .as_ref()
            .map(|summary| {
                summary
                    .records
                    .iter()
                    .filter(|record| record.decode_status == "unsupported_framing")
                    .count()
            })
            .unwrap_or(0),
    }
}
