package metrics

import (
	"time"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"
)

var (
	RBACPermissionCheckCallsCounter = promauto.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "dbaas",
			Subsystem: "account",
			Name:      "permission_check_counter",
		},
		[]string{"method"},
	)

	RBACPermissionCheckDuration = promauto.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: "dbaas",
			Subsystem: "account",
			Name:      "permission_check_duration",
		}, []string{"method", "stage"})

	AuthnSessionCallsCounter = promauto.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "dbaas",
			Subsystem: "account",
			Name:      "authn_session_counter",
		},
		[]string{"method"},
	)

	AuthnSessionDuration = promauto.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: "dbaas",
			Subsystem: "account",
			Name:      "authn_session_ms",
		}, []string{"method", "stage"})
)

func RecPermissionCheckMetrics(method, stage string, start time.Time, recCall bool) {
	if recCall {
		RBACPermissionCheckCallsCounter.
			WithLabelValues(method).
			Inc()
	}

	duration := time.Since(start).Milliseconds()
	RBACPermissionCheckDuration.
		WithLabelValues(method, stage).
		Observe(float64(duration))
}

func RecAuthnSessionMetrics(method, stage string, start time.Time, recCall bool) {
	if recCall {
		AuthnSessionCallsCounter.
			WithLabelValues(method).
			Inc()
	}

	duration := time.Since(start).Milliseconds()
	AuthnSessionDuration.
		WithLabelValues(method, stage).
		Observe(float64(duration))
}
