//! WASM utility functions.

#![cfg(target_arch = "wasm32")]

use wasm_bindgen::prelude::*;

use crate::bindings::CredentialTypeInfo;

/// Generate a unique state parameter for CSRF protection.
#[wasm_bindgen(js_name = generateState)]
pub fn generate_state() -> String {
    uuid::Uuid::new_v4().to_string()
}

/// Generate a unique nonce for replay protection.
#[wasm_bindgen(js_name = generateNonce)]
pub fn generate_nonce() -> String {
    uuid::Uuid::new_v4().to_string()
}

/// Get all supported credential types.
#[wasm_bindgen(js_name = getSupportedCredentialTypes)]
pub fn get_supported_credential_types() -> JsValue {
    let types = vec![
        CredentialTypeInfo {
            type_name: "LastID.Base".into(),
            display_name: "Base Credential".into(),
            description: "Basic identity verification with user DID".into(),
            fields: vec![],
        },
        CredentialTypeInfo {
            type_name: "LastID.Persona".into(),
            display_name: "Persona".into(),
            description: "User profile information".into(),
            fields: vec![
                "given_name",
                "family_name",
                "display_name",
                "avatar_url",
                "bio",
            ]
            .into_iter()
            .map(String::from)
            .collect(),
        },
        CredentialTypeInfo {
            type_name: "LastID.VerifiedEmail".into(),
            display_name: "Verified Email".into(),
            description: "Verified email address".into(),
            fields: vec!["domain"].into_iter().map(String::from).collect(),
        },
        CredentialTypeInfo {
            type_name: "LastID.VerifiedPhone".into(),
            display_name: "Verified Phone".into(),
            description: "Verified phone number".into(),
            fields: vec!["country"].into_iter().map(String::from).collect(),
        },
        CredentialTypeInfo {
            type_name: "LastID.VerifiedPersona".into(),
            display_name: "Verified Persona".into(),
            description: "KYC-verified identity".into(),
            fields: vec![
                "legal_name",
                "date_of_birth",
                "address",
                "nationality",
                "document_number",
            ]
            .into_iter()
            .map(String::from)
            .collect(),
        },
        CredentialTypeInfo {
            type_name: "LastID.Employment".into(),
            display_name: "Employment".into(),
            description: "Employment verification".into(),
            fields: vec![
                "employer_name",
                "job_title",
                "department",
                "start_date",
                "employment_type",
            ]
            .into_iter()
            .map(String::from)
            .collect(),
        },
        CredentialTypeInfo {
            type_name: "LastID.Trust".into(),
            display_name: "Trust Score".into(),
            description: "Trust level credential".into(),
            fields: vec!["trust_level", "trust_domains"]
                .into_iter()
                .map(String::from)
                .collect(),
        },
        CredentialTypeInfo {
            type_name: "LastID.AgeProof".into(),
            display_name: "Age Proof".into(),
            description: "Age verification without revealing date of birth".into(),
            fields: vec!["comparison", "age"]
                .into_iter()
                .map(String::from)
                .collect(),
        },
    ];
    serde_wasm_bindgen::to_value(&types).unwrap()
}

/// Get the SDK version.
#[wasm_bindgen(js_name = getVersion)]
pub fn get_version() -> String {
    env!("CARGO_PKG_VERSION").to_string()
}
