package reliable

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/tech/sendico/pkg/model"
)

func TestParseSettingsDefaultsWhenBlockMissing(t *testing.T) {
	got, err := ParseSettings(model.SettingsT{
		"url_env": "NATS_URL",
	})
	require.NoError(t, err)

	assert.Equal(t, DefaultSettings(), got)
}

func TestParseSettingsOverrides(t *testing.T) {
	got, err := ParseSettings(model.SettingsT{
		SettingsBlockKey: map[string]any{
			"enabled":               true,
			"batch_size":            250,
			"poll_interval_seconds": 3,
			"max_attempts":          7,
		},
	})
	require.NoError(t, err)

	assert.True(t, got.Enabled)
	assert.Equal(t, 250, got.BatchSize)
	assert.Equal(t, 3, got.PollIntervalSeconds)
	assert.Equal(t, 7, got.MaxAttempts)
}

func TestParseSettingsAppliesDefaultsForInvalidNumbers(t *testing.T) {
	got, err := ParseSettings(model.SettingsT{
		SettingsBlockKey: map[string]any{
			"enabled":               true,
			"batch_size":            0,
			"poll_interval_seconds": -1,
			"max_attempts":          0,
		},
	})
	require.NoError(t, err)

	assert.True(t, got.Enabled)
	assert.Equal(t, defaultBatchSize, got.BatchSize)
	assert.Equal(t, int(defaultPollInterval.Seconds()), got.PollIntervalSeconds)
	assert.Equal(t, defaultMaxAttempts, got.MaxAttempts)
}

func TestParseSettingsCanDisableReliablePublisher(t *testing.T) {
	got, err := ParseSettings(model.SettingsT{
		SettingsBlockKey: map[string]any{
			"enabled": false,
		},
	})
	require.NoError(t, err)
	assert.False(t, got.Enabled)
}
