use clap::Parser;
use log::info;

mod app;
mod cli;
mod config;
mod tui;

use cli::{ensure_conf_extension, Cli, Commands};
use config::ConfigManager;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    env_logger::init();

    let cli = Cli::parse();
    info!("Starting tmucks with command: {:?}", cli.command);

    match cli.command {
        Some(Commands::List) => {
            info!("Executing List command");
            let config_manager = ConfigManager::new()?;
            if config_manager.configs.is_empty() {
                println!("No configs found in ~/.config/tmucks/");
                println!("Try setting a config using the update flag!");
            } else {
                println!("Available configs:");
                for config in &config_manager.configs {
                    println!("  - {}", config);
                }
            }
        }
        Some(Commands::Apply { name }) => {
            info!("Executing Apply command for config: {}", name);
            let config_manager = ConfigManager::new()?;
            let config_name = ensure_conf_extension(name);
            config_manager.apply_config(&config_name)?;
            println!("✓ Applied config: {}", config_name);
            info!("Successfully applied config: {}", config_name);
        }
        Some(Commands::Save { name }) => {
            info!("Executing Save command for config: {}", name);
            let config_manager = ConfigManager::new()?;
            let config_name = ensure_conf_extension(name);
            config_manager.save_current_config(&config_name)?;
            println!("✓ Saved current config as: {}", config_name);
            info!("Successfully saved config: {}", config_name);
        }
        Some(Commands::Update { name }) => {
            info!("Executing Update command for config: {}", name);
            let config_manager = ConfigManager::new()?;
            let config_name = ensure_conf_extension(name);
            config_manager.update_config(&config_name)?;
            println!("+ updated config: {}", config_name);
            info!("Successfully updated config: {}", config_name);
        }
        Some(Commands::Delete { name }) => {
            info!("Executing Delete command for config: {}", name);
            let config_manager = ConfigManager::new()?;
            let config_name = ensure_conf_extension(name);
            config_manager.delete_config(&config_name)?;
            println!("✓ Deleted config: {}", config_name);
            info!("Successfully deleted config: {}", config_name);
        }
        Some(Commands::Rename { old_name, new_name }) => {
            info!("Executing Rename command: {} -> {}", old_name, new_name);
            let config_manager = ConfigManager::new()?;
            let old_config = ensure_conf_extension(old_name);
            let new_config = ensure_conf_extension(new_name);
            config_manager.rename_config(&old_config, &new_config)?;
            println!("renamed config: {} -> {}", old_config, new_config);
        }
        Some(Commands::Readme) => {
            info!("Executing Readme command");
            let readme = include_str!("../README.md");
            termimad::print_text(readme);
        }
        None => {
            info!("No command provided, starting TUI");
            tui::run()?;
        }
    }

    info!("tmucks completed successfully");
    Ok(())
}
