// Aicent Stack | GTIOT (Global Trusted IoT)
// Domain: http://gtiot.com
// Purpose: 1.2 kHz high-frequency reflex & 128-bit Action-Collapse logic.
// Specification: RFC-005 Standard (Active).
// License: Apache-2.0 via Aicent.com Organization.
//! # RFC-005: GTIOT Sensory-Motor Reflex Arc
//! 
//! This module implements the high-frequency embodied execution loop.
//! Utilizing 128-bit hardware atomicity, it facilitates the "Action-Collapse" 
//! mechanism, ensuring digital intent manifests as physical reality in <200µs.

use embassy_time::{Duration, Instant, Timer};
use rttp::PulseFrameHeader;
use crate::aal::{ActionAbstractionLayer, ActionPrimitive};
use crate::shadow::ShadowState;
use crate::fusion::SensorFusion;
use crate::resonance::KineticResonance;

/// [RFC-005] Sensory-Motor Task Execution.
/// Manages the 1.2 kHz proprioceptive loop, orchestrating the transition 
/// from perception to actuation across the planetary grid.
#[embassy_executor::task]
pub async fn sensory_motor_loop() {
    // --- Initialization of Embodied Primitives ---
    let mut sensor_fusion = SensorFusion::new();      
    let mut shadow_state = ShadowState::new();    
    let aal = ActionAbstractionLayer::new();          
    let mut hive_sync = KineticResonance::new();     
    let motor_primitive = ActionPrimitive::default();
    
    #[cfg(debug_assertions)]
    log_gtiot("Body Homeostasis Initialized. RFC-005 Standard Active.");

    let mut last_pulse_time = Instant::now();

    loop {
        // --- PHASE 1: SENSORY INGRESS (Perception) ---
        // [RFC-005] High-fidelity sampling (1200Hz).
        // Captures [X-Acc | Y-Acc | Vibration_Hz | Temp_K] as an atomic manifold.
        let raw_sensors = crate::hw::read_sensors();
        let fused_shadow = sensor_fusion.fuse(&raw_sensors); 

        // --- PHASE 2: NEURAL EMISSION (Pulse Generation) ---
        // Packaging the sensory state into a 64-byte RTTP Header (RFC-002).
        let mut header = PulseFrameHeader::new(
            [0x88; 32],                        // Local AID Fingerprint (RFC-001)
            80_000,                            // ZCMK Resource Bid (RFC-004)
            fused_shadow.parity_score as u64,  // Semantic Hash for routing
        );
        
        // [RFC-006] Hive-Sync Flag: Enable Collective Intelligence if phase-locked.
        if hive_sync.is_active() {
            header.flags |= 0b1000; 
        }

        // --- PHASE 3: MOTOR REFLEX (Action-Collapse) ---
        // In production, this channel ingests pulses from the RTTP spinal cord.
        let mock_command: Option<&[u8]> = None; 
        
        if let Some(cmd_frame) = mock_command {
            let cmd_header = unsafe { &*(cmd_frame.as_ptr() as *const PulseFrameHeader) };
            let payload = &cmd_frame[64..];

            // 🛡️ [RFC-003] Parallel Immunity Scan (Hardware Gate)
            // Ensures motor commands carry valid In-band Tensor Watermarks.
            if !rpki::parallel_immune_scan(cmd_header, payload).is_safe() {
                crate::hw::hardware_kill_switch(); // Atomic physical isolation
                continue;
            }

            // 🩸 [RFC-004] Circulatory Settlement (Credit Clearing)
            // Finalize picotoken shunting before releasing torque to physical actuators.
            if zcmk::circulatory_pump(cmd_header, payload).is_none() {
                continue; 
            }

            // 🦾 [RFC-005] ACTION-COLLAPSE
            // Collapsing digital intent into a 128-bit hardware torque manifold in <200µs.
            let brain_intent = crate::parser::parse_intent_stub(payload);
            let packed_kinetics = aal.collapse(brain_intent, &shadow_state);

            // 🟣 [RFC-006] Hive Kinetic Alignment (Collective Resonance)
            // Phase-locking the local trajectory with the collective swarm resonance.
            let aligned_kinetics = if cmd_header.flags & 0b1000 != 0 {
                hive_sync.align_with_swarm_u128(packed_kinetics)
            } else {
                packed_kinetics
            };

            // [HARDWARE ATOMICITY] Commit the 128-bit manifold to the physical actuators.
            aal.commit_to_actuators(&motor_primitive, aligned_kinetics);
            crate::hw::execute_actuators(&motor_primitive);
            
            last_pulse_time = Instant::now();

            // --- PHASE 4: PROPRIOCEPTIVE SYNC ---
            // Update local shadow twin and predict next 5 trajectories.
            shadow_state.update(aligned_kinetics);
            shadow_state.predict_trajectories(); 
        }

        // --- PHASE 5: FAIL-SAFE AUTONOMY ---
        // If the RTTP nerve connection is severed (>3ms), fallback to safe shadow-trajectory.
        if Instant::now() - last_pulse_time > Duration::from_millis(3) {
            let safe_action = shadow_state.get_safe_trajectory();
            // 🛡️ FIX: Calling torque_vectors() method instead of field access.
            crate::hw::execute_actuators_direct(&safe_action.torque_vectors());
            
            #[cfg(debug_assertions)]
            log_gtiot("Neural sever detected. Fail-safe Oracle engaged.");
        }

        // Cycle Calibration: 833µs fixed interval for 1.2 kHz resonance.
        Timer::after_micros(833).await;
    }
}

/// Internal high-fidelity logging for GTIOT execution events.
fn log_gtiot(msg: &str) {
    println!("\x1b[1;33m[GTIOT-BODY]\x1b[0m 🦾 {}", msg);
}
