use serde::{Deserialize, Serialize};
use serde_bytes::ByteBuf;

use crate::PublicKey;

/// Reset the main secret key
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct ResetMainSecret {
    /// Encrypted private key of the new main secret
    pub encrypted_pri_key: ByteBuf,
    /// Public key of the new main secret
    pub l2_pub_key: PublicKey,
}

/// Create API secret
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(transparent)]
pub struct CreateApiSecret(pub PublicKey);

/// Destroy API secret
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(transparent)]
pub struct DestroyApiSecret(pub PublicKey);

#[cfg(test)]
mod tests {
    use ex3_serde::bincode;

    use super::*;

    #[test]
    fn test_reset_main_secret_serde() {
        let reset_main_secret = ResetMainSecret {
            encrypted_pri_key: ByteBuf::from(vec![1, 2, 3]),
            l2_pub_key: PublicKey::from([1u8; 32]),
        };
        // bincode
        let encoded = bincode::serialize(&reset_main_secret).unwrap();
        let decoded: ResetMainSecret = bincode::deserialize(&encoded).unwrap();
        assert_eq!(reset_main_secret, decoded);

        // cbor
        let encoded = bincode::serialize(&reset_main_secret).unwrap();
        let decoded: ResetMainSecret = bincode::deserialize(&encoded).unwrap();
        assert_eq!(reset_main_secret, decoded);
    }

    #[test]
    fn test_create_api_secret_serde() {
        let create_api_secret = CreateApiSecret(PublicKey::from([1u8; 32]));
        // bincode
        let encoded = bincode::serialize(&create_api_secret).unwrap();
        let decoded: CreateApiSecret = bincode::deserialize(&encoded).unwrap();
        assert_eq!(create_api_secret, decoded);

        // cbor
        let encoded = bincode::serialize(&create_api_secret).unwrap();
        let decoded: CreateApiSecret = bincode::deserialize(&encoded).unwrap();
        assert_eq!(create_api_secret, decoded);
    }

    #[test]
    fn test_destroy_api_secret_serde() {
        let destroy_api_secret = DestroyApiSecret(PublicKey::from([1u8; 32]));
        // bincode
        let encoded = bincode::serialize(&destroy_api_secret).unwrap();
        let decoded: DestroyApiSecret = bincode::deserialize(&encoded).unwrap();
        assert_eq!(destroy_api_secret, decoded);

        // cbor
        let encoded = bincode::serialize(&destroy_api_secret).unwrap();
        let decoded: DestroyApiSecret = bincode::deserialize(&encoded).unwrap();
        assert_eq!(destroy_api_secret, decoded);
    }
}
