//! Default configurations for templates
//!
//! Provides logic for determining which nodes should be pre-selected
//! based on the chosen template.

use super::templates::get_template_by_id;

/// Get default node selection for a template
///
/// Returns a vector of booleans indicating which nodes in the available_ids
/// list should be pre-selected based on the template's defaults.
///
/// # Arguments
///
/// * `template_id` - Optional template ID (basic, rover, arm, humanoid)
/// * `available_ids` - List of available node IDs
///
/// # Returns
///
/// Vector of booleans, same length as available_ids, where true means pre-selected
pub fn get_template_defaults(template_id: &Option<String>, available_ids: &[String]) -> Vec<bool> {
    let mut defaults = vec![false; available_ids.len()];

    // Get template definition
    let template_id_str = template_id.as_deref().unwrap_or("basic");
    let Some(template) = get_template_by_id(template_id_str) else {
        // Unknown template, use basic defaults
        return get_basic_defaults(available_ids);
    };

    // Mark nodes that should be pre-selected
    for (i, node_id) in available_ids.iter().enumerate() {
        if template.default_nodes.iter().any(|n| n == node_id) {
            defaults[i] = true;
        }
    }

    defaults
}

/// Get basic template defaults (fallback)
fn get_basic_defaults(available_ids: &[String]) -> Vec<bool> {
    let basic_nodes = [
        "speaker".to_string(),
        "listener".to_string(),
        "motor".to_string(),
        "imu".to_string(),
        "teleop".to_string(),
        "simulation-bridge".to_string(),
        "diagnostics-node".to_string(),
        "websocket-bridge".to_string(),
        "image-classifier".to_string(),
        "object-detector".to_string(),
        "llm-command".to_string(),
        "behavior-executor".to_string(),
    ];
    available_ids.iter().map(|id| basic_nodes.contains(id)).collect()
}

/// Check if a template includes a specific node by default
///
/// # Arguments
///
/// * `template_id` - Template ID
/// * `node_id` - Node ID to check
///
/// # Returns
///
/// true if the template includes this node by default
#[allow(dead_code)] // Tested, planned for future use
pub fn template_has_node(template_id: &str, node_id: &str) -> bool {
    get_template_by_id(template_id)
        .map(|t| t.default_nodes.iter().any(|n| n == node_id))
        .unwrap_or(false)
}
