package processor_test

import (
	"fmt"
	"testing"

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

func TestStringDecodeFromRegistry(t *testing.T) {
	registration, ok := processor.GetProcessorRegistration("string.decode")
	if !ok {
		t.Fatalf("string.decode processor not registered")
	}

	processorInstance, err := registration.New(config.ProcessorConfig{
		Id:   "test-id",
		Type: "string.decode",
	})
	if err != nil {
		t.Fatalf("failed to create string.decode processor: %s", err)
	}

	if processorInstance.Id() != "test-id" {
		t.Fatalf("string.decode processor has wrong id: %s", processorInstance.Id())
	}

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

	payload := []byte{'h', 'e', 'l', 'l', 'o'}
	expected := "hello"

	got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: payload})
	if err != nil {
		t.Fatalf("string.decode processing failed: %s", err)
	}

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

func TestGoodStringDecode(t *testing.T) {
	stringDecoder := processor.StringDecode{}
	tests := []struct {
		name     string
		payload  any
		expected string
	}{
		{
			name:     "basic string",
			payload:  []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
			expected: "hello",
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			got, err := stringDecoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
			if err != nil {
				t.Fatalf("string.decode processing failed: %s", err)
			}

			gotString, ok := got.Payload.(string)
			if !ok {
				t.Fatalf("string.decode returned a %T payload: %+v", got, got)
			}

			if gotString != test.expected {
				t.Fatalf("string.decode got %+v, expected %s", got, test.expected)
			}
		})
	}
}

func TestBadStringDecode(t *testing.T) {
	stringDecoder := processor.StringDecode{}
	tests := []struct {
		name        string
		payload     any
		errorString string
	}{
		{
			name:        "non-[]byte input",
			payload:     "hello",
			errorString: "string.decode processor only accepts a []byte",
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			got, err := stringDecoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})

			if err == nil {
				t.Fatalf("string.decode expected to fail but got payload: %+v", got)
			}
			if err.Error() != test.errorString {
				t.Fatalf("string.decode got error '%s', expected '%s'", err.Error(), test.errorString)
			}
		})
	}
}

func BenchmarkStringDecode(b *testing.B) {
	registration, ok := processor.GetProcessorRegistration("string.decode")
	if !ok {
		b.Fatalf("string.decode processor not registered")
	}

	processorInstance, err := registration.New(config.ProcessorConfig{
		Type: "string.decode",
	})

	if err != nil {
		b.Fatalf("string.decode failed to create processor: %s", err)
	}

	count := 0
	for b.Loop() {
		_, err := processorInstance.Process(b.Context(), common.WrappedPayload{Payload: fmt.Appendf(nil, "%d", count)})
		if err != nil {
			b.Fatalf("string.decode processing failed: %s", err)
		}
		count++
	}
}
