use crate::inspect::smp::services::SmpServiceConsumerHypothesis;
use crate::inspect::smp::world::SmpSaveCompanyChairmanAnalysisReport;

pub(super) fn build_tagged_load_hypothesis(
    analysis: &SmpSaveCompanyChairmanAnalysisReport,
) -> SmpServiceConsumerHypothesis {
    SmpServiceConsumerHypothesis {
        label: "region tagged-load restore path".to_string(),
        status: if analysis.region_record_triplets.is_some() {
            "parallel_static_mapping_target".to_string()
        } else {
            "possible_consumer_family".to_string()
        },
        candidate_consumers: vec![
            "0x00421510 world_region_collection_refresh_records_from_tagged_bundle".to_string(),
            "0x0041f5c0 world_region_load_tagged_payload_and_profile_collection_0x37f".to_string(),
            "0x00455fc0 shared region tagged-payload reload companion".to_string(),
        ],
        evidence: vec![
            "the checked-in function map already grounds 0x00421510 as the tagged region-collection load owner that dispatches each live record through vtable slot +0x40".to_string(),
            "the checked-in function map already grounds 0x0041f5c0 as the per-record load slot that reloads the tagged payload through 0x00455fc0 and then rebuilds profile collection [region+0x37f]".to_string(),
            "constructor-side evidence now proves the latches are initialized locally, so the remaining gap can legitimately be framed as post-construction restore or rebuild".to_string(),
            "direct disassembly of 0x0041f590/0x0041f5b0 now proves the world-region vtable root is 0x005c9a28, so the 0x00455fc0 dispatch at slot +0x48 lands on 0x00455870 and the serializer sibling at +0x4c lands on 0x00455930".to_string(),
            "direct disassembly of 0x00455870/0x00455930 now shows that callback pair only restores and serializes two helper-local three-lane scalar bands: 0x00455870 reads six dwords through 0x531150 and forwards them to 0x530720 -> [helper+0x1e2/+0x1e6/+0x1ea] and 0x52e8b0 -> [helper+0x4b/+0x4f/+0x53], while 0x00455930 writes that same pair back through 0x531030; it still does not touch acquisition-side lanes [region+0x2a4] or [region+0x310/+0x338/+0x360]".to_string(),
            "direct disassembly now tightens the rest of 0x00455fc0 too: after the +0x48 callback it only runs 0x0052ebd0 to read two one-byte generic flags through 0x531150 into base object bytes [this+0x20], [this+0x8d], [this+0x5c..+0x61], [this+0x1ee], [this+0x1fa], and [this+0x3e], then opens 0x55f3 only for span accounting before returning, so the missing region latches are not hiding in the remainder of 0x00455fc0 either".to_string(),
            "direct disassembly of 0x00421510 now also shows the exact tagged loop shape: it opens 0x5209, reads 0x520a, iterates live entry ordinals through 0x518380/0x518140, seeds a stack-local world-region vtable helper through 0x0041f590/0x0041f5b0, dispatches each loaded record through slot +0x40, and only then closes 0x520b".to_string(),
            "direct disassembly of 0x0041f5c0 now also shows its post-0x00455fc0 work is local to the profile collection path: it clamps [region+0x31b] back to 1.0f when needed, zeroes [region+0x317], allocates one 0x88-sized helper through 0x53b070/0x518b90, stores it at [region+0x37f], loads that helper through 0x518680, and clears [region+0x385] before returning".to_string(),
            "the first caller-side checkpoint above 0x00421510 is now grounded too: 0x00444887 invokes the region collection refresh inside a broader restore sequence and then immediately advances to territory_collection_refresh_records_from_tagged_bundle 0x00487c20 and support_collection_refresh_records_from_tagged_bundle 0x0040b5d0, which makes the missing latches look like a later global rebuild seam rather than hidden work inside 0x00421510 itself".to_string(),
            "direct disassembly now rules down the next 0x00444887 continuation branch too: after the region, territory, and support refresh owners, 0x00433130 only opens 0x4e99/0x4e9a, repopulates live event collection 0x0062be18 through 0x0042db20, and closes 0x4e9b, so that event-side loader is not the missing later region restore handoff for [region+0x2a4] or [region+0x310/+0x338/+0x360] either".to_string(),
            "that broader restore strip now also has one grounded later region-local sweep: at 0x00444b08 it re-enters the live region collection through 0x00421ce0, which walks live records via 0x518380/0x518140 and dispatches 0x0041fb00 per record".to_string(),
            "the checked-in atlas already grounds 0x0041fb00 as the class-0-only default-region helper under the same family, and 0x00421730 as the later raster finalizer that repopulates [world+0x212d] from class-0 region ids".to_string(),
            "direct disassembly now tightens that later sweep too: 0x0041fb00 exits immediately for nonzero [region+0x23e], while 0x00421730 clears the per-cell region word at [world+0x212d]+cell*4+1, seeds cached bounds-like fields [region+0x242/+0x246/+0x24a/+0x24e/+0x252], and only then enters the class-0 path that consumes [region+0x256] and the coordinate helpers 0x00455800/0x00455810".to_string(),
            "the companion region-set root is runtime-owned now too: direct disassembly of the broader bring-up strip at 0x00448740..0x0044881f shows 0x006cfc9c being allocated through 0x53b070 and constructed through 0x00487bd0 before later rebuild passes run, so the 0x00487650/0x004881b0 companion path is operating on a runtime-owned helper collection rather than a hidden save-owned latch seam".to_string(),
            "the later restore-band siblings are tighter now too: 0x00487de0 clears the prior chunked border queues through 0x00533cf0, builds a small per-region id map from [region+0x00]/[region+0x35] keyed by class [region+0x31], scans the live world raster at [world+0x2131], and appends fresh border-segment rows through 0x00536ea0 without touching [region+0x25e/+0x276/+0x302/+0x316]".to_string(),
            "the neighboring world-grid reseed 0x0044c4b0 is tighter now too: it clears bit 0x10 across the live grid byte at cell offset +0xe6, then walks the live region collection at 0x0062bae0, admits only class-0 records via [region+0x23e], resolves one representative center cell through 0x00455f60, and marks that same bit back on, which again reads as raster presentation state rather than pending/completion latch restore".to_string(),
            "the companion region-set rebuild above that border pass is narrower now too: 0x00487650 is only a small constructor wrapper over 0x00487540 that seeds [region+0x00] from the caller-supplied id, while 0x004881b0 rebuilds [region+0x3d] from the world raster histogram, zeroes [region+0x41], folds class-0 children back into parent [region+0x41] through [region+0x35], and then tails into the border emitter on 0x006cfc9c via 0x00487de0".to_string(),
            "the later class-0 batch at 0x00438087 is narrower now too: it walks live class-0 regions through 0x0062bae0, scales the mirrored severity/source pair [region+0x25a/+0x25e] from the current value using world-side factors, clamps the result, and then hands the whole collection to 0x00421c20; it never reads or writes [region+0x276/+0x302/+0x316]".to_string(),
            "the follow-on 0x00421c20 is now bounded as a parameterized region-collection helper rather than a latch owner: it loops the same collection with caller-provided scalar arguments, dispatches each record through 0x004235c0, and never touches the pending/completion/one-shot lanes directly".to_string(),
            "the subsequent world follow-ons are narrower too: 0x00437b20 only stages one world-side reentry guard at [world+0x46c38], iterates the live region collection through 0x00423d30, and then tails into 0x00434d40, while 0x00437220 rebuilds broader world byte-set state around [world+0x66be/+0x69db] and other global collections. Those branches are still broader runtime follow-ons, not direct owners of [region+0x276/+0x302/+0x316]".to_string(),
        ],
        blockers: vec![
            "which later restore or rebuild owner rehydrates [region+0x276/+0x302/+0x316] after the 0x00421510 -> 0x0041f5c0 -> 0x00455fc0 path completes".to_string(),
            "whether [region+0x25e] severity/source and any stable region id/class discriminator are serialized elsewhere in the tagged region body or recomputed later by another post-load owner after the 0x00421ce0 -> 0x0041fb00 -> 0x00421730 class-0 raster/id sweep, 0x004881b0 companion cell-count rebuild, 0x00487de0 border rebuild, 0x0044c4b0 center-cell reseed, the 0x00438087 mirrored severity/source batch, and the 0x00421c20 -> 0x004235c0 follow-on helper loop are all ruled out as latch owners".to_string(),
        ],
    }
}
