package metrics

import "github.com/prometheus/client_golang/prometheus"

const replicatorSubsystem = "replicator"

type replicatorMetrics struct {
	inFlightRequests           metric[prometheus.Gauge]
	processedObjects           metric[prometheus.Counter]
	totalReplicatedPayloadSize metric[prometheus.Counter]
}

func (m replicatorMetrics) IncInFlightRequest() {
	m.inFlightRequests.value.Inc()
}

func (m replicatorMetrics) DecInFlightRequest() {
	m.inFlightRequests.value.Dec()
}

func (m replicatorMetrics) IncProcessedObjects() {
	m.processedObjects.value.Inc()
}

func (m replicatorMetrics) AddPayloadSize(size int64) {
	m.totalReplicatedPayloadSize.value.Add(float64(size))
}

func newReplicatorMetrics() replicatorMetrics {
	return replicatorMetrics{
		inFlightRequests:           newReplicatorGauge("in_flight_requests", "Number of in-flight requests"),
		processedObjects:           newReplicatorCounter("processed_objects", "Number of objects processed since the node startup"),
		totalReplicatedPayloadSize: newReplicatorCounter("total_replicated_payload_size", "Total size of payloads replicated"),
	}
}

func (m replicatorMetrics) register() {
	mustRegister(m.inFlightRequests)
	mustRegister(m.processedObjects)
	mustRegister(m.totalReplicatedPayloadSize)
}

func newReplicatorCounter(name, help string) metric[prometheus.Counter] {
	return newCounter(prometheus.CounterOpts{
		Namespace: namespace,
		Subsystem: replicatorSubsystem,
		Name:      name,
		Help:      help,
	})
}

func newReplicatorGauge(name, help string) metric[prometheus.Gauge] {
	return newGauge(prometheus.GaugeOpts{
		Namespace: namespace,
		Subsystem: replicatorSubsystem,
		Name:      name,
		Help:      help,
	})
}