package processor_test

import (
	"reflect"
	"testing"

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

func TestKvSetFromRegistry(t *testing.T) {
	registration, ok := processor.GetProcessorRegistration("kv.set")
	if !ok {
		t.Fatalf("kv.set processor not registered")
	}

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

	if processorInstance.Id() != "test-id" {
		t.Fatalf("kv.set processor has wrong id: %s", processorInstance.Id())
	}
	if processorInstance.Type() != "kv.set" {
		t.Fatalf("kv.set processor has wrong type: %s", processorInstance.Type())
	}

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

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

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

func TestGoodKvSet(t *testing.T) {

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

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

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

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

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

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

func TestBadKvSet(t *testing.T) {
	testCases := []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.TestKVModule{},
			},
			errorString: "kv.set 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.TestKVModule{},
			},
			errorString: "kv.set 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.TestKVModule{},
			},
			errorString: "kv.set 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.TestKVModule{},
			},
			errorString: "kv.set 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.set 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.set 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.set module with id test is not a KeyValueModule",
		},
	}

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

			registration, ok := processor.GetProcessorRegistration("kv.set")
			if !ok {
				t.Fatalf("kv.set processor not registered")
			}

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

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

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

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

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

func BenchmarkKvSet(b *testing.B) {
	registration, ok := processor.GetProcessorRegistration("kv.set")
	if !ok {
		b.Fatalf("kv.set processor not registered")
	}

	processorInstance, err := registration.New(config.ProcessorConfig{
		Type: "kv.set",
		Params: map[string]any{
			"module": "test",
			"key":    "test",
		},
	})

	if err != nil {
		b.Fatalf("kv.set failed to create processor: %s", err)
	}
	modules := map[string]common.Module{
		"test": test.NewTestKVModule("test", nil),
	}

	count := 0
	for b.Loop() {
		_, err := processorInstance.Process(b.Context(), common.WrappedPayload{Payload: count, Modules: modules})
		if err != nil {
			b.Fatalf("kv.set processing failed: %s", err)
		}
		count++
	}
}
