//! Example: Requesting a Verified Email Credential
//!
//! Demonstrates requesting a LastID.VerifiedEmail credential with optional
//! domain constraints to restrict which email domains are accepted.
//!
//! # Running
//!
//! ```bash
//! cargo run --example verified_email_credential --features verified-email-policy
//! ```

use lastid_sdk::{ClientBuilder, LastIDError, PolicyBuilder, VerifiedEmailPolicy};

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

    // Initialize client
    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: Any verified email
    println!("1. Requesting ANY verified email...");
    let policy = VerifiedEmailPolicy::new().with_callback("https://example.com/callback");

    let request = policy.clone().to_request()?;
    println!("   Type: {}", request.credential_type);
    println!("   Constraints: {:?}", request.constraints);

    let request_id = client.request_credential(policy).await?;
    println!("   Request ID: {}\n", request_id);

    // Example 2: Email with domain constraint
    println!("2. Requesting email with domain constraint...");
    let policy = VerifiedEmailPolicy::new()
        .with_domain_constraint("acme.com")
        .with_callback("https://example.com/callback");

    let request = policy.clone().to_request()?;
    println!("   Type: {}", request.credential_type);
    println!("   Domain: acme.com (only @acme.com emails accepted)");

    let request_id = client.request_credential(policy).await?;
    println!("   Request ID: {}\n", request_id);

    // Example 3: Corporate email validation
    println!("3. Corporate email validation example...");
    let corporate_domains = ["company.com", "subsidiary.company.com"];

    for domain in &corporate_domains {
        let policy = VerifiedEmailPolicy::new()
            .with_domain_constraint(*domain)
            .with_callback("https://example.com/callback");

        println!("   - Domain: {}", domain);
        let _ = policy.to_request()?;
    }

    println!("\n=== Verified Email Examples Complete ===");
    Ok(())
}
