//! Rustelo Development Tools CLI
//!
//! Command-line utilities for development workflows, build tasks, and project
//! management.

use std::process;

use anyhow::{Context, Result};
use clap::{Arg, Command as ClapCommand};
use rustelo_tools::{build, cli, dev};

fn main() {
    let matches = ClapCommand::new("dev-tools")
        .version("0.1.0")
        .author("Rustelo Team")
        .about("Rustelo Development Tools - CLI utilities for build, dev, and project management")
        .subcommand(
            ClapCommand::new("build").about("Run build tasks").arg(
                Arg::new("task")
                    .help("Build task to run")
                    .value_name("TASK")
                    .index(1),
            ),
        )
        .subcommand(
            ClapCommand::new("dev")
                .about("Development workflow commands")
                .arg(
                    Arg::new("command")
                        .help("Dev command to run")
                        .value_name("COMMAND")
                        .index(1),
                ),
        )
        .subcommand(ClapCommand::new("manager").about("Launch the page manager TUI"))
        .get_matches();

    let result = match matches.subcommand() {
        Some(("build", sub_matches)) => {
            let task = sub_matches
                .get_one::<String>("task")
                .map(String::as_str)
                .unwrap_or("all");
            handle_build_command(task)
        }
        Some(("dev", sub_matches)) => {
            let command = sub_matches
                .get_one::<String>("command")
                .map(String::as_str)
                .unwrap_or("watch");
            handle_dev_command(command)
        }
        Some(("manager", _)) => handle_manager_command(),
        _ => {
            println!("🚀 Rustelo Development Tools");
            println!();
            println!("Available commands:");
            println!("  build    - Run build tasks");
            println!("  dev      - Development workflow commands");
            println!("  manager  - Launch page manager TUI");
            println!();
            println!("Use --help with any command for more information.");
            Ok(())
        }
    };

    if let Err(e) = result {
        eprintln!("❌ Error: {}", e);
        process::exit(1);
    }
}

fn handle_build_command(task: &str) -> Result<()> {
    match task {
        "all" | "shared-resources" => {
            println!("🚀 Running shared resources generation...");
            build::generate_shared_resources()
                .map_err(|e| anyhow::anyhow!("Failed to generate shared resources: {}", e))?;
            println!("✅ Build completed successfully");
        }
        _ => {
            println!("❌ Unknown build task: {}", task);
            println!("Available tasks: all, shared-resources");
            return Err(anyhow::anyhow!("Invalid build task"));
        }
    }
    Ok(())
}

fn handle_dev_command(command: &str) -> Result<()> {
    match command {
        "watch" | "dev" => {
            println!("🚀 Starting development server...");
            cli::execute_dev_workflow("dev").context("Failed to start development server")?;
        }
        "build" => {
            println!("🚀 Building project...");
            cli::execute_dev_workflow("build").context("Failed to build project")?;
        }
        "init" => {
            println!("🚀 Initializing development environment...");
            let workflow = dev::create_workflow()
                .map_err(|e| anyhow::anyhow!("Failed to create development workflow: {}", e))?;
            workflow.init().map_err(|e| {
                anyhow::anyhow!("Failed to initialize development environment: {}", e)
            })?;
        }
        _ => {
            println!("❌ Unknown dev command: {}", command);
            println!("Available commands: watch, build, init");
            return Err(anyhow::anyhow!("Invalid dev command"));
        }
    }
    Ok(())
}

fn handle_manager_command() -> Result<()> {
    println!("🚀 Launching Rustelo Manager...");
    // This would launch the manager TUI - for now just show a message
    println!("💡 Use 'cargo run --bin rustelo-manager' to launch the full manager interface");
    Ok(())
}
