//! Validation functions for development mode

use crate::paths;
use anyhow::{anyhow, Context, Result};
use std::path::Path;

/// Check if the current directory is a Mecha10 project
pub fn check_project_initialized(project_root: &Path) -> Result<()> {
    let config_file = project_root.join(paths::PROJECT_CONFIG);

    if !config_file.exists() {
        return Err(anyhow!(
            "Not in a Mecha10 project directory.\n\
             Run 'mecha10 init' to create a new project."
        ));
    }

    Ok(())
}

/// Check Redis connection
#[allow(dead_code)] // Planned for future use
pub async fn check_redis_connection(redis_url: &str) -> Result<()> {
    let client = redis::Client::open(redis_url).context("Failed to create Redis client")?;

    let mut conn = client
        .get_multiplexed_async_connection()
        .await
        .context("Failed to connect to Redis")?;

    // Test connection with a ping
    redis::cmd("PING")
        .query_async::<String>(&mut conn)
        .await
        .context("Failed to ping Redis")?;

    Ok(())
}

/// Validate node names against available nodes
pub fn validate_node_names(requested_nodes: &[String], available_nodes: &[String]) -> Result<Vec<String>> {
    let mut invalid = Vec::new();

    for node in requested_nodes {
        if !available_nodes.contains(node) {
            invalid.push(node.clone());
        }
    }

    if !invalid.is_empty() {
        return Err(anyhow!(
            "Unknown nodes: {}\nAvailable nodes: {}",
            invalid.join(", "),
            available_nodes.join(", ")
        ));
    }

    Ok(requested_nodes.to_vec())
}
