package processor_test

import (
	"testing"

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

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

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

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

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

func TestGoodIntParse(t *testing.T) {
	tests := []struct {
		name     string
		params   map[string]any
		payload  any
		expected int64
	}{
		{
			name: "positive number",
			params: map[string]any{
				"base":    10,
				"bitSize": 64,
			},
			payload:  "12345",
			expected: 12345,
		},
		{
			name: "negative number",
			params: map[string]any{
				"base":    10,
				"bitSize": 64,
			},
			payload:  "-12345",
			expected: -12345,
		},
		{
			name: "zero",
			params: map[string]any{
				"base":    10,
				"bitSize": 64,
			},
			payload:  "0",
			expected: 0,
		},
		{
			name: "binary",
			params: map[string]any{
				"base":    2,
				"bitSize": 64,
			},
			payload:  "1010101",
			expected: 85,
		},
		{
			name: "hex",
			params: map[string]any{
				"base":    16,
				"bitSize": 64,
			},
			payload:  "15F",
			expected: 351,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			registration, ok := processor.ProcessorRegistry["int.parse"]
			if !ok {
				t.Fatalf("int.parse processor not registered")
			}

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

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

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

			gotInt, ok := got.Payload.(int64)
			if !ok {
				t.Fatalf("int.parse returned a %T payload: %+v", got, got)
			}
			if gotInt != test.expected {
				t.Fatalf("int.parse got %d, expected %d", gotInt, test.expected)
			}
		})
	}
}

func TestBadIntParse(t *testing.T) {
	tests := []struct {
		name        string
		params      map[string]any
		payload     any
		errorString string
	}{
		{
			name: "non-string base",
			params: map[string]any{
				"base":    "10",
				"bitSize": 64,
			},
			payload:     "12345",
			errorString: "int.parse base error: not a number",
		},
		{
			name: "non-string bitSize",
			params: map[string]any{
				"base":    10,
				"bitSize": "64",
			},
			payload:     "12345",
			errorString: "int.parse bitSize error: not a number",
		},
		{
			name: "non-string input",
			params: map[string]any{
				"base":    10,
				"bitSize": 64,
			},
			payload:     []byte{0x01},
			errorString: "int.parse processor only accepts a string",
		},
		{
			name: "not int string",
			params: map[string]any{
				"base":    10,
				"bitSize": 64,
			},
			payload:     "123.46",
			errorString: "strconv.ParseInt: parsing \"123.46\": invalid syntax",
		},
		{
			name: "bit overflow",
			params: map[string]any{
				"base":    10,
				"bitSize": 32,
			},
			payload:     "12345678901234567890",
			errorString: "strconv.ParseInt: parsing \"12345678901234567890\": value out of range",
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			registration, ok := processor.ProcessorRegistry["int.parse"]
			if !ok {
				t.Fatalf("int.parse processor not registered")
			}

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

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

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

			if err == nil {
				t.Fatalf("int.parse expected to fail but succeeded, got: %v", got)

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