//! Project status handler

use anyhow::{anyhow, Context, Result};
use colored::Colorize;
use syntaxis_core::ProjectConfig;
use tools_shared::find_config_path_warn_conflicts;

/// Handler for status operations
#[derive(Debug)]
pub struct StatusHandler;

impl StatusHandler {
    /// Show overall project status
    pub fn show_status() -> Result<()> {
        let config_path = Self::find_config()?;
        let config = ProjectConfig::load_from_file(&config_path)
            .context("Failed to load project configuration")?;

        println!("📊 Project Status Overview\n");

        println!("  {} Project Information", "▪".bold());
        println!("    Name: {}", config.project.name);
        println!("    Version: {}", config.project.version);
        println!("    Type: {:?}", config.project_type);
        println!("    Status: {}", "Active".green());

        println!("\n  {} Current Phase", "▪".bold());
        println!(
            "    Phase: {}",
            format!("{:?}", config.phase).yellow().bold()
        );
        println!("    Created: {}", config.project.created);
        println!("    Updated: {}", config.project.last_updated);

        println!("\n  {} Tools Status", "▪".bold());
        let enabled_count = config.tools.tools.iter().filter(|(_, t)| t.enabled).count();
        let total_count = config.tools.tools.len();
        for (name, tool) in &config.tools.tools {
            let status = if tool.enabled {
                format!("✓ {}", "enabled".green())
            } else {
                format!("○ {}", "disabled".dimmed())
            };
            println!("    {:<15} {}", name, status);
        }

        println!("\n  {} Configuration", "▪".bold());
        println!("    Active tools: {}/{}", enabled_count, total_count);
        println!("    Languages: {}", config.languages.join(", "));
        println!(
            "    Config file: {}",
            format!("{}", config_path.display()).dimmed()
        );

        println!("\n  {} Summary", "▪".bold());
        println!("    Project is properly configured and ready for development");

        Ok(())
    }

    // Helper: Find config file
    fn find_config() -> Result<std::path::PathBuf> {
        find_config_path_warn_conflicts("config.toml", None).ok_or_else(|| {
            anyhow!(
                "Configuration not found. Please create:\n\
                 - .project/lifecycle.toml, or\n\
                 - .vapora/lifecycle.toml"
            )
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_status_handler_creation() {
        let handler = StatusHandler;
        assert_eq!(format!("{:?}", handler), "StatusHandler");
    }
}
