package v1

import (
	"strings"
	"testing"
)

var testConfig = `delta_index_cache_size = 3221225472
mark_cache_size = 1073741824
minmax_index_cache_size = 1073741824
graceful-wait-before-shutdown = 30

[flash]
disaggregated_mode = "tiflash_compute"

[security]
cert_allowed_cn = ["7a22fc43"]
redact_info_log = true

[storage]

[storage.main]
dir = ["/data0/db"]

[storage.remote]

[storage.remote.cache]
capacity = 0
dir = "/data0/cache"`

func TestSetConfig(t *testing.T) {
	conf := NewTomlConfig(map[string]any{})
	if err := conf.UnmarshalTOML([]byte(testConfig)); err != nil {
		t.Fatal(err)
	}

	size := uint32(100)
	conf.Set("storage.remote.cache.capacity", (int64(size) * 1024 * 1024 * 1024))

	configFile, err := conf.MarshalTOML()
	if err != nil {
		t.Fatal(err)
	}

	if !strings.Contains(string(configFile), "capacity = 107374182400") {
		t.Error("Failed to set config")
	}
}

func TestSetNewNestedKey(t *testing.T) {
	conf := NewTomlConfig(map[string]any{})
	conf.Set("new.section.key", "value")

	configFile, err := conf.MarshalTOML()
	if err != nil {
		t.Fatal(err)
	}

	if !strings.Contains(string(configFile), "[new.section]\nkey = \"value\"") {
		t.Error("Failed to set new nested key")
	}
}

func TestSetExistingKey(t *testing.T) {
	conf := NewTomlConfig(map[string]any{})
	if err := conf.UnmarshalTOML([]byte(testConfig)); err != nil {
		t.Fatal(err)
	}

	conf.Set("flash.disaggregated_mode", "new_mode")

	configFile, err := conf.MarshalTOML()
	if err != nil {
		t.Fatal(err)
	}

	if !strings.Contains(string(configFile), "disaggregated_mode = \"new_mode\"") {
		t.Error("Failed to update existing key")
	}
}

func TestSetTopLevelKey(t *testing.T) {
	conf := NewTomlConfig(map[string]any{})
	conf.Set("new_key", "top_level_value")

	configFile, err := conf.MarshalTOML()
	if err != nil {
		t.Fatal(err)
	}

	if !strings.Contains(string(configFile), "new_key = \"top_level_value\"") {
		t.Error("Failed to set top level key")
	}
}

func TestGetConfig(t *testing.T) {
	conf := NewTomlConfig(map[string]any{})
	if err := conf.UnmarshalTOML([]byte(testConfig)); err != nil {
		t.Fatal(err)
	}

	value := conf.Get("flash.disaggregated_mode")
	if value != "tiflash_compute" {
		t.Errorf("Expected 'tiflash_compute', got '%v'", value)
	}

	value = conf.Get("graceful-wait-before-shutdown")
	t.Log(value)
	if value != int64(30) {
		t.Errorf("Expected 30, got '%v'", value)
	}

	nonExistent := conf.Get("non.existent.key")
	if nonExistent != nil {
		t.Errorf("Expected nil for non-existent key, got '%v'", nonExistent)
	}
}

func TestDeepCopy(t *testing.T) {
	conf := NewTomlConfig(map[string]any{})
	if err := conf.UnmarshalTOML([]byte(testConfig)); err != nil {
		t.Fatal(err)
	}

	cp := conf.DeepCopy()
	cp.Set("flash.disaggregated_mode", "copied_mode")

	originalFile, err := conf.MarshalTOML()
	if err != nil {
		t.Fatal(err)
	}

	copyFile, err := cp.MarshalTOML()
	if err != nil {
		t.Fatal(err)
	}

	if strings.Contains(string(originalFile), "copied_mode") {
		t.Error("Deep copy modified original config")
	}

	if !strings.Contains(string(copyFile), "copied_mode") {
		t.Error("Deep copy failed to modify copied config")
	}
}
