package httpapi

import (
	"strings"
	"testing"
	"time"

	"github.com/easyai/easyai-ai-gateway/apps/api/internal/store"
)

func TestRateLimitErrorDetailIncludesUserGroupAndExceededMetric(t *testing.T) {
	resetAt := time.Date(2026, 5, 15, 10, 30, 0, 0, time.UTC)
	detail := rateLimitErrorDetail(&store.RateLimitExceededError{
		ScopeType:     "user_group",
		ScopeKey:      "group-1",
		ScopeName:     "VIP 用户组",
		ScopeMetadata: map[string]any{"groupKey": "vip"},
		Metric:        "rpm",
		Limit:         2,
		Amount:        1,
		Current:       2,
		Used:          1,
		Reserved:      1,
		Projected:     3,
		WindowSeconds: 60,
		ResetAt:       resetAt,
		RetryAfter:    5 * time.Second,
		Retryable:     true,
		Policy: map[string]any{
			"rules": []any{
				map[string]any{"metric": "rpm", "limit": float64(2), "windowSeconds": float64(60)},
			},
		},
	})
	if detail["metric"] != "rpm" || detail["projected"] != float64(3) || detail["limit"] != float64(2) {
		t.Fatalf("unexpected exceeded detail: %+v", detail)
	}
	userGroup, _ := detail["userGroup"].(map[string]any)
	if userGroup["id"] != "group-1" || userGroup["groupKey"] != "vip" || userGroup["name"] != "VIP 用户组" {
		t.Fatalf("missing user group detail: %+v", detail)
	}
	matchedRule, _ := detail["matchedRule"].(map[string]any)
	if matchedRule["metric"] != "rpm" {
		t.Fatalf("missing matched rule: %+v", detail)
	}
	if detail["retryAfterMs"] != int64(5000) || detail["resetAt"] != resetAt.Format(time.RFC3339Nano) {
		t.Fatalf("missing retry/reset detail: %+v", detail)
	}
}

func TestRunErrorMessageIncludesRateLimitSummary(t *testing.T) {
	message := runErrorMessage(&store.RateLimitExceededError{
		ScopeType:     "user_group",
		ScopeKey:      "group-1",
		ScopeName:     "VIP 用户组",
		ScopeMetadata: map[string]any{"groupKey": "vip"},
		Metric:        "rpm",
		Limit:         2,
		Amount:        1,
		Current:       2,
		Projected:     3,
		WindowSeconds: 60,
		RetryAfter:    5 * time.Second,
		Retryable:     true,
		Message:       "rate limit exceeded: rpm window has no remaining capacity",
	})
	for _, expected := range []string{"限流摘要", "用户组 VIP 用户组(vip)", "rpm 超限", "当前 2", "本次 1", "预计 3", "限制 2", "窗口 60 秒", "约5秒后可重试"} {
		if !strings.Contains(message, expected) {
			t.Fatalf("message %q should contain %q", message, expected)
		}
	}
}
