package service_test

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	caasmmodel "security-center-backend/internal/modules/caasm/model"
	"security-center-backend/internal/modules/caasm/service"
)

func TestResolveAccountThrottlingCapsLegacyOverridesToGlobal(t *testing.T) {
	base := service.ResolveGlobalThrottling(&caasmmodel.ThrottlingConfig{
		Enabled:           true,
		TokenCapacity:     12,
		TokenRefillPerSec: 6,
		MaxConcurrency:    4,
		MinConcurrency:    2,
	})

	account := &caasmmodel.CloudAccount{
		Provider: caasmmodel.CloudProviderAWS,
		Metadata: map[string]interface{}{
			"throttle_tokens":          float64(20),
			"throttle_refill_per_sec":  8.5,
			"throttle_max_concurrency": float64(6),
			"throttle_min_concurrency": float64(3),
		},
	}

	resolution := service.ResolveAccountThrottlingWithBase(base, account)

	require.True(t, resolution.Effective.Enabled)
	assert.Equal(t, 12, resolution.Effective.TokenCapacity)
	assert.Equal(t, 6.0, resolution.Effective.TokenRefillPerSec)
	assert.Equal(t, 4, resolution.Effective.MaxConcurrency)
	assert.Equal(t, 2, resolution.Effective.MinConcurrency)
	assert.Equal(t, "legacy_capped_to_global", resolution.Sources["token_capacity"])
	assert.Equal(t, "legacy_capped_to_global", resolution.Sources["token_refill_per_sec"])
	assert.Equal(t, "legacy_capped_to_global", resolution.Sources["max_concurrency"])
	assert.Equal(t, "legacy_capped_to_global", resolution.Sources["min_concurrency"])
}
