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

//! Application-facing decoded DEX event DTO.

/// Application-facing decoded DEX event DTO.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct DexDecodedEventDto {
    /// Optional numeric primary key.
    pub id: std::option::Option<i64>,
    /// Parent transaction id.
    pub transaction_id: i64,
    /// Optional parent instruction id.
    pub instruction_id: std::option::Option<i64>,
    /// Decoded protocol name.
    pub protocol_name: std::string::String,
    /// Program id that produced the decoded event.
    pub program_id: std::string::String,
    /// Event kind.
    pub event_kind: std::string::String,
    /// Optional decoded pool account.
    pub pool_account: std::option::Option<std::string::String>,
    /// Optional decoded lp mint.
    pub lp_mint: std::option::Option<std::string::String>,
    /// Optional decoded token A mint.
    pub token_a_mint: std::option::Option<std::string::String>,
    /// Optional decoded token B mint.
    pub token_b_mint: std::option::Option<std::string::String>,
    /// Optional decoded market account.
    pub market_account: std::option::Option<std::string::String>,
    /// Serialized decoded payload.
    pub payload_json: std::string::String,
    /// Creation timestamp.
    pub created_at: chrono::DateTime<chrono::Utc>,
}

impl DexDecodedEventDto {
    /// Creates a new decoded DEX event DTO.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        transaction_id: i64,
        instruction_id: std::option::Option<i64>,
        protocol_name: std::string::String,
        program_id: std::string::String,
        event_kind: std::string::String,
        pool_account: std::option::Option<std::string::String>,
        lp_mint: std::option::Option<std::string::String>,
        token_a_mint: std::option::Option<std::string::String>,
        token_b_mint: std::option::Option<std::string::String>,
        market_account: std::option::Option<std::string::String>,
        payload_json: std::string::String,
    ) -> Self {
        return Self {
            id: None,
            transaction_id,
            instruction_id,
            protocol_name,
            program_id,
            event_kind,
            pool_account,
            lp_mint,
            token_a_mint,
            token_b_mint,
            market_account,
            payload_json,
            created_at: chrono::Utc::now(),
        };
    }
}

impl TryFrom<crate::DexDecodedEventEntity> for DexDecodedEventDto {
    type Error = crate::Error;

    fn try_from(entity: crate::DexDecodedEventEntity) -> Result<Self, Self::Error> {
        let created_at_result = chrono::DateTime::parse_from_rfc3339(&entity.created_at);
        let created_at = match created_at_result {
            Ok(created_at) => created_at.with_timezone(&chrono::Utc),
            Err(error) => {
                return Err(crate::Error::Db(format!(
                    "cannot parse dex decoded event created_at '{}': {}",
                    entity.created_at, error
                )));
            },
        };
        return Ok(Self {
            id: Some(entity.id),
            transaction_id: entity.transaction_id,
            instruction_id: entity.instruction_id,
            protocol_name: entity.protocol_name,
            program_id: entity.program_id,
            event_kind: entity.event_kind,
            pool_account: entity.pool_account,
            lp_mint: entity.lp_mint,
            token_a_mint: entity.token_a_mint,
            token_b_mint: entity.token_b_mint,
            market_account: entity.market_account,
            payload_json: entity.payload_json,
            created_at,
        });
    }
}
