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

//! Wallet-holding DTO.

/// Application-facing wallet-holding DTO.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct WalletHoldingDto {
    /// Optional numeric primary key.
    pub id: std::option::Option<i64>,
    /// Related wallet id.
    pub wallet_id: i64,
    /// Related token id.
    pub token_id: i64,
    /// First transaction id that observed this wallet/token relationship.
    pub first_transaction_id: i64,
    /// Last transaction id that observed this wallet/token relationship.
    pub last_transaction_id: i64,
    /// Optional last decoded-event id.
    pub last_decoded_event_id: std::option::Option<i64>,
    /// Optional last related pool id.
    pub last_pool_id: std::option::Option<i64>,
    /// Optional last related pair id.
    pub last_pair_id: std::option::Option<i64>,
    /// Optional last observed role.
    pub last_role: std::option::Option<std::string::String>,
    /// Optional last observed raw balance-like value.
    pub balance_raw: std::option::Option<std::string::String>,
    /// Optional last observed slot.
    pub last_slot_observed: std::option::Option<i64>,
    /// Observation source kind.
    pub source_kind: crate::ObservationSourceKind,
    /// Optional logical source endpoint name.
    pub source_endpoint_name: std::option::Option<std::string::String>,
    /// Creation timestamp.
    pub created_at: chrono::DateTime<chrono::Utc>,
    /// Update timestamp.
    pub updated_at: chrono::DateTime<chrono::Utc>,
}

impl WalletHoldingDto {
    /// Creates a new wallet-holding DTO.
    pub fn new(
        wallet_id: i64,
        token_id: i64,
        first_transaction_id: i64,
        last_transaction_id: i64,
        last_decoded_event_id: std::option::Option<i64>,
        last_pool_id: std::option::Option<i64>,
        last_pair_id: std::option::Option<i64>,
        last_role: std::option::Option<std::string::String>,
        balance_raw: std::option::Option<std::string::String>,
        last_slot_observed: std::option::Option<i64>,
        source_kind: crate::ObservationSourceKind,
        source_endpoint_name: std::option::Option<std::string::String>,
    ) -> Self {
        let now = chrono::Utc::now();
        return Self {
            id: None,
            wallet_id,
            token_id,
            first_transaction_id,
            last_transaction_id,
            last_decoded_event_id,
            last_pool_id,
            last_pair_id,
            last_role,
            balance_raw,
            last_slot_observed,
            source_kind,
            source_endpoint_name,
            created_at: now,
            updated_at: now,
        };
    }
}

impl TryFrom<crate::WalletHoldingEntity> for WalletHoldingDto {
    type Error = crate::Error;

    fn try_from(entity: crate::WalletHoldingEntity) -> Result<Self, Self::Error> {
        let source_kind_result = crate::ObservationSourceKind::from_i16(entity.source_kind);
        let source_kind = match source_kind_result {
            Ok(source_kind) => source_kind,
            Err(error) => return Err(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 wallet_holding created_at '{}': {}",
                    entity.created_at, error
                )));
            },
        };
        let updated_at_result = chrono::DateTime::parse_from_rfc3339(&entity.updated_at);
        let updated_at = match updated_at_result {
            Ok(updated_at) => updated_at.with_timezone(&chrono::Utc),
            Err(error) => {
                return Err(crate::Error::Db(format!(
                    "cannot parse wallet_holding updated_at '{}': {}",
                    entity.updated_at, error
                )));
            },
        };
        return Ok(Self {
            id: Some(entity.id),
            wallet_id: entity.wallet_id,
            token_id: entity.token_id,
            first_transaction_id: entity.first_transaction_id,
            last_transaction_id: entity.last_transaction_id,
            last_decoded_event_id: entity.last_decoded_event_id,
            last_pool_id: entity.last_pool_id,
            last_pair_id: entity.last_pair_id,
            last_role: entity.last_role,
            balance_raw: entity.balance_raw,
            last_slot_observed: entity.last_slot_observed,
            source_kind,
            source_endpoint_name: entity.source_endpoint_name,
            created_at,
            updated_at,
        });
    }
}
