//! Trace collection and output for simulation runs.
//!
//! Records per-role state snapshots at each simulation step.

use serde::{Deserialize, Serialize};
use telltale_types::FixedQ32;

/// A single step record for one role.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct StepRecord {
    /// Simulation step index.
    pub step: u64,
    /// Role name.
    pub role: String,
    /// State vector after this step.
    pub state: Vec<FixedQ32>,
}

/// Collected trace from a simulation run.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct Trace {
    /// All recorded step records.
    pub records: Vec<StepRecord>,
}

impl Trace {
    /// Create an empty trace.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Record a step.
    pub fn record(&mut self, record: StepRecord) {
        self.records.push(record);
    }

    /// Get all records for a specific role.
    #[must_use]
    pub fn records_for_role(&self, role: &str) -> Vec<&StepRecord> {
        self.records.iter().filter(|r| r.role == role).collect()
    }

    /// Get the final state for a role.
    #[must_use]
    pub fn final_state(&self, role: &str) -> Option<&[FixedQ32]> {
        self.records
            .iter()
            .rev()
            .find(|r| r.role == role)
            .map(|r| r.state.as_slice())
    }
}
