package processor_test

import (
	"reflect"
	"testing"

	"github.com/jwetzell/showbridge-go/internal/common"
	"github.com/jwetzell/showbridge-go/internal/config"
	"github.com/jwetzell/showbridge-go/internal/processor"
	"github.com/jwetzell/showbridge-go/internal/test"
)

func TestKvGetFromRegistry(t *testing.T) {
	registration, ok := processor.ProcessorRegistry["kv.get"]
	if !ok {
		t.Fatalf("kv.get processor not registered")
	}

	processorInstance, err := registration.New(config.ProcessorConfig{
		Type: "kv.get",
		Params: map[string]any{
			"module": "test",
			"key":    "test",
		},
	})
	if err != nil {
		t.Fatalf("failed to create kv.get processor: %s", err)
	}

	if processorInstance.Type() != "kv.get" {
		t.Fatalf("kv.get processor has wrong type: %s", processorInstance.Type())
	}

	payload := "hello"
	expected := "test"

	got, err := processorInstance.Process(t.Context(), common.WrappedPayload{
		Modules: map[string]common.Module{
			"test": test.NewTestKVModule("test"),
		},
		Payload: payload,
	})
	if err != nil {
		t.Fatalf("kv.get processing failed: %s", err)
	}

	if got.Payload != expected {
		t.Fatalf("kv.get got %+v, expected %+v", got.Payload, expected)
	}
}

func TestGoodKvGet(t *testing.T) {

	testCases := []struct {
		name     string
		params   map[string]any
		payload  any
		expected any
	}{
		{
			name: "basic value",
			params: map[string]any{
				"module": "test",
				"key":    "test",
				"value":  "hello",
			},
			payload:  "hello",
			expected: "test",
		},
		{
			name: "template value",
			params: map[string]any{
				"module": "test",
				"key":    "test",
				"value":  "{{.Payload}}",
			},
			payload:  "hello",
			expected: "test",
		},
	}
	for _, testCase := range testCases {
		t.Run(testCase.name, func(t *testing.T) {
			registration, ok := processor.ProcessorRegistry["kv.get"]
			if !ok {
				t.Fatalf("kv.get processor not registered")
			}

			processorInstance, err := registration.New(config.ProcessorConfig{
				Type:   "kv.get",
				Params: testCase.params,
			})

			if err != nil {
				t.Fatalf("kv.get failed to create processor: %s", err)
			}

			got, err := processorInstance.Process(t.Context(), common.WrappedPayload{
				Modules: map[string]common.Module{
					"test": test.NewTestKVModule("test"),
				},
				Payload: testCase.payload,
			})

			if err != nil {
				t.Fatalf("kv.get processing failed: %s", err)
			}

			if !reflect.DeepEqual(got.Payload, testCase.expected) {
				t.Fatalf("kv.get got payload: %+v, expected %+v", got.Payload, testCase.expected)
			}
		})
	}
}

func TestBadKvGet(t *testing.T) {
	tests := []struct {
		name                  string
		params                map[string]any
		payload               any
		wrappedPayloadModules map[string]common.Module
		errorString           string
	}{
		{
			name:    "no module param",
			payload: test.TestStruct{Data: "hello"},
			params: map[string]any{
				"key": "test",
			},
			wrappedPayloadModules: map[string]common.Module{
				"test": test.NewTestKVModule("test"),
			},
			errorString: "kv.get module error: not found",
		},
		{
			name:    "non string module",
			payload: test.TestStruct{Data: "hello"},
			params: map[string]any{
				"module": 1,
				"key":    "test",
			},
			wrappedPayloadModules: map[string]common.Module{
				"test": test.NewTestKVModule("test"),
			},
			errorString: "kv.get module error: not a string",
		},
		{
			name:    "no key param",
			payload: test.TestStruct{Data: "hello"},
			params: map[string]any{
				"module": "test",
			},
			wrappedPayloadModules: map[string]common.Module{
				"test": test.NewTestKVModule("test"),
			},
			errorString: "kv.get key error: not found",
		},
		{
			name:    "non string key",
			payload: test.TestStruct{Data: "hello"},
			params: map[string]any{
				"module": "test",
				"key":    1,
			},
			wrappedPayloadModules: map[string]common.Module{
				"test": test.NewTestKVModule("test"),
			},
			errorString: "kv.get key error: not a string",
		},
		{
			name:    "no modules in context",
			payload: test.TestStruct{Data: "hello"},
			params: map[string]any{
				"module": "test",
				"key":    "test",
			},
			wrappedPayloadModules: nil,
			errorString:           "kv.get wrapped payload has no modules",
		},
		{
			name:    "module not found in context",
			payload: test.TestStruct{Data: "hello"},
			params: map[string]any{
				"module": "test",
				"key":    "test",
			},
			wrappedPayloadModules: map[string]common.Module{},
			errorString:           "kv.get unable to find module with id: test",
		},
		{
			name:    "module not a kv module",
			payload: test.TestStruct{Data: "hello"},
			params: map[string]any{
				"module": "test",
				"key":    "test",
			},
			wrappedPayloadModules: map[string]common.Module{
				"test": test.NewTestDBModule("test"),
			},
			errorString: "kv.get module with id test is not a KeyValueModule",
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {

			registration, ok := processor.ProcessorRegistry["kv.get"]
			if !ok {
				t.Fatalf("kv.get processor not registered")
			}

			processorInstance, err := registration.New(config.ProcessorConfig{
				Type:   "kv.get",
				Params: test.params,
			})

			if err != nil {
				if test.errorString != err.Error() {
					t.Fatalf("kv.get got error '%s', expected '%s'", err.Error(), test.errorString)
				}
				return
			}

			got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Modules: test.wrappedPayloadModules, Payload: test.payload})

			if err == nil {
				t.Fatalf("kv.get expected to fail but got payload: %+v", got)
			}

			if err.Error() != test.errorString {
				t.Fatalf("kv.get got error '%s', expected '%s'", err.Error(), test.errorString)
			}
		})
	}
}
