//! Metric helpers for the state-tree crate.

use metrics::{counter, histogram};

// --- Commit failure reasons ---

pub(crate) const REASON_BATCH_TOO_LARGE: &str = "batch_too_large";
pub(crate) const REASON_VALUE_TOO_LARGE: &str = "value_too_large";
pub(crate) const REASON_JMT: &str = "jmt";

// --- Proof-cache outcome labels ---

pub(crate) const OUTCOME_MISS_EMPTY: &str = "miss_empty";
pub(crate) const OUTCOME_HIT_FRESH: &str = "hit_fresh";
pub(crate) const OUTCOME_HIT_STALE_EVICTED: &str = "hit_stale_evicted";
pub(crate) const OUTCOME_MISS_VERSION_CHANGED: &str = "miss_version_changed";

// --- Commit operation labels ---

pub(crate) const OP_PUT: &str = "put";
pub(crate) const OP_DELETE: &str = "delete";
pub(crate) const OP_BATCH: &str = "batch";

// --- Commit metrics ---

pub(crate) fn commit_failure(reason: &'static str) {
    counter!("state_tree_commit_failures_total", "reason" => reason).increment(1);
}

pub(crate) fn commit_success(op: &'static str, duration_secs: f64, batch_size: usize, value_bytes: u64) {
    histogram!("state_tree_commit_duration_seconds", "op" => op).record(duration_secs);
    histogram!("state_tree_batch_size", "op" => op).record(batch_size as f64);
    histogram!("state_tree_value_bytes", "op" => op).record(value_bytes as f64);
    counter!("state_tree_commits_total", "op" => op).increment(1);
}

// --- Proof-cache metrics ---

pub(crate) fn cache_outcome(outcome: &'static str) {
    counter!("proof_cache_outcomes_total", "outcome" => outcome).increment(1);
}

pub(crate) fn cache_invalidation() {
    counter!("proof_cache_invalidations_total").increment(1);
}
