// Activation key generator tool
// Usage: keygen <machine_code> <days>
// Double-click to run without args shows help

use tauri_app_lib::license;

fn pause() {
    println!();
    println!("按回车键退出...");
    let _ = std::io::stdin().read_line(&mut String::new());
}

fn main() {
    let args: Vec<String> = std::env::args().collect();

    if args.len() < 3 {
        println!("=== 进销存管理系统 - 激活码生成工具 ===");
        println!();
        println!("用法: keygen.exe <机器码> <授权天数>");
        println!();
        println!("示例:");
        println!("  keygen.exe ABC123DEF456 365    (生成一年授权)");
        println!("  keygen.exe ABC123DEF456 99999  (生成永久授权)");
        println!();
        println!("客户将机器码发给你，你用此工具生成激活码，发给客户即可。");
        pause();
        return;
    }

    let machine_code = &args[1];
    let days: u32 = match args[2].parse() {
        Ok(d) => d,
        Err(_) => {
            eprintln!("错误: 授权天数必须是数字");
            pause();
            return;
        }
    };

    let activation_code = license::generate_activation(machine_code, days);

    println!("========================================");
    println!("  激活码生成成功");
    println!("========================================");
    println!();
    println!("  机器码:     {}", machine_code);
    println!("  授权天数:   {} 天", days);
    println!("  激活码:");
    println!("  {}", activation_code);
    println!();
    println!("========================================");
    println!("  将此激活码发送给客户即可");
    println!("========================================");
    pause();
}
