package frontend

import (
	"testing"

	"gopkg.in/ini.v1"

	"github.com/grafana/grafana/pkg/api/dtos"
	"github.com/grafana/grafana/pkg/infra/log"
	"github.com/stretchr/testify/assert"
)

func TestFSRequestConfig_ApplyOverrides(t *testing.T) {
	t.Run("should handle empty ini file", func(t *testing.T) {
		config := FSRequestConfig{
			AppURL:     "https://base.example.com",
			CSPEnabled: true,
		}

		iniFile := ini.Empty()

		config.ApplyOverrides(iniFile, log.New("test"))

		assert.Equal(t, "https://base.example.com", config.AppURL)
		assert.Equal(t, true, config.CSPEnabled)
	})

	t.Run("should preserve non-overridden fields", func(t *testing.T) {
		config := FSRequestConfig{
			FSFrontendSettings: FSFrontendSettings{
				AnonymousEnabled: true,
				DisableLoginForm: true,
				LoginHint:        "test@example.com",
				BuildInfo: dtos.FrontendSettingsBuildInfoDTO{
					Version: "10.3.0",
				},
			},
			AppURL:     "https://base.example.com",
			CSPEnabled: false,
		}

		iniFile := ini.Empty()
		securitySection, _ := iniFile.NewSection("security")
		_, _ = securitySection.NewKey("allow_embedding_hosts", "foo.example.com")

		config.ApplyOverrides(iniFile, log.New("test"))

		// CSP overridden field
		assert.Equal(t, []string{"foo.example.com"}, config.AllowEmbeddingHosts)

		// Non-overridden fields should be preserved
		assert.Equal(t, "https://base.example.com", config.AppURL)
		assert.True(t, config.AnonymousEnabled)
		assert.True(t, config.DisableLoginForm)
		assert.Equal(t, "test@example.com", config.LoginHint)
		assert.Equal(t, "10.3.0", config.BuildInfo.Version)
	})

	t.Run("should override FSFrontendSettings fields from settings service", func(t *testing.T) {
		config := FSRequestConfig{
			FSFrontendSettings: FSFrontendSettings{
				RudderstackWriteKey:     "base-write-key",
				RudderstackDataPlaneUrl: "https://base-dataplane.example.com",
			},
		}

		iniFile := ini.Empty()
		analyticsSection, _ := iniFile.NewSection("analytics")
		_, _ = analyticsSection.NewKey("rudderstack_write_key", "tenant-write-key")
		_, _ = analyticsSection.NewKey("rudderstack_data_plane_url", "https://tenant-dataplane.example.com")

		config.ApplyOverrides(iniFile, log.New("test"))

		assert.Equal(t, "tenant-write-key", config.RudderstackWriteKey)
		assert.Equal(t, "https://tenant-dataplane.example.com", config.RudderstackDataPlaneUrl)
	})

	t.Run("should override allow_embedding_hosts from settings service", func(t *testing.T) {
		config := FSRequestConfig{
			AllowEmbeddingHosts: nil,
		}

		iniFile := ini.Empty()
		securitySection, _ := iniFile.NewSection("security")
		_, _ = securitySection.NewKey("allow_embedding_hosts", "wiki.example.com foo.example.com")

		config.ApplyOverrides(iniFile, log.New("test"))

		assert.Equal(t, []string{"wiki.example.com", "foo.example.com"}, config.AllowEmbeddingHosts)
	})

	t.Run("should override allow_embedding_hosts with wildcard from settings service", func(t *testing.T) {
		config := FSRequestConfig{
			AllowEmbeddingHosts: nil,
		}

		iniFile := ini.Empty()
		securitySection, _ := iniFile.NewSection("security")
		_, _ = securitySection.NewKey("allow_embedding_hosts", "*")

		config.ApplyOverrides(iniFile, log.New("test"))

		assert.Equal(t, []string{"*"}, config.AllowEmbeddingHosts)
	})

	t.Run("should override form_action_additional_hosts from settings service", func(t *testing.T) {
		config := FSRequestConfig{
			FormActionAdditionalHosts: nil,
		}

		iniFile := ini.Empty()
		securitySection, _ := iniFile.NewSection("security")
		_, _ = securitySection.NewKey("form_action_additional_hosts", "login.example.com auth.example.com")

		config.ApplyOverrides(iniFile, log.New("test"))

		assert.Equal(t, []string{"login.example.com", "auth.example.com"}, config.FormActionAdditionalHosts)
	})

	t.Run("should override form_action_additional_hosts with wildcard from settings service", func(t *testing.T) {
		config := FSRequestConfig{
			FormActionAdditionalHosts: nil,
		}

		iniFile := ini.Empty()
		securitySection, _ := iniFile.NewSection("security")
		_, _ = securitySection.NewKey("form_action_additional_hosts", "*")

		config.ApplyOverrides(iniFile, log.New("test"))

		assert.Equal(t, []string{"*"}, config.FormActionAdditionalHosts)
	})

	t.Run("should preserve form_action_additional_hosts when not overridden", func(t *testing.T) {
		config := FSRequestConfig{
			FormActionAdditionalHosts: []string{"base.example.com"},
		}

		iniFile := ini.Empty()

		config.ApplyOverrides(iniFile, log.New("test"))

		assert.Equal(t, []string{"base.example.com"}, config.FormActionAdditionalHosts)
	})
}
