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

//! Pool origin DTO.

/// Application-facing pool-origin DTO.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PoolOriginDto {
    /// 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>,
    /// Optional related pool listing id.
    pub pool_listing_id: std::option::Option<i64>,
    /// Founding transaction id in the local model.
    pub founding_transaction_id: i64,
    /// Founding decoded-event id in the local model.
    pub founding_decoded_event_id: i64,
    /// Founding signature in the local model.
    pub founding_signature: std::string::String,
    /// Founding protocol name.
    pub founding_protocol_name: std::string::String,
    /// Founding event kind.
    pub founding_event_kind: std::string::String,
    /// Discovery source kind.
    pub source_kind: crate::ObservationSourceKind,
    /// Optional source endpoint logical name.
    pub source_endpoint_name: std::option::Option<std::string::String>,
    /// Optional related launch attribution id.
    pub launch_attribution_id: std::option::Option<i64>,
    /// Creation timestamp.
    pub created_at: chrono::DateTime<chrono::Utc>,
    /// Update timestamp.
    pub updated_at: chrono::DateTime<chrono::Utc>,
}

impl PoolOriginDto {
    /// Creates a new pool-origin DTO.
    pub fn new(
        dex_id: i64,
        pool_id: i64,
        pair_id: std::option::Option<i64>,
        pool_listing_id: std::option::Option<i64>,
        founding_transaction_id: i64,
        founding_decoded_event_id: i64,
        founding_signature: std::string::String,
        founding_protocol_name: std::string::String,
        founding_event_kind: std::string::String,
        source_kind: crate::ObservationSourceKind,
        source_endpoint_name: std::option::Option<std::string::String>,
        launch_attribution_id: std::option::Option<i64>,
    ) -> Self {
        let now = chrono::Utc::now();
        return Self {
            id: None,
            dex_id,
            pool_id,
            pair_id,
            pool_listing_id,
            founding_transaction_id,
            founding_decoded_event_id,
            founding_signature,
            founding_protocol_name,
            founding_event_kind,
            source_kind,
            source_endpoint_name,
            launch_attribution_id,
            created_at: now,
            updated_at: now,
        };
    }
}

impl TryFrom<crate::PoolOriginEntity> for PoolOriginDto {
    type Error = crate::Error;

    fn try_from(entity: crate::PoolOriginEntity) -> 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 pool_origin 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 pool_origin updated_at '{}': {}",
                    entity.updated_at, error
                )));
            },
        };
        return Ok(Self {
            id: Some(entity.id),
            dex_id: entity.dex_id,
            pool_id: entity.pool_id,
            pair_id: entity.pair_id,
            pool_listing_id: entity.pool_listing_id,
            founding_transaction_id: entity.founding_transaction_id,
            founding_decoded_event_id: entity.founding_decoded_event_id,
            founding_signature: entity.founding_signature,
            founding_protocol_name: entity.founding_protocol_name,
            founding_event_kind: entity.founding_event_kind,
            source_kind,
            source_endpoint_name: entity.source_endpoint_name,
            launch_attribution_id: entity.launch_attribution_id,
            created_at,
            updated_at,
        });
    }
}
