#![allow(dead_code)]

use anyhow::Result;
use clap::{Parser, Subcommand};
use std::path::PathBuf;
use tracing::debug;

mod commands;
mod config;
mod generator;
mod git;
mod i18n;
mod llm;
mod utils;

use commands::{
    changelog::ChangelogCommand, commit::CommitCommand, config::ConfigCommand, init::InitCommand,
    profile::ProfileCommand, tag::TagCommand,
};

/// QuiCommit - AI-powered Git assistant
///
/// A powerful tool that helps you generate conventional commits, tags, and changelogs
/// using AI (LLM APIs or local Ollama models). Manage multiple Git profiles for different
/// work contexts seamlessly.
#[derive(Parser)]
#[command(name = "quicommit")]
#[command(about = "AI-powered Git assistant for conventional commits, tags, and changelogs")]
#[command(version = env!("CARGO_PKG_VERSION"))]
#[command(propagate_version = true)]
#[command(arg_required_else_help = true)]
struct Cli {
    /// Enable verbose output
    #[arg(short, long, global = true, action = clap::ArgAction::Count)]
    verbose: u8,

    /// Configuration file path
    #[arg(short, long, global = true, env = "QUICOMMIT_CONFIG")]
    config: Option<String>,

    /// Disable colored output
    #[arg(long, global = true, env = "NO_COLOR")]
    no_color: bool,

    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Initialize quicommit configuration
    #[command(alias = "i")]
    Init(InitCommand),

    /// Generate and execute conventional commits
    #[command(alias = "c")]
    Commit(CommitCommand),

    /// Generate and create Git tags
    #[command(alias = "t")]
    Tag(TagCommand),

    /// Generate changelog
    #[command(alias = "cl")]
    Changelog(ChangelogCommand),

    /// Manage Git profiles (user info, SSH, GPG)
    #[command(alias = "p")]
    Profile(ProfileCommand),

    /// Manage configuration settings
    #[command(alias = "cfg")]
    Config(ConfigCommand),
}

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();

    let log_level = match cli.verbose {
        0 => "warn",
        1 => "info",
        2 => "debug",
        _ => "trace",
    };

    tracing_subscriber::fmt()
        .with_env_filter(log_level)
        .with_target(false)
        .init();

    debug!("Starting quicommit v{}", env!("CARGO_PKG_VERSION"));

    let config_path: Option<PathBuf> = cli.config.map(PathBuf::from);

    match cli.command {
        Commands::Init(cmd) => cmd.execute(config_path).await,
        Commands::Commit(cmd) => cmd.execute(config_path).await,
        Commands::Tag(cmd) => cmd.execute(config_path).await,
        Commands::Changelog(cmd) => cmd.execute(config_path).await,
        Commands::Profile(cmd) => cmd.execute(config_path).await,
        Commands::Config(cmd) => cmd.execute(config_path).await,
    }
}
