//! Example: Trust Network Credentials
//!
//! Demonstrates requesting trust credentials that verify relationships
//! and trust levels in a web-of-trust network.
//!
//! # Running
//!
//! ```bash
//! cargo run --example trust_credential --features trust-policy
//! ```

use lastid_sdk::{ClientBuilder, LastIDError, PolicyBuilder, TrustLevel, TrustPolicy};

#[tokio::main]
async fn main() -> Result<(), LastIDError> {
    println!("=== Trust Network Credential Examples ===\n");

    // Initialize client (demonstrates configuration, actual requests require IDP)
    let _client = ClientBuilder::new()
        .with_auto_config()
        .map_err(|e| {
            eprintln!("Config error: {e}");
            eprintln!("Create lastid.toml with: idp_endpoint = \"https://human.dev.lastid.co\"");
            e
        })?
        .build()?;

    // Example 1: Basic trust verification
    println!("1. Basic Trust Verification");
    let policy = TrustPolicy::new().with_callback("https://example.com/callback");

    let request = policy.clone().to_request()?;
    println!("   Type: {}", request.credential_type);
    println!("   (Any trust level accepted)");
    println!();

    // Example 2: Minimum trust level
    println!("2. Minimum Trust Level (Verified)");
    let policy = TrustPolicy::new()
        .with_minimum_level(TrustLevel::Verified)
        .with_callback("https://example.com/callback");

    let request = policy.clone().to_request()?;
    let constraints = request.constraints.unwrap();
    println!(
        "   Min level: {}",
        constraints.get("minimumTrustLevel").unwrap()
    );
    println!("   (User must have 'verified', 'high', or 'ultimate' trust)");
    println!();

    // Example 3: Trust from specific issuer
    println!("3. Trust from Specific Issuer");
    let policy = TrustPolicy::new()
        .with_trusted_issuer("did:lastid:issuer:authority")
        .with_minimum_level(TrustLevel::Basic)
        .with_callback("https://example.com/callback");

    let request = policy.clone().to_request()?;
    let constraints = request.constraints.unwrap();
    println!(
        "   Trusted issuer: {:?}",
        constraints.get("trustedIssuers").unwrap()
    );
    println!(
        "   Min level: {}",
        constraints.get("minimumTrustLevel").unwrap()
    );
    println!();

    // Example 4: Multiple trusted issuers
    println!("4. Multiple Trusted Issuers");
    let policy = TrustPolicy::new()
        .with_trusted_issuers(&[
            "did:lastid:issuer:ca1",
            "did:lastid:issuer:ca2",
            "did:lastid:issuer:ca3",
        ])
        .with_callback("https://example.com/callback");

    let request = policy.clone().to_request()?;
    let constraints = request.constraints.unwrap();
    let issuers = constraints
        .get("trustedIssuers")
        .unwrap()
        .as_array()
        .unwrap();
    println!("   Trusted issuers ({}):", issuers.len());
    for issuer in issuers {
        println!("     - {}", issuer);
    }
    println!();

    // Example 5: Mutual trust requirement
    println!("5. Mutual Trust Requirement");
    let policy = TrustPolicy::new()
        .require_mutual_trust()
        .with_minimum_level(TrustLevel::Basic)
        .with_callback("https://example.com/callback");

    let request = policy.clone().to_request()?;
    let constraints = request.constraints.unwrap();
    println!(
        "   Mutual trust: {}",
        constraints.get("requireMutual").unwrap()
    );
    println!("   (Both parties must trust each other)");
    println!();

    // Example 6: Trust with recency requirement
    println!("6. Recent Trust Verification");
    let policy = TrustPolicy::new()
        .with_minimum_level(TrustLevel::High)
        .with_max_age_days(30) // 30 days
        .with_callback("https://example.com/callback");

    let request = policy.clone().to_request()?;
    let constraints = request.constraints.unwrap();
    println!(
        "   Min level: {}",
        constraints.get("minimumTrustLevel").unwrap()
    );
    println!(
        "   Max age: {} days",
        constraints.get("maxAgeDays").unwrap()
    );
    println!();

    // Trust levels explained
    println!("=== Trust Levels ===");
    println!("  None     : No trust established");
    println!("  Basic    : Basic trust (mutual connection)");
    println!("  Verified : Verified trust (identity verified by trusted party)");
    println!("  High     : High trust (multiple verifications, long history)");
    println!("  Ultimate : Ultimate trust (institutional verification)");

    println!("\n=== Trust Network Examples Complete ===");
    Ok(())
}
