use argon2::{
    Argon2,
    password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString, rand_core::OsRng},
};
use std::env;

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

    if args.len() != 3 {
        eprintln!("Usage: {} <password> <hash>", args[0]);
        eprintln!(
            "Example: {} mypassword '$argon2id$v=19$m=19456,t=2,p=1$...'",
            args[0]
        );
        std::process::exit(1);
    }

    let password = &args[1];
    let hash = &args[2];
    let argon2 = Argon2::default();

    // Test verification
    match PasswordHash::new(hash) {
        Ok(parsed_hash) => match argon2.verify_password(password.as_bytes(), &parsed_hash) {
            Ok(_) => println!("✅ Password verification successful!"),
            Err(_) => println!("❌ Password verification failed!"),
        },
        Err(e) => {
            eprintln!("❌ Error parsing hash: {}", e);
            std::process::exit(1);
        }
    }

    // Also test our service implementation
    println!("\nTesting PasswordService implementation:");

    // Generate a new hash
    let salt = SaltString::generate(&mut OsRng);
    match argon2.hash_password(password.as_bytes(), &salt) {
        Ok(new_hash) => {
            println!("Generated hash: {}", new_hash);

            // Verify the new hash
            match PasswordHash::new(&new_hash.to_string()) {
                Ok(parsed_new_hash) => {
                    match argon2.verify_password(password.as_bytes(), &parsed_new_hash) {
                        Ok(_) => println!("✅ New hash verification successful!"),
                        Err(_) => println!("❌ New hash verification failed!"),
                    }
                }
                Err(e) => {
                    eprintln!("❌ Error parsing new hash: {}", e);
                }
            }
        }
        Err(e) => {
            eprintln!("❌ Error generating new hash: {}", e);
        }
    }
}
