use clap::{Parser, Subcommand};
use portable_toolkit::cipher::{decrypt_file, encrypt_file};
use std::path::PathBuf;

#[derive(Parser)]
#[command(name = "file_cipher", about = "文件 AES-GCM 加密/解密工具")]
struct Cli {
    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// 加密文件
    Encrypt {
        /// 输入文件路径
        #[arg(short, long)]
        input: PathBuf,

        /// 输出文件路径
        #[arg(short, long)]
        output: PathBuf,

        /// 16字节 AES-128 密钥
        #[arg(short, long)]
        key: String,
    },
    /// 解密文件
    Decrypt {
        /// 输入文件路径（加密文件）
        #[arg(short, long)]
        input: PathBuf,

        /// 输出文件路径（明文文件）
        #[arg(short, long)]
        output: PathBuf,

        /// 16字节 AES-128 密钥
        #[arg(short, long)]
        key: String,
    },
}

fn main() {
    let cli = Cli::parse();

    match cli.command {
        Command::Encrypt { input, output, key } => {
            if let Err(e) = encrypt_file(&input, &output, &key) {
                eprintln!("加密失败: {}", e);
                std::process::exit(1);
            }
        }
        Command::Decrypt { input, output, key } => {
            if let Err(e) = decrypt_file(&input, &output, &key) {
                eprintln!("解密失败: {}", e);
                std::process::exit(1);
            }
        }
    }
}
