#include "meter_health.h"

MeterHealthEvaluation meter_health_evaluate(MeterHealthState &state,
                                             bool has_snapshot,
                                             uint32_t age_ms,
                                             uint32_t max_age_ms) {
  MeterHealthEvaluation evaluation = {};
  evaluation.ok = has_snapshot && age_ms <= max_age_ms;
  evaluation.classification = evaluation.ok
      ? MeterHealthClassification::Fresh
      : (has_snapshot ? MeterHealthClassification::Stale : MeterHealthClassification::NoData);
  evaluation.fault_started = !evaluation.ok && !state.fault_active;
  evaluation.recovered = evaluation.ok && state.fault_active;
  state.fault_active = !evaluation.ok;
  return evaluation;
}

const char *meter_health_classification_text(MeterHealthClassification classification) {
  switch (classification) {
    case MeterHealthClassification::Fresh: return "fresh";
    case MeterHealthClassification::NoData: return "no_data";
    case MeterHealthClassification::Stale: return "stale";
  }
  return "unknown";
}
