//! Phase 72: PHI dst observation integration test
//!
//! This test observes PHI dst allocation by instrumenting the MirBuilder.

use nyash_rust::mir::join_ir::verify_phi_reserved::{
    analyze_distribution, disable_observation, enable_observation, get_observations,
};

/// Phase 72-1: Observe PHI dst distribution from MirBuilder usage
///
/// This test manually creates MIR scenarios to observe PHI dst allocation.
#[test]
fn test_phase72_observe_phi_dst_via_builder() {
    use nyash_rust::mir::builder::MirBuilder;
    use nyash_rust::mir::types::ConstValue;
    use nyash_rust::mir::MirInstruction;

    enable_observation();

    // Create multiple builders to simulate different compilation contexts
    for scenario in 0..10 {
        let mut builder = MirBuilder::new();
        builder.enter_function(format!("test_func_{}", scenario));

        // Allocate some values (simulating loop setup)
        let entry_block = builder.current_block();

        // Simulate loop header block
        builder.push_block();
        let header_block = builder.current_block();

        // Allocate initial values
        let v1 = builder.next_value_id();
        let v2 = builder.next_value_id();
        let v3 = builder.next_value_id();

        // Emit some instructions
        builder.emit_instruction(MirInstruction::Const {
            dst: v1,
            value: ConstValue::Integer(0),
        });
        builder.emit_instruction(MirInstruction::Const {
            dst: v2,
            value: ConstValue::Integer(100),
        });
        builder.emit_instruction(MirInstruction::Const {
            dst: v3,
            value: ConstValue::Integer(1),
        });

        // Now allocate PHI dst candidates (what would be PHI dsts)
        // These come from builder.next_value_id() in loop_header_phi_builder
        let phi_dst_1 = builder.next_value_id();
        let phi_dst_2 = builder.next_value_id();
        let phi_dst_3 = builder.next_value_id();

        // Manually observe these as if they were PHI dsts
        #[cfg(debug_assertions)]
        {
            nyash_rust::mir::join_ir::verify_phi_reserved::observe_phi_dst(phi_dst_1);
            nyash_rust::mir::join_ir::verify_phi_reserved::observe_phi_dst(phi_dst_2);
            nyash_rust::mir::join_ir::verify_phi_reserved::observe_phi_dst(phi_dst_3);
        }

        builder.exit_function();
    }

    // Collect observations
    let observations = get_observations();
    let report = analyze_distribution(&observations);

    eprintln!("\n========== Phase 72: PHI dst Distribution Analysis ==========");
    eprintln!("{}", report.summary());
    eprintln!();
    eprintln!("Detailed breakdown:");
    eprintln!(
        "  - Reserved region (0-99):   {} PHI dsts",
        report.in_reserved
    );
    eprintln!("  - Param region (100-999):   {} PHI dsts", report.in_param);
    eprintln!("  - Local region (1000+):     {} PHI dsts", report.in_local);
    eprintln!();

    if report.is_all_reserved() {
        eprintln!("✅ CONCLUSION: All PHI dsts are in reserved region (0-99)");
        eprintln!("   → Safe to strengthen verifier with reserved region check");
    } else {
        eprintln!("⚠️  CONCLUSION: Some PHI dsts are OUTSIDE reserved region");
        eprintln!("   → PHI dst allocation does NOT respect reserved boundary");
        eprintln!("   → Document this finding and skip verifier strengthening");

        if let (Some(min), Some(max)) = (report.min_val, report.max_val) {
            eprintln!();
            eprintln!("Observed range: [{}, {}]", min, max);
            eprintln!("Expected range: [0, 99]");
        }
    }
    eprintln!("==============================================================\n");

    disable_observation();

    // This test always passes - it's for observation and decision-making
    assert!(true, "Observation complete - see output above for analysis");
}
