package metrics

import (
	"strconv"
	"time"

	"git.frostfs.info/TrueCloudLab/frostfs-observability/metrics"
	"github.com/prometheus/client_golang/prometheus"
)

const (
	innerRingSubsystem    = "ir"
	innerRingLabelSuccess = "success"
	innerRingLabelType    = "type"
)

// InnerRingServiceMetrics contains metrics collected by inner ring.
type InnerRingServiceMetrics struct {
	epoch         prometheus.Gauge
	health        prometheus.Gauge
	eventDuration *prometheus.HistogramVec
}

// NewInnerRingMetrics returns new instance of metrics collectors for inner ring.
func NewInnerRingMetrics() *InnerRingServiceMetrics {
	var (
		epoch = metrics.NewGauge(prometheus.GaugeOpts{
			Namespace: namespace,
			Subsystem: innerRingSubsystem,
			Name:      "epoch",
			Help:      "Current epoch as seen by inner-ring node.",
		})
		health = metrics.NewGauge(prometheus.GaugeOpts{
			Namespace: namespace,
			Subsystem: innerRingSubsystem,
			Name:      "health",
			Help:      "Current inner-ring node state.",
		})
		eventDuration = metrics.NewHistogramVec(prometheus.HistogramOpts{
			Namespace: namespace,
			Subsystem: innerRingSubsystem,
			Name:      "event_duration_seconds",
			Help:      "Duration of processing of inner-ring events",
		}, []string{innerRingLabelType, innerRingLabelSuccess})
	)

	return &InnerRingServiceMetrics{
		epoch:         epoch,
		health:        health,
		eventDuration: eventDuration,
	}
}

// SetEpoch updates epoch metrics.
func (m InnerRingServiceMetrics) SetEpoch(epoch uint64) {
	m.epoch.Set(float64(epoch))
}

// SetHealth updates health metrics.
func (m InnerRingServiceMetrics) SetHealth(s int32) {
	m.health.Set(float64(s))
}

func (m InnerRingServiceMetrics) AddEvent(d time.Duration, typ string, success bool) {
	m.eventDuration.With(prometheus.Labels{
		innerRingLabelType:    typ,
		innerRingLabelSuccess: strconv.FormatBool(success),
	}).Observe(d.Seconds())
}