// Package prommetrics provides Prometheus metrics registration and update functions.
// This package is intentionally separate from routes to avoid import cycles.
package prommetrics

import (
	"strconv"
	"time"

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

var (
	// Prometheus registry
	Registry = prometheus.NewRegistry()

	// Custom metrics
	httpRequestsTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "sub2api_http_requests_total",
			Help: "Total number of HTTP requests",
		},
		[]string{"method", "path", "status"},
	)

	httpRequestDuration = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Name:    "sub2api_http_request_duration_seconds",
			Help:    "HTTP request duration in seconds",
			Buckets: prometheus.DefBuckets,
		},
		[]string{"method", "path"},
	)

	dbConnectionsActive = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Name: "sub2api_db_connections_active",
			Help: "Number of active database connections",
		},
	)

	dbConnectionsIdle = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Name: "sub2api_db_connections_idle",
			Help: "Number of idle database connections",
		},
	)

	redisConnectionsTotal = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Name: "sub2api_redis_connections_total",
			Help: "Total number of Redis connections",
		},
	)

	redisConnectionsIdle = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Name: "sub2api_redis_connections_idle",
			Help: "Number of idle Redis connections",
		},
	)

	accountActiveTotal = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Name: "sub2api_accounts_active_total",
			Help: "Total number of active accounts",
		},
	)

	requestQueueDepth = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Name: "sub2api_request_queue_depth",
			Help: "Current request queue depth",
		},
	)

	opsHealthScore = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Name: "sub2api_ops_health_score",
			Help: "Overall system health score (0-100)",
		},
	)

	errorRate = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Name: "sub2api_error_rate",
			Help: "Current error rate",
		},
	)

	successRate = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Name: "sub2api_success_rate",
			Help: "Current success rate",
		},
	)

	qps = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Name: "sub2api_qps",
			Help: "Queries per second",
		},
	)

	tps = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Name: "sub2api_tps",
			Help: "Tokens per second",
		},
	)
)

func init() {
	// Register custom metrics
	Registry.MustRegister(
		httpRequestsTotal,
		httpRequestDuration,
		dbConnectionsActive,
		dbConnectionsIdle,
		redisConnectionsTotal,
		redisConnectionsIdle,
		accountActiveTotal,
		requestQueueDepth,
		opsHealthScore,
		errorRate,
		successRate,
		qps,
		tps,
	)
}

// RecordHTTPRequest records an HTTP request for Prometheus metrics
func RecordHTTPRequest(method, path string, statusCode int, duration time.Duration) {
	httpRequestsTotal.WithLabelValues(method, path, strconv.Itoa(statusCode)).Inc()
	httpRequestDuration.WithLabelValues(method, path).Observe(duration.Seconds())
}

// SetDBConnections sets database connection metrics
func SetDBConnections(active, idle int) {
	dbConnectionsActive.Set(float64(active))
	dbConnectionsIdle.Set(float64(idle))
}

// SetRedisConnections sets Redis connection metrics
func SetRedisConnections(total, idle int) {
	redisConnectionsTotal.Set(float64(total))
	redisConnectionsIdle.Set(float64(idle))
}

// SetActiveAccounts sets the active accounts count
func SetActiveAccounts(count int) {
	accountActiveTotal.Set(float64(count))
}

// SetRequestQueueDepth sets the request queue depth
func SetRequestQueueDepth(depth int) {
	requestQueueDepth.Set(float64(depth))
}

// SetOpsHealthScore sets the overall health score
func SetOpsHealthScore(score int) {
	opsHealthScore.Set(float64(score))
}

// SetErrorRate sets the error rate
func SetErrorRate(rate float64) {
	errorRate.Set(rate)
}

// SetSuccessRate sets the success rate
func SetSuccessRate(rate float64) {
	successRate.Set(rate)
}

// SetQPS sets queries per second
func SetQPS(value float64) {
	qps.Set(value)
}

// SetTPS sets tokens per second
func SetTPS(value float64) {
	tps.Set(value)
}
