package monitoring_test

import (
	"testing"
	"time"

	"github.com/user-management-system/internal/monitoring"
)

// TestNewMetrics 测试监控指标初始化
func TestNewMetrics(t *testing.T) {
	m := monitoring.NewMetrics()
	if m == nil {
		t.Fatal("NewMetrics() returned nil")
	}
}

// TestMetricsGetCollectors 测试获取 Prometheus 收集器列表不为空
func TestMetricsGetCollectors(t *testing.T) {
	m := monitoring.NewMetrics()
	collectors := m.GetMetrics()
	if len(collectors) == 0 {
		t.Error("GetMetrics() should return non-empty collector list")
	}
}

// TestIncHTTPRequest 测试HTTP请求计数不 panic
func TestIncHTTPRequest(t *testing.T) {
	m := monitoring.NewMetrics()
	m.IncHTTPRequest("GET", "/api/v1/users", 200)
	m.IncHTTPRequest("POST", "/api/v1/users", 201)
	m.IncHTTPRequest("GET", "/api/v1/users", 500)
}

// TestObserveHTTPRequestDuration 测试HTTP请求耗时记录不 panic
func TestObserveHTTPRequestDuration(t *testing.T) {
	m := monitoring.NewMetrics()
	m.ObserveHTTPRequestDuration("GET", "/api/v1/users", 50*time.Millisecond)
	m.ObserveHTTPRequestDuration("POST", "/api/v1/auth/login", 200*time.Millisecond)
}

// TestIncDBQuery 测试数据库查询计数不 panic
func TestIncDBQuery(t *testing.T) {
	m := monitoring.NewMetrics()
	m.IncDBQuery("SELECT", "users")
	m.IncDBQuery("INSERT", "users")
	m.IncDBQuery("UPDATE", "users")
	m.IncDBQuery("DELETE", "users")
}

// TestObserveDBQueryDuration 测试数据库查询耗时记录不 panic
func TestObserveDBQueryDuration(t *testing.T) {
	m := monitoring.NewMetrics()
	m.ObserveDBQueryDuration("SELECT", "users", 5*time.Millisecond)
	m.ObserveDBQueryDuration("INSERT", "users", 10*time.Millisecond)
}

// TestIncUserRegistration 测试用户注册计数不 panic
func TestIncUserRegistration(t *testing.T) {
	m := monitoring.NewMetrics()
	m.IncUserRegistration("normal")
	m.IncUserRegistration("oauth")
}

// TestIncUserLogin 测试用户登录计数不 panic
func TestIncUserLogin(t *testing.T) {
	m := monitoring.NewMetrics()
	m.IncUserLogin("password", "success")
	m.IncUserLogin("password", "fail")
	m.IncUserLogin("oauth", "success")
}

// TestSetActiveUsers 测试活跃用户数设置不 panic
func TestSetActiveUsers(t *testing.T) {
	m := monitoring.NewMetrics()
	m.SetActiveUsers("daily", 1000)
	m.SetActiveUsers("weekly", 5000)
}

// TestSetMemoryUsage 测试内存使用量设置不 panic
func TestSetMemoryUsage(t *testing.T) {
	m := monitoring.NewMetrics()
	m.SetMemoryUsage(1024 * 1024 * 100) // 100MB
}

// TestSetGoroutines 测试协程数设置不 panic
func TestSetGoroutines(t *testing.T) {
	m := monitoring.NewMetrics()
	m.SetGoroutines(50)
	m.SetGoroutines(100)
}
