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

//! Protocol candidate summary DTO.

/// Aggregated protocol candidate diagnostic row.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ProtocolCandidateSummaryDto {
    /// Program id observed in protocol candidates.
    pub program_id: std::string::String,
    /// Optional program name hint.
    pub program_name_hint: std::option::Option<std::string::String>,
    /// Optional candidate protocol.
    pub candidate_protocol: std::option::Option<std::string::String>,
    /// Optional candidate surface.
    pub candidate_surface: std::option::Option<std::string::String>,
    /// Candidate reason.
    pub reason: std::string::String,
    /// Number of candidate rows.
    pub occurrence_count: u64,
    /// Number of distinct transactions.
    pub transaction_count: u64,
    /// Latest observed slot.
    pub last_slot: std::option::Option<u64>,
    /// Latest candidate row id in this group.
    pub latest_candidate_id: i64,
    /// Latest signature in this group.
    pub latest_signature: std::string::String,
    /// Latest candidate creation timestamp.
    pub latest_created_at: chrono::DateTime<chrono::Utc>,
}

impl TryFrom<crate::ProtocolCandidateSummaryEntity> for ProtocolCandidateSummaryDto {
    type Error = crate::Error;

    fn try_from(entity: crate::ProtocolCandidateSummaryEntity) -> Result<Self, Self::Error> {
        let occurrence_count_result = u64::try_from(entity.occurrence_count);
        let occurrence_count = match occurrence_count_result {
            Ok(occurrence_count) => occurrence_count,
            Err(error) => {
                return Err(crate::Error::Db(format!(
                    "cannot convert protocol candidate occurrence_count '{}' to u64: {}",
                    entity.occurrence_count, error
                )));
            },
        };
        let transaction_count_result = u64::try_from(entity.transaction_count);
        let transaction_count = match transaction_count_result {
            Ok(transaction_count) => transaction_count,
            Err(error) => {
                return Err(crate::Error::Db(format!(
                    "cannot convert protocol candidate transaction_count '{}' to u64: {}",
                    entity.transaction_count, error
                )));
            },
        };
        let last_slot = match entity.last_slot {
            Some(last_slot) => {
                let slot_result = u64::try_from(last_slot);
                match slot_result {
                    Ok(slot) => Some(slot),
                    Err(error) => {
                        return Err(crate::Error::Db(format!(
                            "cannot convert protocol candidate last_slot '{}' to u64: {}",
                            last_slot, error
                        )));
                    },
                }
            },
            None => None,
        };
        let latest_created_at_result =
            chrono::DateTime::parse_from_rfc3339(entity.latest_created_at.as_str());
        let latest_created_at = match latest_created_at_result {
            Ok(latest_created_at) => latest_created_at.with_timezone(&chrono::Utc),
            Err(error) => {
                return Err(crate::Error::Db(format!(
                    "cannot parse protocol candidate latest_created_at '{}': {}",
                    entity.latest_created_at, error
                )));
            },
        };
        return Ok(Self {
            program_id: entity.program_id,
            program_name_hint: entity.program_name_hint,
            candidate_protocol: entity.candidate_protocol,
            candidate_surface: entity.candidate_surface,
            reason: entity.reason,
            occurrence_count,
            transaction_count,
            last_slot,
            latest_candidate_id: entity.latest_candidate_id,
            latest_signature: entity.latest_signature,
            latest_created_at,
        });
    }
}
