//! MPC scheme configuration ported from
//! `python/dynamic_wallet_sdk/mpc_config.py`.
//!
//! Wire format for `thresholdSignatureScheme` is `TWO_OF_TWO` /
//! `TWO_OF_THREE` (matches Python). The `MpcSchemeConfig::from` lookup
//! returns the per-share counts the orchestration layer needs.

use dynamic_waas_sdk_core::ThresholdSignatureScheme;

#[derive(Debug, Clone, Copy)]
pub(crate) struct MpcSchemeConfig {
    pub n: u16,
    pub t: u16,
    /// Number of client-side shares the SDK manages.
    pub client_threshold: u16,
}

impl MpcSchemeConfig {
    pub(crate) fn from(scheme: ThresholdSignatureScheme) -> Self {
        match scheme {
            ThresholdSignatureScheme::TwoOfTwo => Self {
                n: 2,
                t: 2,
                client_threshold: 1,
            },
            ThresholdSignatureScheme::TwoOfThree => Self {
                n: 3,
                t: 2,
                client_threshold: 2,
            },
        }
    }
}

/// BIP-44 derivation path for EVM (`m/44'/60'/0'/0/0`). Mirrors
/// `MPC_CHAIN_CONFIG["EVM"]` in Python.
pub const EVM_DERIVATION_PATH: &[u32] = &[44, 60, 0, 0, 0];

/// BIP-44 derivation path for SVM (`m/44'/501'/0'/0/0`). Mirrors
/// `MPC_CHAIN_CONFIG["SVM"]` in Python. `ExportableEd25519` ignores the
/// path internally — kept for API symmetry with ECDSA.
pub const SVM_DERIVATION_PATH: &[u32] = &[44, 501, 0, 0, 0];

/// Public Evervault relay identifier for the preprod keyshares relay.
/// **Not a customer secret.** This value is part of the SDK's published
/// configuration — it tells the SDK which Evervault relay environment to
/// reach, and is identical for every customer. Mirrored verbatim from
/// `python/dynamic_wallet_sdk/constants.py:PREPROD_RELAY_API_KEY`.
/// Customer authentication is handled separately via the per-org API
/// token passed to `authenticate_api_token`.
pub const PREPROD_RELAY_API_KEY: &str =
    "ev:key:1:7j2jIPMzlcKlpDz1RTV6Vadsm8sgmj1VHZzJXqW9ie1dgmyzKmccEGI8BBWU0PaXv:XfF7Ri:ivvRp1"; // NOSONAR — public per-env relay identifier, see doc comment above.

/// Public Evervault relay identifier for the production keyshares relay.
/// Same caveat as `PREPROD_RELAY_API_KEY` — not a customer secret.
pub const PROD_RELAY_API_KEY: &str =
    "ev:key:1:7kRuVm1sE1J5FjGz2ijufy0IkATzrBKEvde0IMLW1dt3xbFcdTdOKHO0vNnJeAjAD:YmAPOP:Jh7DdD"; // NOSONAR — public per-env relay identifier, see doc comment above.

/// Evervault App IDs for the keyshares relay.
pub const PREPROD_RELAY_APP_ID: &str = "app_32d15525a875";
pub const PROD_RELAY_APP_ID: &str = "app_6e12fc400995";

/// Keyshares relay base URLs.
pub const PREPROD_KEYSHARES_RELAY_URL: &str = "https://waas-keyshares-relay.dynamic-preprod.xyz";
pub const PROD_KEYSHARES_RELAY_URL: &str = "https://waas-keyshares-relay.dynamicauth.com";

pub fn relay_app_id_for(env: dynamic_waas_sdk_core::Environment) -> Option<&'static str> {
    use dynamic_waas_sdk_core::Environment;
    match env {
        Environment::Production => Some(PROD_RELAY_APP_ID),
        Environment::Preprod => Some(PREPROD_RELAY_APP_ID),
        _ => None,
    }
}

pub fn keyshares_relay_url_for(env: dynamic_waas_sdk_core::Environment) -> &'static str {
    use dynamic_waas_sdk_core::Environment;
    match env {
        Environment::Production => PROD_KEYSHARES_RELAY_URL,
        _ => PREPROD_KEYSHARES_RELAY_URL,
    }
}

/// Pick the relay API key for an environment.
pub fn relay_api_key_for(env: dynamic_waas_sdk_core::Environment) -> Option<&'static str> {
    use dynamic_waas_sdk_core::Environment;
    match env {
        Environment::Production => Some(PROD_RELAY_API_KEY),
        Environment::Preprod => Some(PREPROD_RELAY_API_KEY),
        _ => None,
    }
}

/// Wire-format string for a threshold scheme; matches Python's enum value.
pub(crate) fn threshold_wire(scheme: ThresholdSignatureScheme) -> &'static str {
    match scheme {
        ThresholdSignatureScheme::TwoOfTwo => "TWO_OF_TWO",
        ThresholdSignatureScheme::TwoOfThree => "TWO_OF_THREE",
    }
}
