// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT

package debug

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"go.opentelemetry.io/collector/config/configtelemetry"
	"go.opentelemetry.io/collector/confmap"
	"go.opentelemetry.io/collector/exporter/debugexporter"

	"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/common"
)

func TestTranslator(t *testing.T) {
	tt := NewTranslator(common.WithName("test"))
	assert.EqualValues(t, "debug/test", tt.ID().String())
	got, err := tt.Translate(confmap.New())
	assert.Error(t, err)
	assert.Nil(t, got)
}

func TestTranslate(t *testing.T) {
	tt := NewTranslator()
	testCases := map[string]struct {
		input   map[string]interface{}
		want    *debugexporter.Config
		wantErr error
	}{
		"WithMissingKey": {
			input: map[string]interface{}{"logs": map[string]interface{}{}},
			wantErr: &common.MissingKeyError{
				ID:      tt.ID(),
				JsonKey: common.AgentDebugConfigKey,
			},
		},
		"WithDebugLoggingEnabled": {
			input: map[string]interface{}{
				"agent": map[string]interface{}{
					"debug": true,
				},
			},
			want: &debugexporter.Config{
				Verbosity:          configtelemetry.LevelDetailed,
				SamplingInitial:    2,
				SamplingThereafter: 500,
				UseInternalLogger:  true,
			},
		},
	}
	for name, testCase := range testCases {
		t.Run(name, func(t *testing.T) {
			conf := confmap.NewFromStringMap(testCase.input)
			got, err := tt.Translate(conf)
			assert.Equal(t, testCase.wantErr, err)
			if err == nil {
				require.NotNil(t, got)
				gotCfg, ok := got.(*debugexporter.Config)
				require.True(t, ok)
				assert.Equal(t, testCase.want, gotCfg)
			}
		})
	}
}
