//! Lifecycle adapter for CLI process management
//!
//! This module adapts the runtime lifecycle system for the CLI's process-based
//! node management. While the runtime manages nodes in-process, the CLI spawns
//! nodes as separate processes via node-runner.
//!
//! This adapter:
//! - Reads lifecycle config from mecha10.json
//! - Determines which nodes to spawn based on current mode
//! - Provides mode change logic without runtime dependency

use crate::types::project::{LifecycleConfig, ModeConfig, ProjectConfig};
use anyhow::Result;
use std::collections::{HashMap, HashSet};

/// Lifecycle mode manager for CLI
///
/// Manages mode transitions and determines which nodes should run in each mode.
/// This is a lightweight adapter that doesn't depend on the full runtime.
pub struct CliLifecycleManager {
    /// Current operational mode
    current_mode: String,

    /// Set of currently running node names
    running_nodes: HashSet<String>,

    /// Mode configurations from mecha10.json
    mode_config: HashMap<String, ModeConfig>,
}

/// Result of calculating mode transition
#[derive(Debug)]
#[allow(dead_code)] // Will be used for mode change commands
pub struct ModeTransitionDiff {
    /// Nodes that need to be started
    pub start: Vec<String>,

    /// Nodes that need to be stopped
    pub stop: Vec<String>,
}

impl CliLifecycleManager {
    /// Create a new CLI lifecycle manager from project config
    ///
    /// Returns None if project doesn't have lifecycle configuration
    pub fn from_project_config(config: &ProjectConfig) -> Option<Self> {
        let lifecycle = config.lifecycle.as_ref()?;

        Some(Self {
            current_mode: lifecycle.default_mode.clone(),
            running_nodes: HashSet::new(),
            mode_config: lifecycle.modes.clone(),
        })
    }

    /// Get the current mode
    pub fn current_mode(&self) -> &str {
        &self.current_mode
    }

    /// Get list of nodes that should run in the current mode
    pub fn nodes_for_current_mode(&self) -> Vec<String> {
        self.mode_config
            .get(&self.current_mode)
            .map(|config| config.nodes.clone())
            .unwrap_or_default()
    }

    /// Calculate what changes are needed to transition to target mode
    ///
    /// Returns the diff (nodes to start/stop) without changing state.
    #[allow(dead_code)] // Will be used for mode change commands
    pub fn calculate_mode_diff(&self, target_mode: &str) -> Result<ModeTransitionDiff> {
        // Validate mode exists
        let target_config = self
            .mode_config
            .get(target_mode)
            .ok_or_else(|| anyhow::anyhow!("Mode '{}' not found", target_mode))?;

        // Nodes that should run in target mode
        let target_nodes: HashSet<_> = target_config.nodes.iter().map(|s| s.as_str()).collect();

        // Nodes to start: in target but not currently running
        let start: Vec<_> = target_nodes
            .iter()
            .filter(|n| !self.running_nodes.contains(**n))
            .map(|s| s.to_string())
            .collect();

        // Nodes to stop: running but not in target
        let stop: Vec<_> = self
            .running_nodes
            .iter()
            .filter(|n| !target_nodes.contains(n.as_str()))
            .cloned()
            .collect();

        Ok(ModeTransitionDiff { start, stop })
    }

    /// Mark nodes as running (after spawning them)
    pub fn mark_nodes_running(&mut self, nodes: &[String]) {
        for node in nodes {
            self.running_nodes.insert(node.clone());
        }
    }

    /// Mark nodes as stopped (after killing them)
    #[allow(dead_code)] // Will be used for mode change commands
    pub fn mark_nodes_stopped(&mut self, nodes: &[String]) {
        for node in nodes {
            self.running_nodes.remove(node);
        }
    }

    /// Change to a new mode (updates internal state only)
    ///
    /// Returns the diff of what needs to change. Caller is responsible
    /// for actually spawning/killing processes.
    #[allow(dead_code)] // Will be used for mode change commands
    pub fn change_mode(&mut self, target_mode: &str) -> Result<ModeTransitionDiff> {
        let diff = self.calculate_mode_diff(target_mode)?;
        self.current_mode = target_mode.to_string();
        Ok(diff)
    }

    /// Get available modes
    #[allow(dead_code)] // Will be used for mode change commands
    pub fn available_modes(&self) -> Vec<&str> {
        self.mode_config.keys().map(|s| s.as_str()).collect()
    }

    /// Validate lifecycle configuration
    ///
    /// Checks that:
    /// - All node references exist in project config
    /// - Default mode exists
    #[allow(dead_code)] // Will be used for validation on project init
    pub fn validate(lifecycle: &LifecycleConfig, available_nodes: &[String]) -> Result<()> {
        // Check default mode exists
        if !lifecycle.modes.contains_key(&lifecycle.default_mode) {
            return Err(anyhow::anyhow!(
                "Default mode '{}' not found in modes",
                lifecycle.default_mode
            ));
        }

        // Check all node references are valid
        let node_set: HashSet<_> = available_nodes.iter().map(|s| s.as_str()).collect();

        for (mode_name, mode_config) in &lifecycle.modes {
            for node in &mode_config.nodes {
                if !node_set.contains(node.as_str()) {
                    return Err(anyhow::anyhow!(
                        "Mode '{}' references unknown node '{}'",
                        mode_name,
                        node
                    ));
                }
            }
        }

        Ok(())
    }
}
