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

pub(super) fn build_periodic_company_service_entries(
    analysis: &SmpSaveCompanyChairmanAnalysisReport,
) -> Vec<SmpPeriodicCompanyServiceTraceEntry> {
    analysis
        .company_entries
        .iter()
        .map(|entry| {
            let mut branches = Vec::new();
            branches.push(build_service_trace_branch_status(
                "route_preference_override",
                if entry.preferred_locomotive_engine_type_raw_u8 == 2 {
                    "grounded_electric_override_candidate"
                } else {
                    "grounded_non_electric_or_inactive_override_candidate"
                },
                &[
                    "company periodic side-latch trio",
                    "world route-preference byte",
                    "preferred locomotive engine-type lane",
                ],
                &[],
                &[
                    "0x004019e0 periodic company outer owner",
                    "0x004078a0 preferred-locomotive chooser",
                    "0x0041d550 locomotive-era and engine-type approval gate",
                ],
                &["This probe keeps the outer owner at the save-owned input level; the concrete runtime reader/apply/restore seam is already grounded separately."],
            ));
            branches.push(build_service_trace_branch_status(
                "annual_finance_policy",
                "runnable_from_grounded_owner_state",
                &[
                    "company market/cache owner state",
                    "periodic side-latches",
                    "world issue/timing owner state",
                    "derived annual-finance readers",
                ],
                &[],
                &[
                    "0x004019e0 periodic company outer owner",
                    "0x00401c50 annual finance-policy owner",
                ],
                &["The shellless annual-finance helper is already rehosted on top of runtime-owned state."],
            ));
            branches.push(build_service_trace_branch_status(
                "city_connection_announcement",
                "blocked_missing_region_and_infrastructure_asset_owner_seams",
                &[
                    "company periodic side-latches",
                    "route-preference override seam",
                    "annual-finance sequencing owner",
                ],
                &[
                    "region pending/completion/one-shot/severity lanes",
                    "stable region id or class discriminator",
                    "placed-structure or infrastructure-asset consumer mapping",
                ],
                &[
                    "0x004019e0 periodic company outer owner",
                    "0x00406050 city-connection bonus/news owner",
                    "0x00420030 / 0x00420280 city-connection peer probes",
                    "0x0047efe0 placed-structure linked-company resolver",
                ],
                &["Current atlas evidence places this branch above both the region pending-bonus lane and infrastructure/placed-structure consumers."],
            ));
            branches.push(build_service_trace_branch_status(
                "linked_transit_roster_maintenance",
                "blocked_missing_site_cache_input_owner_mapping",
                &[
                    "company linked-transit latch",
                    "company linked-transit route-anchor tuple",
                    "company linked-transit peer-cache refresh absolute counter [company+0x0d3e]",
                    "company linked-transit autoroute-score refresh absolute counter [company+0x0d3a]",
                    "route-preference override seam",
                ],
                &[
                    "placed-structure site-cache input owners beneath 0x004093d0 / 0x00407bd0",
                    "persisted site-side inputs behind 0x00408280 / 0x00408380",
                ],
                &[
                    "0x004019e0 periodic company outer owner",
                    "0x00409720 timed linked-transit cache-service wrapper",
                    "0x004093d0 linked-transit site-peer cache rebuild",
                    "0x00407bd0 linked-transit autoroute site-score cache rebuild",
                    "0x00408280 linked-transit ranked-site chooser",
                    "0x00408380 linked-transit staged autoroute-entry builder",
                    "0x00408f70 linked-transit aggregate site-score pressure helper",
                    "0x00409770 linked-transit autoroute append/rotate owner",
                    "0x00409830 linked-transit add-train/news owner",
                    "0x00409950 linked-transit train-roster balancer",
                ],
                &["The save side now grounds the timed cache-owner seams, ranked-site chooser, staged route builder, and train-side follow-ons; the remaining blocker is the placed-structure site-cache input ownership beneath those bounded consumers, not the train-side strip itself."],
            ));
            branches.push(build_service_trace_branch_status(
                "industry_acquisition_side_branch",
                "blocked_missing_near-city_owner_mapping",
                &["periodic service outer owner", "annual-finance ordering"],
                &[
                    "near-city industry candidate owner seam",
                    "city or region peer linkage",
                ],
                &[
                    "0x004019e0 periodic company outer owner",
                    "0x004014b0 near-city industry acquisition and news owner",
                    "0x004010f0 near-city acquisition region scorer",
                    "0x00405920 same-company industry proximity aggregator",
                    "0x0041f6e0 center-cell token gate",
                    "0x0042b2d0 packed-u16 contains-key predicate",
                    "0x0047de00 linked-region resolver feeding 0x0040c990",
                    "0x004801a0 linked-transit route-anchor reachability gate",
                    "0x00425b90 pending-bonus/company-state gate",
                    "0x0040d360 region type/class filter",
                    "0x0040d540 weighted region-to-company proximity scorer",
                    "0x0040f6d0 subtype-1 placed-structure constructor",
                    "0x00481390 / 0x00480210 subtype-1 linked-site allocation and constructor",
                    "0x00444690 late world bring-up caller of 0x004133b0",
                    "0x004133b0 placed-structure local-runtime replay owner",
                    "0x0040e450 queued site-id cloned local-runtime replay helper",
                    "0x0040ee10 live-site position/scalar refresh helper",
                    "0x00480710 linked-site runtime side-buffer and route-entry-anchor refresh owner",
                    "0x0040df27 / 0x0040e00a / 0x0040edf6 linked-site side-refresh callers",
                    "0x004160aa non-bring-up runtime caller of 0x0040ee10",
                    "0x0048abc0 / 0x00493cf0 route-entry-anchor rebind and synthesis strip",
                    "0x0047dda0 linked-peer route-entry-anchor validator",
                    "0x00420030 / 0x00420280 peer-site boolean/selector pair",
                    "0x00406050 city-connection bonus/news sibling owner",
                ],
                &["Direct disassembly now shows this branch scanning the live placed-structure collection at 0x0062b26c for the best current acquisition target, rejecting sites whose owner field [site+0x276] is already nonzero, reusing the center-cell token gate 0x0041f6e0 -> 0x0042b2d0, reusing the linked-region status branch 0x0047de00 -> 0x0040c990, checking candidate reachability through 0x004801a0, consulting the placed-structure peer-site boolean/selector pair 0x00420030 / 0x00420280 over 0x006cec20, scoring candidate sites against company proximity and age through 0x0040d540 and 0x0040cac0, and then committing the chosen site through 0x004269b0. The peer-site selector seam itself is now grounded through the local helper strip: 0x0040cd70 seeds [site+0x3cc/+0x3d0] from 0x62b2fc / 0x62b268, 0x0040ceab and 0x0040d1a1 reach the save-backed 0x0045c150 / 0x0045c310 owner directly, that owner fills [owner+0x23e/+0x242] from tagged payload 0x5dc1, and 0x0045c36e then feeds [owner+0x23e] through 0x00456100 -> 0x00455b70 -> 0x0052edf0 into the live backing-record selector [site+0x04]. The cached tri-lane is no longer a restore-only mystery either: 0x0040d450 and 0x00410b30..0x004118f4 now bound the live writer family above the shared 0x00412560 candidate/admissibility gate, 0x0040fb70 is the small wrapper into that gate, and direct callers now separate acquisition-adjacent 0x0040d540 users like 0x00401633 / 0x0044b81a from broader sibling sweeps such as 0x004b4052 / 0x004b46ec / 0x004b70f5 / 0x004b7979. The remaining linked-site field work is now about which persisted site/peer lanes are actually sufficient for shellless acquisition and city-connection behavior, not about who owns [site+0x04] or whether the tri-lane has live producers."],
            ));
            SmpPeriodicCompanyServiceTraceEntry {
                company_id: entry.company_id,
                name: entry.name.clone(),
                active: entry.active,
                linked_chairman_profile_id: entry.linked_chairman_profile_id,
                preferred_locomotive_engine_type_raw_u8: entry.preferred_locomotive_engine_type_raw_u8,
                city_connection_latch: entry.city_connection_latch,
                linked_transit_latch: entry.linked_transit_latch,
                linked_transit_autoroute_site_score_cache_refresh_absolute_counter: entry
                    .linked_transit_autoroute_site_score_cache_refresh_absolute_counter,
                linked_transit_site_peer_cache_refresh_absolute_counter: entry
                    .linked_transit_site_peer_cache_refresh_absolute_counter,
                branches,
            }
        })
        .collect()
}
