cc-hook 0.1.0

A cross-platform CLI that installs a git commit-msg hook to enforce Conventional Commits
use std::io::{self, IsTerminal, Write};

const RED: &str = "\x1b[0;31m";
const GREEN: &str = "\x1b[0;32m";
const YELLOW: &str = "\x1b[1;33m";
const BLUE: &str = "\x1b[0;34m";
const NC: &str = "\x1b[0m";

fn use_color() -> bool {
    std::env::var("NO_COLOR").is_err() && io::stdout().is_terminal()
}

fn colorize(color: &str, label: &str, msg: &str) {
    if use_color() {
        print!("{color}{label}{NC} {msg}");
    } else {
        print!("{label} {msg}");
    }
    let _ = io::stdout().flush();
}

pub fn print_error(msg: &str) {
    colorize(RED, "ERRO:", msg);
    println!();
}

pub fn print_success(msg: &str) {
    colorize(GREEN, "SUCESSO:", msg);
    println!();
}

pub fn print_warning(msg: &str) {
    colorize(YELLOW, "AVISO:", msg);
    println!();
}

pub fn print_info(msg: &str) {
    colorize(BLUE, "INFO:", msg);
    println!();
}

/// Enables ANSI escape code support on Windows 10+.
/// No-op on other platforms.
pub fn enable_ansi_support() {
    #[cfg(windows)]
    {
        use std::os::windows::io::AsRawHandle;

        unsafe extern "system" {
            fn GetConsoleMode(handle: *mut std::ffi::c_void, mode: *mut u32) -> i32;
            fn SetConsoleMode(handle: *mut std::ffi::c_void, mode: u32) -> i32;
        }

        unsafe {
            let handle = io::stdout().as_raw_handle();
            let mut mode: u32 = 0;
            if GetConsoleMode(handle as *mut _, &mut mode) != 0 {
                // ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
                let _ = SetConsoleMode(handle as *mut _, mode | 0x0004);
            }
        }
    }
}

/// Prints the validation error message with format guide, matching the original script output.
pub fn print_validation_error(commit_msg: &str) {
    println!("❌ ERRO: A mensagem de commit não segue o formato do Conventional Commits.");
    println!();
    println!("📝 Formato obrigatório:");
    println!("   <tipo>(<escopo opcional>): <descrição>");
    println!();
    println!("🏷️  Tipos válidos:");
    println!("   feat     - Nova funcionalidade");
    println!("   fix      - Correção de bug");
    println!("   docs     - Alterações na documentação");
    println!("   style    - Formatação, espaços, etc. (sem mudança de código)");
    println!("   refactor - Refatoração (sem nova funcionalidade ou correção)");
    println!("   test     - Adicionar ou modificar testes");
    println!("   chore    - Manutenção geral (dependências, ferramentas)");
    println!("   build    - Sistema de build ou dependências externas");
    println!("   ci       - Configuração de CI/CD");
    println!("   perf     - Melhoria de performance");
    println!("   revert   - Reverter commit anterior");
    println!();
    println!("✨ Exemplos válidos:");
    println!("   feat(auth): adicionar autenticação JWT");
    println!("   fix(api): corrigir timeout na requisição");
    println!("   docs: atualizar README com instruções");
    println!("   style(components): ajustar indentação");
    println!("   feat!: mudança que quebra compatibilidade");
    println!();
    println!("❌ Sua mensagem:");
    println!("   \"{commit_msg}\"");
    println!();
}