package prommetrics

import (
	"testing"
	"time"
)

func TestRegistryNotNil(t *testing.T) {
	if Registry == nil {
		t.Fatal("Registry should not be nil")
	}
}

func TestSetDBConnections(t *testing.T) {
	// Should not panic
	SetDBConnections(10, 5)
	SetDBConnections(0, 0)
}

func TestSetRedisConnections(t *testing.T) {
	// Should not panic
	SetRedisConnections(20, 8)
	SetRedisConnections(0, 0)
}

func TestSetActiveAccounts(t *testing.T) {
	SetActiveAccounts(100)
	SetActiveAccounts(0)
}

func TestSetRequestQueueDepth(t *testing.T) {
	SetRequestQueueDepth(50)
	SetRequestQueueDepth(0)
}

func TestSetOpsHealthScore(t *testing.T) {
	SetOpsHealthScore(95)
	SetOpsHealthScore(0)
	SetOpsHealthScore(100)
}

func TestSetErrorRate(t *testing.T) {
	SetErrorRate(0.05)
	SetErrorRate(0)
	SetErrorRate(1.0)
}

func TestSetSuccessRate(t *testing.T) {
	SetSuccessRate(0.95)
	SetSuccessRate(0)
	SetSuccessRate(1.0)
}

func TestSetQPS(t *testing.T) {
	SetQPS(123.45)
	SetQPS(0)
}

func TestSetTPS(t *testing.T) {
	SetTPS(4567.89)
	SetTPS(0)
}

func TestRecordHTTPRequest(t *testing.T) {
	RecordHTTPRequest("GET", "/api/v1/chat", 200, 100*time.Millisecond)
	RecordHTTPRequest("POST", "/api/v1/sora/generate", 201, 200*time.Millisecond)
	RecordHTTPRequest("GET", "/api/v1/models", 500, 50*time.Millisecond)
}

func TestMetricsRegistered(t *testing.T) {
	// Verify all metrics are registered by gathering them
	mfs, err := Registry.Gather()
	if err != nil {
		t.Fatalf("Failed to gather metrics: %v", err)
	}

	expectedMetrics := map[string]bool{
		"sub2api_http_requests_total":          false,
		"sub2api_http_request_duration_seconds": false,
		"sub2api_db_connections_active":       false,
		"sub2api_db_connections_idle":         false,
		"sub2api_redis_connections_total":     false,
		"sub2api_redis_connections_idle":      false,
		"sub2api_accounts_active_total":       false,
		"sub2api_request_queue_depth":         false,
		"sub2api_ops_health_score":            false,
		"sub2api_error_rate":                  false,
		"sub2api_success_rate":                false,
		"sub2api_qps":                         false,
		"sub2api_tps":                         false,
	}

	for _, mf := range mfs {
		if _, ok := expectedMetrics[mf.GetName()]; ok {
			expectedMetrics[mf.GetName()] = true
		}
	}

	for name, found := range expectedMetrics {
		if !found {
			t.Errorf("Expected metric %q not found in registry", name)
		}
	}
}
