//! Example: Employment Verification Credentials
//!
//! Demonstrates requesting employment credentials with various constraints
//! including employer validation, employment status, and field selection.
//!
//! # Running
//!
//! ```bash
//! cargo run --example employment_verification --features employment-policy
//! ```

use lastid_sdk::{
    ClientBuilder, EmploymentField, EmploymentPolicy, EmploymentStatus, LastIDError, PolicyBuilder,
};

#[tokio::main]
async fn main() -> Result<(), LastIDError> {
    println!("=== Employment Verification 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: Any current employment
    println!("1. Current Employment Verification");
    let policy = EmploymentPolicy::new()
        .with_status(EmploymentStatus::Current)
        .with_callback("https://example.com/callback");

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

    // Example 2: Employment at specific company domain
    println!("2. Employment at Specific Employer Domain");
    let policy = EmploymentPolicy::new()
        .with_employer_domain("acme-corp.com")
        .with_status(EmploymentStatus::Current)
        .with_callback("https://example.com/callback");

    let request = policy.clone().to_request()?;
    let constraints = request.constraints.unwrap();
    println!(
        "   Employer Domain: {}",
        constraints.get("employerDomain").unwrap()
    );
    println!(
        "   Status: {}",
        constraints.get("employmentStatus").unwrap()
    );
    println!();

    // Example 3: Request specific fields (required + optional)
    println!("3. Employment with Specific Fields");
    let policy = EmploymentPolicy::new()
        .with_status(EmploymentStatus::Current)
        .with_fields(&[EmploymentField::Company, EmploymentField::Title]) // Required
        .with_optional_fields(&[
            EmploymentField::Department,
            EmploymentField::StartDate,
        ]) // Optional
        .with_callback("https://example.com/callback");

    let request = policy.clone().to_request()?;
    println!("   Required fields: {:?}", request.required_fields);
    println!("   Optional fields: {:?}", request.optional_fields);
    println!();

    // Example 4: Former employment verification
    println!("4. Former Employment Verification");
    let policy = EmploymentPolicy::new()
        .with_status(EmploymentStatus::Former)
        .with_fields(&[
            EmploymentField::Company,
            EmploymentField::Title,
            EmploymentField::StartDate,
            EmploymentField::EndDate,
        ])
        .with_callback("https://example.com/callback");

    let request = policy.clone().to_request()?;
    let constraints = request.constraints.unwrap();
    println!(
        "   Status: {}",
        constraints.get("employmentStatus").unwrap()
    );
    println!("   Fields: {:?}", request.required_fields);
    println!("   (Includes end date for employment history)");
    println!();

    // Example 5: Any employment history
    println!("5. Any Employment (Current or Former)");
    let policy = EmploymentPolicy::new()
        .with_status(EmploymentStatus::Any)
        .with_callback("https://example.com/callback");

    let request = policy.clone().to_request()?;
    let constraints = request.constraints.unwrap();
    println!(
        "   Status: {}",
        constraints.get("employmentStatus").unwrap()
    );
    println!("   (Accepts current or former employment)");
    println!();

    // Example 6: Combined constraints with required + optional fields
    println!("6. Full Employment Verification");
    let policy = EmploymentPolicy::new()
        .with_employer_domain("tech-company.com")
        .with_status(EmploymentStatus::Current)
        .with_fields(&[EmploymentField::Company, EmploymentField::Title]) // Required
        .with_optional_fields(&[
            EmploymentField::Department,
            EmploymentField::StartDate,
            EmploymentField::Responsibilities,
        ]) // Optional
        .with_callback("https://example.com/callback");

    let request = policy.to_request()?;
    let constraints = request.constraints.unwrap();
    println!("   Complete verification:");
    println!(
        "     Employer Domain: {}",
        constraints.get("employerDomain").unwrap()
    );
    println!(
        "     Status: {}",
        constraints.get("employmentStatus").unwrap()
    );
    println!("     Required: {:?}", request.required_fields);
    println!("     Optional: {:?}", request.optional_fields);

    println!("\n=== Employment Verification Examples Complete ===");
    Ok(())
}
