pub mod act;
pub mod decide;
pub mod integrate;
pub mod orient;
pub mod sense;

use crate::graph::SignalGraph;
use anyhow::Result;
use atlas::{invocation::SkillContext, SkillRegistry};

pub struct SpiralCycle {
    pub graph: SignalGraph,
    pub registry: SkillRegistry,
    pub max_turns: usize,
    pub turn: usize,
}

impl SpiralCycle {
    pub fn new(registry: SkillRegistry) -> Self {
        Self {
            graph: SignalGraph::new(),
            registry,
            max_turns: 10,
            turn: 0,
        }
    }

    /// Run one turn of the Spiral Cycle (SENSE→ORIENT→DECIDE→ACT→INTEGRATE).
    pub fn run_turn(&mut self, ctx: &SkillContext) -> Result<bool> {
        self.turn += 1;
        let signals = sense::harvest(ctx);
        let situ = orient::contextualize(&signals, &self.graph);
        let decision = decide::select_skills(&situ, &self.registry, ctx)?;
        let outcomes = act::execute_all(decision, &self.registry, ctx);
        integrate::merge(outcomes, &mut self.graph, ctx);
        Ok(self.turn >= self.max_turns)
    }
}
