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

//! Launch attribution DTO.

/// Application-facing launch attribution DTO.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LaunchAttributionDto {
    /// Optional numeric primary key.
    pub id: std::option::Option<i64>,
    /// Related launch surface id.
    pub launch_surface_id: i64,
    /// Related transaction id.
    pub transaction_id: i64,
    /// Related decoded event id.
    pub decoded_event_id: i64,
    /// Optional related pool id.
    pub pool_id: std::option::Option<i64>,
    /// Optional related pair id.
    pub pair_id: std::option::Option<i64>,
    /// Optional related matched key id.
    pub matched_key_id: std::option::Option<i64>,
    /// Decoded protocol name.
    pub protocol_name: std::string::String,
    /// Match kind used for attribution.
    pub match_kind: std::string::String,
    /// Matched value used for attribution.
    pub matched_value: std::string::String,
    /// Attribution timestamp.
    pub attributed_at: chrono::DateTime<chrono::Utc>,
    /// Update timestamp.
    pub updated_at: chrono::DateTime<chrono::Utc>,
}

impl LaunchAttributionDto {
    /// Creates a new launch attribution DTO.
    pub fn new(
        launch_surface_id: i64,
        transaction_id: i64,
        decoded_event_id: i64,
        pool_id: std::option::Option<i64>,
        pair_id: std::option::Option<i64>,
        matched_key_id: std::option::Option<i64>,
        protocol_name: std::string::String,
        match_kind: std::string::String,
        matched_value: std::string::String,
    ) -> Self {
        let now = chrono::Utc::now();
        return Self {
            id: None,
            launch_surface_id,
            transaction_id,
            decoded_event_id,
            pool_id,
            pair_id,
            matched_key_id,
            protocol_name,
            match_kind,
            matched_value,
            attributed_at: now,
            updated_at: now,
        };
    }
}

impl TryFrom<crate::LaunchAttributionEntity> for LaunchAttributionDto {
    type Error = crate::Error;

    fn try_from(entity: crate::LaunchAttributionEntity) -> Result<Self, Self::Error> {
        let attributed_at_result = chrono::DateTime::parse_from_rfc3339(&entity.attributed_at);
        let attributed_at = match attributed_at_result {
            Ok(attributed_at) => attributed_at.with_timezone(&chrono::Utc),
            Err(error) => {
                return Err(crate::Error::Db(format!(
                    "cannot parse launch_attribution attributed_at '{}': {}",
                    entity.attributed_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 launch_attribution updated_at '{}': {}",
                    entity.updated_at, error
                )));
            },
        };
        return Ok(Self {
            id: Some(entity.id),
            launch_surface_id: entity.launch_surface_id,
            transaction_id: entity.transaction_id,
            decoded_event_id: entity.decoded_event_id,
            pool_id: entity.pool_id,
            pair_id: entity.pair_id,
            matched_key_id: entity.matched_key_id,
            protocol_name: entity.protocol_name,
            match_kind: entity.match_kind,
            matched_value: entity.matched_value,
            attributed_at,
            updated_at,
        });
    }
}
