package metrics

import (
	"time"

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

var (
	requestCounter = promauto.NewCounterVec(prometheus.CounterOpts{
		Namespace: "app",
		Name:      "requests_total",
		Help:      "Total amount of request by handler",
	}, []string{"method", "path", "status_code"})

	requestDurationHistogram = promauto.NewHistogramVec(prometheus.HistogramOpts{
		Namespace: "app",
		Name:      "request_duration_seconds",
		Help:      "Latency of handler processing, seconds",
		Buckets:   prometheus.DefBuckets,
	}, []string{"method", "path", "status_code"})

	outboundCounter = promauto.NewCounterVec(prometheus.CounterOpts{
		Namespace: "app",
		Name:      "outbound_requests_total",
		Help:      "Total HTTP requests to external services",
	}, []string{"method", "url", "status_code"})

	outboundDurationHistogram = promauto.NewHistogramVec(prometheus.HistogramOpts{
		Namespace: "app",
		Name:      "outbound_request_duration_seconds",
		Help:      "Latency of outbound HTTP requests, seconds",
		Buckets:   prometheus.DefBuckets,
	}, []string{"method", "url", "status_code"})
)

func IncRequestHandlerCount(method, path, statusCode string) {
	requestCounter.WithLabelValues(method, path, statusCode).Inc()
}

func StoreHandlerRequestDuration(method, path, statusCode string, d time.Duration) {
	requestDurationHistogram.WithLabelValues(method, path, statusCode).Observe(d.Seconds())
}

func IncOutboundRequestCount(method, url, status string) {
	outboundCounter.WithLabelValues(method, url, status).Inc()
}

func StoreOutboundRequestDuration(method, url, status string, d time.Duration) {
	outboundDurationHistogram.WithLabelValues(method, url, status).Observe(d.Seconds())
}
