//! Diagnostics and health check methods for [`TidalDb`].

use std::sync::Arc;
use std::sync::atomic::Ordering;

use super::{MetricsState, TidalDb};

impl TidalDb {
    /// Returns a reference to the shared metrics state.
    #[must_use]
    #[allow(clippy::missing_const_for_fn)] // Arc field prevents const in practice
    pub fn metrics(&self) -> &Arc<MetricsState> {
        &self.metrics
    }

    /// Access the load detector for metrics and health check reporting.
    #[must_use]
    #[allow(clippy::missing_const_for_fn)] // Arc field prevents const in practice
    pub fn load_detector(&self) -> &crate::load::LoadDetector {
        &self.load_detector
    }

    /// Returns `Ok(())` if the database is initialized and operational.
    ///
    /// # Errors
    ///
    /// Returns an error if the database has been closed or an internal
    /// check fails.
    #[tracing::instrument(skip(self))]
    pub fn health_check(&self) -> crate::Result<()> {
        if self.closed.load(Ordering::Acquire) {
            self.metrics.health_ok.store(false, Ordering::Release);
            return Err(crate::TidalError::internal(
                "health_check",
                "database is closed",
            ));
        }
        Ok(())
    }
}
