package service

import (
	"context"
	"testing"
	"time"

	"github.com/alicebob/miniredis/v2"
	"github.com/redis/go-redis/v9"
)

func TestStatsService_KeyRealtimeStats(t *testing.T) {
	t.Parallel()

	mr := miniredis.RunT(t)
	rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
	svc := NewStatsService(rdb)

	mr.Set("key:stats:hash:requests", "3")
	mr.Set("key:stats:hash:tokens", "42")
	mr.Set("key:stats:hash:last_access", "1700000000")

	stats, err := svc.GetKeyRealtimeStats(context.Background(), "hash")
	if err != nil {
		t.Fatalf("GetKeyRealtimeStats: %v", err)
	}
	if stats.Requests != 3 || stats.Tokens != 42 {
		t.Fatalf("unexpected stats: %+v", stats)
	}
	if stats.LastAccessedAt == nil || !stats.LastAccessedAt.Equal(time.Unix(1700000000, 0).UTC()) {
		t.Fatalf("unexpected last access: %+v", stats.LastAccessedAt)
	}
}

func TestStatsService_MasterRealtimeStats(t *testing.T) {
	t.Parallel()

	mr := miniredis.RunT(t)
	rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
	svc := NewStatsService(rdb)

	mr.Set("master:stats:99:requests", "7")
	mr.Set("master:stats:99:tokens", "100")

	stats, err := svc.GetMasterRealtimeStats(context.Background(), 99)
	if err != nil {
		t.Fatalf("GetMasterRealtimeStats: %v", err)
	}
	if stats.Requests != 7 || stats.Tokens != 100 {
		t.Fatalf("unexpected stats: %+v", stats)
	}
}

func TestStatsService_MasterRealtimeSnapshot(t *testing.T) {
	t.Parallel()

	mr := miniredis.RunT(t)
	rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
	svc := NewStatsService(rdb)

	mr.Set("master:stats:12:requests", "9")
	mr.Set("master:stats:12:tokens", "18")
	mr.HSet("master:rate:12", "qps", "3")
	mr.HSet("master:rate:12", "limit", "10")
	mr.HSet("master:rate:12", "blocked", "1")
	mr.HSet("master:rate:12", "updated_at", "1700000100")

	stats, err := svc.GetMasterRealtimeSnapshot(context.Background(), 12)
	if err != nil {
		t.Fatalf("GetMasterRealtimeSnapshot: %v", err)
	}
	if stats.Requests != 9 || stats.Tokens != 18 {
		t.Fatalf("unexpected totals: %+v", stats)
	}
	if stats.QPS != 3 || stats.QPSLimit != 10 || !stats.RateLimited {
		t.Fatalf("unexpected rate stats: %+v", stats)
	}
	if stats.UpdatedAt == nil || !stats.UpdatedAt.Equal(time.Unix(1700000100, 0).UTC()) {
		t.Fatalf("unexpected updated_at: %+v", stats.UpdatedAt)
	}
}
