//! Example: Typed Response Parsing (FR-028)
//!
//! Demonstrates using typed accessors on VerifiedCredential and parsed struct
//! types for type-safe credential field access.
//!
//! # Use Case
//!
//! After verifying a credential presentation, access claims using:
//! 1. Direct typed accessors on VerifiedCredential (email(), phone_number(), etc.)
//! 2. Parsed structs with TryFrom (ParsedPersona, ParsedEmployment, etc.)
//!
//! # Running
//!
//! ```bash
//! cargo run --example typed_response_parsing --features persona-policy,employment-policy
//! ```

use chrono::Utc;
use lastid_sdk::{
    LastIDError,
    trust_registry::IssuerStatus,
    types::{
        VerifiedCredential,
        parsed::{ParsedEmployment, ParsedPersona},
    },
};

fn main() -> Result<(), LastIDError> {
    println!("=== Typed Response Parsing Example ===\n");

    // Simulated verified credential (in production, from verify_presentation())
    let persona_credential = VerifiedCredential {
        subject_did: "did:lastid:user123".into(),
        issuer_did: "did:lastid:issuer456".into(),
        credential_type: "LastID.Persona".into(),
        claims: serde_json::json!({
            "display_name": "John Doe",
            "email": "john@example.com",
            "phone": "+1234567890",
            "company": "Acme Corp",
            "job_title": "Engineer",
            "device_attestation": {
                "has_secure_element": true,
                "has_tee": true,
                "integrity_status": "pass"
            },
            "device_security": {
                "security_level": "high",
                "biometric_used": true,
                "platform": "ios"
            },
            "field_verification": {
                "email": {
                    "verified": true,
                    "provider": "google",
                    "method": "oauth"
                }
            }
        }),
        issued_at: Utc::now(),
        expires_at: Utc::now() + chrono::Duration::hours(24),
        issuer_status: IssuerStatus::Authorized,
        signature_verified: true,
    };

    // Example 1: Direct typed accessors
    println!("1. Direct Typed Accessors on VerifiedCredential");
    println!("   email(): {:?}", persona_credential.email());
    println!("   phone_number(): {:?}", persona_credential.phone_number());
    println!("   display_name(): {:?}", persona_credential.display_name());
    println!("   company(): {:?}", persona_credential.company());
    println!("   job_title(): {:?}", persona_credential.job_title());
    println!("   verified_at(): {:?}", persona_credential.verified_at());
    println!();

    // Example 2: Plural accessors for array fields
    println!("2. Plural Accessors (for array claims)");
    println!("   emails(): {:?}", persona_credential.emails());
    println!(
        "   phone_numbers(): {:?}",
        persona_credential.phone_numbers()
    );
    println!();

    // Example 3: Device attestation accessor
    println!("3. Device Attestation");
    if let Some(attestation) = persona_credential.device_attestation() {
        println!("   has_secure_element: {}", attestation.has_secure_element);
        println!("   has_tee: {}", attestation.has_tee);
        println!("   integrity_status: {}", attestation.integrity_status);
        println!("   is_high_security(): {}", attestation.is_high_security());
    }
    println!();

    // Example 4: Device security accessor
    println!("4. Device Security");
    if let Some(security) = persona_credential.device_security() {
        println!("   security_level: {}", security.security_level);
        println!("   biometric_used: {}", security.biometric_used);
        println!("   platform: {}", security.platform);
        println!("   is_high_security(): {}", security.is_high_security());
    }
    println!();

    // Example 5: Field verification accessor
    println!("5. Field Verification Metadata");
    if let Some(meta) = persona_credential.field_verification("email") {
        println!("   email verified: {}", meta.verified);
        println!("   provider: {:?}", meta.provider);
        println!("   method: {:?}", meta.method);
    }
    println!();

    // Example 6: Parse into typed struct
    println!("6. Parse into ParsedPersona Struct");
    let parsed_persona = ParsedPersona::try_from(&persona_credential)?;
    println!("   display_name: {:?}", parsed_persona.display_name);
    println!("   email: {:?}", parsed_persona.email);
    println!("   company: {:?}", parsed_persona.company);
    println!("   job_title: {:?}", parsed_persona.job_title);
    println!();

    // Example 7: Employment credential parsing
    println!("7. Parse Employment Credential");
    let employment_credential = VerifiedCredential {
        subject_did: "did:lastid:user123".into(),
        issuer_did: "did:lastid:acme-hr".into(),
        credential_type: "LastID.Employment".into(),
        claims: serde_json::json!({
            "company": "Acme Corp",
            "title": "Senior Engineer",
            "department": "Engineering",
            "is_current": true,
            "start_date": "2022-01-15"
        }),
        issued_at: Utc::now(),
        expires_at: Utc::now() + chrono::Duration::hours(24),
        issuer_status: IssuerStatus::Authorized,
        signature_verified: true,
    };

    let parsed_employment = ParsedEmployment::try_from(&employment_credential)?;
    println!("   company: {:?}", parsed_employment.company);
    println!("   title: {:?}", parsed_employment.title);
    println!("   department: {:?}", parsed_employment.department);
    println!("   is_current: {:?}", parsed_employment.is_current);
    println!("   start_date: {:?}", parsed_employment.start_date);

    println!("\n=== Typed Response Parsing Example Complete ===");
    Ok(())
}
