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

//! Transaction classification DTO.

/// Application-facing transaction classification DTO.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TransactionClassificationDto {
    /// Optional numeric primary key.
    pub id: std::option::Option<i64>,
    /// Related chain transaction id.
    pub transaction_id: i64,
    /// Transaction signature.
    pub signature: std::string::String,
    /// Optional Solana slot.
    pub slot: std::option::Option<u64>,
    /// Stable classification kind.
    pub classification_kind: std::string::String,
    /// Optional primary protocol name.
    pub primary_protocol: std::option::Option<std::string::String>,
    /// Optional primary program id.
    pub primary_program_id: std::option::Option<std::string::String>,
    /// Confidence level from 0 to 100.
    pub confidence_level: i16,
    /// Human-readable reason.
    pub reason: std::string::String,
    /// Serialized JSON evidence.
    pub evidence_json: std::string::String,
    /// Creation timestamp.
    pub created_at: chrono::DateTime<chrono::Utc>,
    /// Update timestamp.
    pub updated_at: chrono::DateTime<chrono::Utc>,
}

impl TransactionClassificationDto {
    /// Creates a new transaction classification DTO.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        transaction_id: i64,
        signature: std::string::String,
        slot: std::option::Option<u64>,
        classification_kind: std::string::String,
        primary_protocol: std::option::Option<std::string::String>,
        primary_program_id: std::option::Option<std::string::String>,
        confidence_level: i16,
        reason: std::string::String,
        evidence_json: std::string::String,
    ) -> Self {
        let now = chrono::Utc::now();
        return Self {
            id: None,
            transaction_id,
            signature,
            slot,
            classification_kind,
            primary_protocol,
            primary_program_id,
            confidence_level,
            reason,
            evidence_json,
            created_at: now,
            updated_at: now,
        };
    }
}

impl TryFrom<crate::TransactionClassificationEntity> for TransactionClassificationDto {
    type Error = crate::Error;

    fn try_from(entity: crate::TransactionClassificationEntity) -> Result<Self, Self::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 transaction classification slot '{}' to u64: {}",
                            slot, error
                        )));
                    },
                }
            },
            None => None,
        };
        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 transaction classification 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 transaction classification updated_at '{}': {}",
                    entity.updated_at, error
                )));
            },
        };
        let confidence_level_result = i16::try_from(entity.confidence_level);
        let confidence_level = match confidence_level_result {
            Ok(confidence_level) => confidence_level,
            Err(error) => {
                return Err(crate::Error::Db(format!(
                    "cannot convert transaction classification confidence_level '{}' to i16: {}",
                    entity.confidence_level, error
                )));
            },
        };
        return Ok(Self {
            id: Some(entity.id),
            transaction_id: entity.transaction_id,
            signature: entity.signature,
            slot,
            classification_kind: entity.classification_kind,
            primary_protocol: entity.primary_protocol,
            primary_program_id: entity.primary_program_id,
            confidence_level,
            reason: entity.reason,
            evidence_json: entity.evidence_json,
            created_at,
            updated_at,
        });
    }
}
