use device_signer::identity::DeviceIdentity;
use sha2::{Sha256, Digest};

fn main() {

    let camera_id = std::env::args().nth(1); // optional: pass camera ID as arg

    let id = match DeviceIdentity::new(camera_id) {
        Ok(id) => id,
        Err(e) => {
            eprintln!("✗ Failed to initialise device identity: {}", e);
            std::process::exit(1);
        }
    };

    println!();
    println!("✓ Hardware Identity Initialized");
    println!("  Ethereum Address : {}", id.info.eth_address);
    println!("  ETH Pubkey (hex) : {}...", &id.info.eth_pubkey_hex[..32]);
    println!("  Camera ID        : {}", id.info.camera_id.as_deref().unwrap_or("Not provided"));
    println!("  Salt Location    : {}", id.info.salt_location);

    let test_data = b"Hello, Hardware device!";
    let sig_bytes = id.sign_data_ecdsa(test_data).unwrap();
    println!();
    println!("✓ ECDSA Signature (Ethereum)");
    println!("  Data      : {:?}", std::str::from_utf8(test_data).unwrap());
    println!("  Signature : {}...", hex::encode(&sig_bytes[..16]));

    let image_hash: [u8; 32] = Sha256::digest(b"test image data").into();
    let sig_info = id.sign_hash_ecdsa(&image_hash).unwrap();
    println!();
    println!("✓ ECDSA Hash Signature");
    println!("  Algorithm : ECDSA_SECP256k1");
    println!("  Address   : {}", id.info.eth_address);
    println!("  Sig (hex) : {}...", &sig_info[..32]);

    // ── Nostr Identity ────────────────────────────────────────────────────────
    println!();
    println!("✓ Nostr Identity (NIP-01 / NIP-19)");
    println!("  Public Key (hex) : {}", id.info.nostr_pubkey_hex);
    println!("  npub             : {}", id.info.nostr_npub);

    // Sign a dummy Nostr event ID (in production: SHA-256 of the NIP-01 event JSON)
    let event_id: [u8; 32] = Sha256::digest(b"[0,\"pubkey\",1700000000,1,[],\"hello nostr\"]").into();
    let schnorr_sig = id.sign_nostr_event(&event_id).unwrap();
    println!("  Schnorr Sig      : {}...", &schnorr_sig[..32]);

    println!();
    println!("{}", "=".repeat(60));
}
