use crate::inspect::smp::services::SmpRegionServiceTraceEntry;
use crate::inspect::smp::services::build_service_trace_branch_status;
use crate::inspect::smp::world::SmpSaveCompanyChairmanAnalysisReport;

pub(super) fn build_region_service_trace_entries(
    analysis: &SmpSaveCompanyChairmanAnalysisReport,
) -> Vec<SmpRegionServiceTraceEntry> {
    analysis
        .region_record_triplets
        .as_ref()
        .map(|probe| {
            probe.entries
                .iter()
                .map(|entry| SmpRegionServiceTraceEntry {
                    name: entry.name.clone(),
                    profile_collection_count: entry
                        .profile_collection
                        .as_ref()
                        .map(|collection| collection.live_record_count),
                    policy_leading_f32_0_text: format!("{:.6}", entry.policy_leading_f32_0),
                    policy_leading_f32_1_text: format!("{:.6}", entry.policy_leading_f32_1),
                    policy_leading_f32_2_text: format!("{:.6}", entry.policy_leading_f32_2),
                    policy_reserved_dword_hex_words: entry
                        .policy_reserved_dwords
                        .iter()
                        .map(|word| format!("0x{word:08x}"))
                        .collect(),
                    policy_trailing_word_hex: entry.policy_trailing_word_hex.clone(),
                    branches: vec![
                        build_service_trace_branch_status(
                            "pending_bonus_queue_seed",
                            "blocked_missing_pending_bonus_owner_lane",
                            &[
                                "region triplet envelope",
                                "embedded profile subcollection",
                                "policy float lanes",
                            ],
                            &[
                                "[region+0x276] pending amount lane",
                                "[region+0x25e] severity/source lane",
                            ],
                            &[
                                "0x00422100 periodic class-0 region picker and queue seed owner",
                                "0x004337c0 queued 0x20-byte notice-node append helper",
                            ],
                            &["The queued kind-7 notice family is not obviously persisted in ordinary saves, so the pending queue must be treated as transient until a direct owner seam is found."],
                        ),
                        build_service_trace_branch_status(
                            "city_connection_completion",
                            "blocked_missing_completion_and_one_shot_latches",
                            &[
                                "region triplet envelope",
                                "region name stem",
                            ],
                            &[
                                "[region+0x302] completion latch",
                                "[region+0x316] one-shot notice latch",
                                "stable region id or class discriminator",
                            ],
                            &[
                                "0x004358d0 pending region bonus service owner",
                                "0x00420030 / 0x00420280 city-connection peer probes",
                                "0x0047efe0 placed-structure linked-company resolver",
                            ],
                            &["The remaining region blocker is a separate owner seam for the latches the city-connection branch reads and writes."],
                        ),
                    ],
                })
                .collect::<Vec<_>>()
        })
        .unwrap_or_default()
}
