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

//! Application-facing normalized chain slot DTO.

/// Application-facing normalized chain slot DTO.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ChainSlotDto {
    /// Slot number.
    pub slot: u64,
    /// Optional parent slot number.
    pub parent_slot: std::option::Option<u64>,
    /// Optional block time in unix seconds.
    pub block_time_unix: std::option::Option<i64>,
    /// Creation timestamp.
    pub created_at: chrono::DateTime<chrono::Utc>,
    /// Update timestamp.
    pub updated_at: chrono::DateTime<chrono::Utc>,
}

impl ChainSlotDto {
    /// Creates a new chain slot DTO.
    pub fn new(
        slot: u64,
        parent_slot: std::option::Option<u64>,
        block_time_unix: std::option::Option<i64>,
    ) -> Self {
        let now = chrono::Utc::now();
        return Self {
            slot,
            parent_slot,
            block_time_unix,
            created_at: now,
            updated_at: now,
        };
    }
}

impl TryFrom<crate::ChainSlotEntity> for ChainSlotDto {
    type Error = crate::Error;

    fn try_from(entity: crate::ChainSlotEntity) -> Result<Self, Self::Error> {
        let slot_result = u64::try_from(entity.slot);
        let slot = match slot_result {
            Ok(slot) => slot,
            Err(error) => {
                return Err(crate::Error::Db(format!(
                    "cannot convert chain slot '{}' to u64: {}",
                    entity.slot, error
                )));
            },
        };
        let parent_slot = match entity.parent_slot {
            Some(parent_slot) => {
                let parent_slot_result = u64::try_from(parent_slot);
                match parent_slot_result {
                    Ok(parent_slot) => Some(parent_slot),
                    Err(error) => {
                        return Err(crate::Error::Db(format!(
                            "cannot convert chain parent_slot '{}' to u64: {}",
                            parent_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 chain slot 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 chain slot updated_at '{}': {}",
                    entity.updated_at, error
                )));
            },
        };
        return Ok(Self {
            slot,
            parent_slot,
            block_time_unix: entity.block_time_unix,
            created_at,
            updated_at,
        });
    }
}
