//! Roll three signed dice, then verify and re-fetch by serial number.
//!
//! Run with: `RANDOM_ORG_API_KEY=... cargo run --example signed_dice`

use randomorg_rpc::{Client, IntegersParams, SignedExtras};
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), randomorg_rpc::Error> {
    let key = std::env::var("RANDOM_ORG_API_KEY").expect("RANDOM_ORG_API_KEY missing");
    let client = Client::new(key);

    let extras = SignedExtras::new().user_data(json!({"event": "raffle-2026-05"}));
    let signed = client
        .generate_signed_integers(IntegersParams::new(3, 1, 6), extras)
        .await?;

    let dice: Vec<i64> = signed.data().unwrap();
    let serial = signed.serial_number().expect("serialNumber missing");
    println!("dice: {dice:?}  (serial #{serial})");

    let v = client
        .verify_signature(&signed.random.0, &signed.signature)
        .await?;
    println!("server says authentic: {}", v.authenticity);

    let url = client.create_url(&signed.random.0, &signed.signature)?;
    println!("verification URL: {url}");
    Ok(())
}
