//! Example: Requesting a Persona Credential
//!
//! Demonstrates requesting a LastID.Persona credential with optional fields.
//!
//! # Running
//!
//! ```bash
//! cargo run --example persona_credential --features persona-policy
//! ```

use lastid_sdk::{ClientBuilder, LastIDError, PersonaField, PersonaPolicy, PolicyBuilder};

#[tokio::main]
async fn main() -> Result<(), LastIDError> {
    println!("=== Persona 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()?;

    // Build persona policy requesting specific fields
    // Required fields must be present, optional fields user can choose
    let policy = PersonaPolicy::new()
        .with_fields(&[PersonaField::DisplayName]) // Required
        .with_optional_fields(&[
            PersonaField::Email,
            PersonaField::Phone,
            PersonaField::AvatarUrl,
        ])
        .with_callback("https://example.com/callback");

    println!("Requesting persona credential...");
    println!("  Required: display_name");
    println!("  Optional: email, phone, avatar_url");

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

    // Make the request
    let response = client.request_credential(policy).await?;
    println!("  Request ID: {}", response.request_id);
    println!("  Request URI: {}", response.request_uri());

    // Poll for completion (in production, use webhooks or manual polling)
    println!("\nPolling for user response...");
    let status = client.poll_for_completion(response.request_id()).await?;
    println!("  Status: {:?}", status);

    Ok(())
}
