//! Generate a properly signed test assertion for the validation script.
//!
//! Usage:
//!   cargo run --package stemedb-api --example gen_test_assertion
//!
//! Output:
//!   JSON assertion body with valid Ed25519 signature, ready for curl.

#![allow(clippy::print_stdout)]
#![allow(clippy::unwrap_used)]

use ed25519_dalek::{Signer, SigningKey};
use rand::rngs::OsRng;

fn main() {
    // Generate a new keypair
    let mut csprng = OsRng;
    let signing_key = SigningKey::generate(&mut csprng);
    let verifying_key = signing_key.verifying_key();

    let subject = "StemeDB_Validation";
    let predicate = "test_status";

    // Sign the message (format: "{subject}:{predicate}")
    let message = format!("{}:{}", subject, predicate);
    let signature = signing_key.sign(message.as_bytes());

    let timestamp = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);

    let json = serde_json::json!({
        "subject": subject,
        "predicate": predicate,
        "object": {"type": "Text", "value": "working"},
        "confidence": 0.99,
        "source_class": "Expert",
        "lifecycle": "Approved",
        "signatures": [{
            "agent_id": hex::encode(verifying_key.to_bytes()),
            "signature": hex::encode(signature.to_bytes()),
            "timestamp": timestamp
        }],
        "source_hash": "0".repeat(64)
    });

    println!("{}", serde_json::to_string_pretty(&json).unwrap());
}
