// Copyright (c) Huawei Technologies Co., Ltd. 2026-2026. All rights reserved.

//! Protocol constants shared between KDC Agent and Proxy.

pub const PSK_LEN: usize = 32;

#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum CommonError {
    #[error("success")]
    Ok = 0,
    #[error("request parameter error")]
    RequestError = 1,
    #[error("PSK mismatch")]
    PskMismatchError = 2,
    #[error("network error")]
    NetworkError = 3,
    #[error("config error")]
    ConfigError = 4,
    #[error("internal error")]
    InternalError = 5,
}

pub const MAX_REQUEST_SIZE: usize = 20 * 1024;
pub const MAX_INNER_REQUEST_SIZE: usize = 100 * 1024;

pub const TRUSTED_ENV_TOKEN_REQ: u32 = 0x0010;
pub const TRUSTED_ENV_VERIFY_REQ: u32 = 0x0011;
pub const TRUSTED_REGISTER_PSK_REQ: u32 = 0x0012;

pub const AGENT_CONF_PATH: &str = "/etc/kdc_agent/agent_conf.json";

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use super::*;

    // Test: PSK_LEN must be exactly 32 bytes (AES-256 key size)
    #[test]
    fn test_psk_len() {
        assert_eq!(PSK_LEN, 32);
    }

    // Test: CommonError discriminants match their documented integer values
    #[test]
    fn test_common_error_values() {
        assert_eq!(CommonError::Ok as u32, 0);
        assert_eq!(CommonError::RequestError as u32, 1);
        assert_eq!(CommonError::PskMismatchError as u32, 2);
        assert_eq!(CommonError::NetworkError as u32, 3);
        assert_eq!(CommonError::ConfigError as u32, 4);
        assert_eq!(CommonError::InternalError as u32, 5);
    }

    // Test: Request type constants match their expected protocol values
    #[test]
    fn test_business_constants() {
        assert_eq!(TRUSTED_ENV_TOKEN_REQ, 0x0010);
        assert_eq!(TRUSTED_ENV_VERIFY_REQ, 0x0011);
        assert_eq!(TRUSTED_REGISTER_PSK_REQ, 0x0012);
    }

    // Test: Each request type constant must be unique to avoid protocol confusion
    #[test]
    fn test_business_constants_are_distinct() {
        let consts = [
            TRUSTED_ENV_TOKEN_REQ,
            TRUSTED_ENV_VERIFY_REQ,
            TRUSTED_REGISTER_PSK_REQ,
        ];
        for i in 0..consts.len() {
            for j in (i + 1)..consts.len() {
                assert_ne!(consts[i], consts[j]);
            }
        }
    }

    // Test: AGENT_CONF_PATH points to the expected configuration file
    #[test]
    fn test_agent_conf_path() {
        assert_eq!(AGENT_CONF_PATH, "/etc/kdc_agent/agent_conf.json");
    }

    // Test: MAX_REQUEST_SIZE is a reasonable non-zero value
    #[test]
    fn test_max_request_size() {
        assert_eq!(MAX_REQUEST_SIZE, 20480);
    }

    // Test: MAX_REQUEST_SIZE is a reasonable non-zero value
    #[test]
    fn test_max_inner_request_size() {
        assert_eq!(MAX_INNER_REQUEST_SIZE, 102400);
    }

    // Test: CommonError Display trait produces the expected error messages
    #[test]
    fn test_common_error_display() {
        assert_eq!(CommonError::Ok.to_string(), "success");
        assert_eq!(CommonError::RequestError.to_string(), "request parameter error");
        assert_eq!(CommonError::PskMismatchError.to_string(), "PSK mismatch");
        assert_eq!(CommonError::NetworkError.to_string(), "network error");
        assert_eq!(CommonError::ConfigError.to_string(), "config error");
        assert_eq!(CommonError::InternalError.to_string(), "internal error");
    }
}