// Copyright (c) 2026, Salesforce, Inc.,
// All rights reserved.
// For full license text, see the LICENSE.txt file

//! PDK Metrics Library
//!
//! USE AT OWN RISK: The metrics library is experimental and subject to change.
//!
//! Exposes interfaces to handle counters and gauges for custom policies.
//!
//! ## Highlights
//!
//! - Counters and gauges via [`MetricsBuilder`]
//! - Default tags applied automatically: `source=custom-metrics` and `category=<filter_name>`
//! - Name/tag sanitization to ensure valid metric identifiers
//! - Convenience readiness gauge in [`readiness`]
//!

mod builder;
pub mod readiness;

pub use builder::{MetricsBuilder, MetricsInstanceBuilder};
use pdk_core::classy::MetricsHost;
use std::rc::Rc;

/// Trait to interface with the underlying metrics system provided by the Flex engine.
pub trait Metric {
    /// Increase the current metric by the provided value
    fn increase(&self, offset: i64);

    /// Set the current metric to the provided value
    fn set(&self, value: u64);

    /// Return the current value of the metric
    fn get(&self) -> u64;
}

/// Implementation of the [`Metric`] trait.
pub struct MetricsInstance {
    metrics: Rc<dyn MetricsHost>,
    id: u32,
}

impl Metric for MetricsInstance {
    fn increase(&self, offset: i64) {
        self.metrics.increment_metric(self.id, offset);
    }

    fn set(&self, value: u64) {
        self.metrics.record_metric(self.id, value);
    }

    fn get(&self) -> u64 {
        self.metrics.get_metric(self.id)
    }
}
