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

//! Liquidity event DTO.

/// Application-facing normalized liquidity event DTO.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LiquidityEventDto {
    /// Optional numeric primary key.
    pub id: std::option::Option<i64>,
    /// Related DEX id.
    pub dex_id: i64,
    /// Related pool id.
    pub pool_id: i64,
    /// Optional related pair id.
    pub pair_id: std::option::Option<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>,
    /// Liquidity event kind.
    pub event_kind: crate::LiquidityEventKind,
    /// Optional actor wallet.
    pub actor_wallet: std::option::Option<std::string::String>,
    /// Base token id.
    pub base_token_id: i64,
    /// Quote token id.
    pub quote_token_id: i64,
    /// Optional LP token id.
    pub lp_token_id: std::option::Option<i64>,
    /// Base amount as decimal text.
    pub base_amount: std::string::String,
    /// Quote amount as decimal text.
    pub quote_amount: std::string::String,
    /// Optional LP amount as decimal text.
    pub lp_amount: std::option::Option<std::string::String>,
    /// Execution timestamp.
    pub executed_at: chrono::DateTime<chrono::Utc>,
}

impl LiquidityEventDto {
    /// Creates a new liquidity event DTO.
    pub fn new(
        dex_id: i64,
        pool_id: i64,
        pair_id: std::option::Option<i64>,
        signature: std::string::String,
        instruction_index: i64,
        slot: std::option::Option<u64>,
        event_kind: crate::LiquidityEventKind,
        actor_wallet: std::option::Option<std::string::String>,
        base_token_id: i64,
        quote_token_id: i64,
        lp_token_id: std::option::Option<i64>,
        base_amount: std::string::String,
        quote_amount: std::string::String,
        lp_amount: std::option::Option<std::string::String>,
    ) -> Self {
        return Self {
            id: None,
            dex_id,
            pool_id,
            pair_id,
            signature,
            instruction_index,
            slot,
            event_kind,
            actor_wallet,
            base_token_id,
            quote_token_id,
            lp_token_id,
            base_amount,
            quote_amount,
            lp_amount,
            executed_at: chrono::Utc::now(),
        };
    }
}

impl TryFrom<crate::LiquidityEventEntity> for LiquidityEventDto {
    type Error = crate::Error;

    fn try_from(entity: crate::LiquidityEventEntity) -> Result<Self, Self::Error> {
        let event_kind_result = crate::LiquidityEventKind::from_i16(entity.event_kind);
        let event_kind = match event_kind_result {
            Ok(event_kind) => event_kind,
            Err(error) => return Err(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 liquidity 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 liquidity event slot '{}' to u64: {}",
                            slot, error
                        )));
                    },
                }
            },
            None => None,
        };
        return Ok(Self {
            id: Some(entity.id),
            dex_id: entity.dex_id,
            pool_id: entity.pool_id,
            pair_id: entity.pair_id,
            signature: entity.signature,
            instruction_index: entity.instruction_index,
            slot,
            event_kind,
            actor_wallet: entity.actor_wallet,
            base_token_id: entity.base_token_id,
            quote_token_id: entity.quote_token_id,
            lp_token_id: entity.lp_token_id,
            base_amount: entity.base_amount,
            quote_amount: entity.quote_amount,
            lp_amount: entity.lp_amount,
            executed_at,
        });
    }
}
