package conversion_test

import (
	"testing"

	"nex/backend/internal/conversion"
	"nex/backend/internal/conversion/anthropic"
	"nex/backend/internal/conversion/openai"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"go.uber.org/zap"
)

func TestConvertHttpRequest_SameProtocolUsesAdapterBuildURL(t *testing.T) {
	tests := []struct {
		name             string
		adapter          conversion.ProtocolAdapter
		clientProtocol   string
		providerProtocol string
		baseURL          string
		nativePath       string
		expectedURL      string
		body             []byte
	}{
		{
			name:             "openai base url includes version path",
			adapter:          openai.NewAdapter(),
			clientProtocol:   "openai",
			providerProtocol: "openai",
			baseURL:          "http://example.com/v1",
			nativePath:       "/chat/completions",
			expectedURL:      "http://example.com/v1/chat/completions",
			body:             []byte(`{"model":"gpt-4","messages":[]}`),
		},
		{
			name:             "anthropic native path keeps v1",
			adapter:          anthropic.NewAdapter(),
			clientProtocol:   "anthropic",
			providerProtocol: "anthropic",
			baseURL:          "http://example.com",
			nativePath:       "/v1/messages",
			expectedURL:      "http://example.com/v1/messages",
			body:             []byte(`{"model":"claude","messages":[]}`),
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			registry := conversion.NewMemoryRegistry()
			engine := conversion.NewConversionEngine(registry, zap.NewNop())
			require.NoError(t, registry.Register(tt.adapter))

			out, err := engine.ConvertHttpRequest(conversion.HTTPRequestSpec{
				URL:    tt.nativePath,
				Method: "POST",
				Body:   tt.body,
			}, tt.clientProtocol, tt.providerProtocol, conversion.NewTargetProvider(tt.baseURL, "key", "upstream-model"))
			require.NoError(t, err)
			assert.Equal(t, tt.expectedURL, out.URL)
		})
	}
}
