//! [`SystemClock`] — production [`Clock`] backed by the system wall clock.

use std::time::{SystemTime, UNIX_EPOCH};

use domain::ports::Clock;

/// Real clock returning the current epoch time in milliseconds.
#[derive(Debug, Default, Clone, Copy)]
pub struct SystemClock;

impl SystemClock {
    /// Creates a new [`SystemClock`].
    #[must_use]
    pub const fn new() -> Self {
        Self
    }
}

impl Clock for SystemClock {
    fn now_millis(&self) -> i64 {
        // Saturating cast is fine: epoch millis fits in i64 until year 292M.
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_millis() as i64)
            .unwrap_or(0)
    }
}
