//! WASM-specific tests.
//!
//! These tests run in a browser or Node.js environment via wasm-bindgen-test.
//! They test the WASM bindings and ensure the SDK works correctly in browser contexts.

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

use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);

// =============================================================================
// Module Loading Tests
// =============================================================================

/// Test that the SDK initializes correctly in WASM.
#[wasm_bindgen_test]
fn test_wasm_module_loads() {
    // This test verifies that the WASM module loads without panicking.
    // The panic hook should be installed by the `init()` function.
}

// =============================================================================
// Policy Builder Tests
// =============================================================================

#[wasm_bindgen_test]
fn test_wasm_base_policy_creation() {
    use lastid_sdk::wasm::WasmBaseCredentialPolicy;

    let policy = WasmBaseCredentialPolicy::new();
    assert!(!policy.is_complete());
    assert_eq!(policy.credential_type(), "LastID.Base");
}

#[wasm_bindgen_test]
fn test_wasm_base_policy_fluent_api() {
    use lastid_sdk::wasm::WasmBaseCredentialPolicy;

    let policy = WasmBaseCredentialPolicy::new()
        .with_state("test-state".to_string())
        .with_nonce("test-nonce".to_string())
        .with_callback("https://example.com/callback".to_string());

    assert!(policy.is_complete());
    assert_eq!(policy.credential_type(), "LastID.Base");
}

#[wasm_bindgen_test]
fn test_wasm_base_policy_to_request() {
    use lastid_sdk::policies::PolicyBuilder;
    use lastid_sdk::wasm::WasmBaseCredentialPolicy;

    let policy = WasmBaseCredentialPolicy::new()
        .with_state("csrf-token".to_string())
        .with_nonce("binding-nonce".to_string())
        .with_callback("https://example.com/callback".to_string());

    let request = policy.to_request().expect("Should build request");

    assert_eq!(request.credential_type, "LastID.Base");
    assert_eq!(
        request.callback_url,
        Some("https://example.com/callback".to_string())
    );
    assert_eq!(request.state, Some("csrf-token".to_string()));
    assert_eq!(request.nonce, Some("binding-nonce".to_string()));
}

#[wasm_bindgen_test]
fn test_wasm_base_policy_missing_callback_error() {
    use lastid_sdk::policies::PolicyBuilder;
    use lastid_sdk::wasm::WasmBaseCredentialPolicy;

    let policy = WasmBaseCredentialPolicy::new().with_state("test".to_string());

    let result = policy.to_request();
    assert!(result.is_err());
}

// =============================================================================
// Config Tests
// =============================================================================

#[wasm_bindgen_test]
fn test_wasm_config_creation() {
    use lastid_sdk::wasm::WasmConfig;

    let config =
        WasmConfig::new("https://human.dev.lastid.co".to_string(), "test-client-id".to_string());

    // Config creation should succeed without panicking
    assert!(true, "Config created successfully");
}

#[wasm_bindgen_test]
fn test_wasm_config_with_options() {
    use lastid_sdk::wasm::WasmConfig;

    let config = WasmConfig::with_options(
        "https://human.dev.lastid.co".to_string(),
        "test-client-id".to_string(),
        Some("https://trust.dev.lastid.co".to_string()),
        Some(60),
    );

    assert!(config.is_ok(), "Config with options should succeed");
}

// =============================================================================
// Client Tests
// =============================================================================

#[wasm_bindgen_test]
fn test_wasm_client_creation() {
    use lastid_sdk::wasm::{WasmClient, WasmConfig};

    let config =
        WasmConfig::new("https://human.dev.lastid.co".to_string(), "test-client-id".to_string());

    let client = WasmClient::new(&config);
    assert!(client.is_ok(), "Client creation should succeed");
}

// =============================================================================
// Utility Function Tests
// =============================================================================

#[wasm_bindgen_test]
fn test_wasm_generate_state() {
    use lastid_sdk::wasm::generate_state;

    let state = generate_state();
    assert!(!state.is_empty());
    // UUID v4 format: 8-4-4-4-12 = 36 characters
    assert_eq!(state.len(), 36);
}

#[wasm_bindgen_test]
fn test_wasm_generate_nonce() {
    use lastid_sdk::wasm::generate_nonce;

    let nonce = generate_nonce();
    assert!(!nonce.is_empty());
    // UUID v4 format: 8-4-4-4-12 = 36 characters
    assert_eq!(nonce.len(), 36);
}

#[wasm_bindgen_test]
fn test_wasm_get_version() {
    use lastid_sdk::wasm::get_version;

    let version = get_version();
    assert!(!version.is_empty());
    // Should match Cargo.toml version
    assert!(version.starts_with("0."));
}

#[wasm_bindgen_test]
fn test_wasm_get_credential_types() {
    use lastid_sdk::wasm::get_supported_credential_types;

    let types = get_supported_credential_types();
    assert!(!types.is_empty());

    // Should include at least the base type
    let types_vec: Vec<String> = serde_wasm_bindgen::from_value(types).unwrap();
    assert!(types_vec.contains(&"LastID.Base".to_string()));
}
