package processor_test

import (
	"fmt"
	"slices"
	"testing"

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

func TestJsonEncodeFromRegistry(t *testing.T) {
	registration, ok := processor.GetProcessorRegistration("json.encode")
	if !ok {
		t.Fatalf("json.encode processor not registered")
	}

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

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

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

	payload := struct {
		Property string `json:"property"`
	}{
		Property: "hello",
	}

	expected := []byte("{\"property\":\"hello\"}")

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

	gotBytes, ok := got.Payload.([]byte)

	if !ok {
		t.Fatalf("json.encode should return byte slice got %T: %+v", got.Payload, got.Payload)
	}

	if !slices.Equal(gotBytes, expected) {
		t.Fatalf("json.encode got %+v, expected %+v", got, expected)
	}
}

func TestGoodJsonEncode(t *testing.T) {
	jsonEncoder := processor.JsonEncode{}
	tests := []struct {
		name     string
		payload  any
		expected []byte
	}{
		{
			name: "basic struct",
			payload: osc.OSCMessage{
				Address: "/hello",
			},
			expected: []byte("{\"address\":\"/hello\",\"args\":null}"),
		},
	}

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

			gotBytes, ok := got.Payload.([]byte)
			if !ok {
				t.Fatalf("json.encode returned a %T payload: %+v", got.Payload, got.Payload)
			}

			if !slices.Equal(gotBytes, test.expected) {
				t.Fatalf("json.encode got %+v, expected %s", got, test.expected)
			}
		})
	}
}

func TestBadJsonEncode(t *testing.T) {
	stringEncoder := processor.JsonEncode{}
	tests := []struct {
		name        string
		payload     any
		errorString string
	}{
		{
			name:        "unencodable type",
			payload:     make(chan int),
			errorString: "json: unsupported type: chan int",
		},
	}

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

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

func BenchmarkJsonEncode(b *testing.B) {
	registration, ok := processor.GetProcessorRegistration("json.encode")
	if !ok {
		b.Fatalf("json.encode processor not registered")
	}

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

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

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