use serde::{Deserialize, Serialize};

use crate::{AssetAmount, AssetId, WalletRegisterId};

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct TransferDetail {
    /// Transfer to wallet id
    pub to: WalletRegisterId,

    /// Transfer amount
    pub amount: AssetAmount,
}

/// Batch transfer
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct BatchTransfer {
    pub asset_id: AssetId,
    pub items: Vec<TransferDetail>,
}

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

    use super::*;

    #[test]
    fn test_serde() {
        let batch_transfer = BatchTransfer {
            asset_id: 1u8.into(),
            items: vec![
                TransferDetail {
                    to: 1u8.into(),
                    amount: 100u8.into(),
                },
                TransferDetail {
                    to: 2u8.into(),
                    amount: 200u8.into(),
                },
            ],
        };

        // bincode
        let encoded = bincode::serialize(&batch_transfer).unwrap();
        let decoded: BatchTransfer = bincode::deserialize(&encoded).unwrap();
        assert_eq!(batch_transfer, decoded);

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