// file: kb_lib/src/db/dtos/token_mint_event.rs

//! Token mint event DTO.

/// Application-facing normalized token mint event DTO.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TokenMintEventDto {
    /// Optional numeric primary key.
    pub id: std::option::Option<i64>,
    /// Related token id.
    pub token_id: i64,
    /// Transaction signature.
    pub signature: std::string::String,
    /// Instruction index inside the transaction.
    pub instruction_index: i64,
    /// Optional slot number.
    pub slot: std::option::Option<u64>,
    /// Optional mint authority wallet.
    pub authority_wallet: std::option::Option<std::string::String>,
    /// Optional destination wallet.
    pub destination_wallet: std::option::Option<std::string::String>,
    /// Minted amount as decimal text.
    pub amount: std::string::String,
    /// Optional supply after mint as decimal text.
    pub supply_after: std::option::Option<std::string::String>,
    /// Execution timestamp.
    pub executed_at: chrono::DateTime<chrono::Utc>,
}

impl TokenMintEventDto {
    /// Creates a new token mint event DTO.
    pub fn new(
        token_id: i64,
        signature: std::string::String,
        instruction_index: i64,
        slot: std::option::Option<u64>,
        authority_wallet: std::option::Option<std::string::String>,
        destination_wallet: std::option::Option<std::string::String>,
        amount: std::string::String,
        supply_after: std::option::Option<std::string::String>,
    ) -> Self {
        return Self {
            id: None,
            token_id,
            signature,
            instruction_index,
            slot,
            authority_wallet,
            destination_wallet,
            amount,
            supply_after,
            executed_at: chrono::Utc::now(),
        };
    }
}

impl TryFrom<crate::TokenMintEventEntity> for TokenMintEventDto {
    type Error = crate::Error;

    fn try_from(entity: crate::TokenMintEventEntity) -> Result<Self, Self::Error> {
        let executed_at_result = chrono::DateTime::parse_from_rfc3339(&entity.executed_at);
        let executed_at = match executed_at_result {
            Ok(executed_at) => executed_at.with_timezone(&chrono::Utc),
            Err(error) => {
                return Err(crate::Error::Db(format!(
                    "cannot parse token mint event executed_at '{}': {}",
                    entity.executed_at, error
                )));
            },
        };
        let slot = match entity.slot {
            Some(slot) => {
                let slot_result = u64::try_from(slot);
                match slot_result {
                    Ok(slot) => Some(slot),
                    Err(error) => {
                        return Err(crate::Error::Db(format!(
                            "cannot convert token mint event slot '{}' to u64: {}",
                            slot, error
                        )));
                    },
                }
            },
            None => None,
        };
        return Ok(Self {
            id: Some(entity.id),
            token_id: entity.token_id,
            signature: entity.signature,
            instruction_index: entity.instruction_index,
            slot,
            authority_wallet: entity.authority_wallet,
            destination_wallet: entity.destination_wallet,
            amount: entity.amount,
            supply_after: entity.supply_after,
            executed_at,
        });
    }
}
