package node_test

import (
	"context"
	"net/http"
	"net/http/httptest"
	"net/url"
	"strings"
	"testing"

	"iop/packages/go/config"
	runtime "iop/packages/go/execution"
	iop "iop/proto/gen/iop"
)

// profileTunnelAdapter is a test adapter that resolves operation URLs from a
// concrete profile and makes real HTTP requests to a test server.
type profileTunnelAdapter struct {
	endpoint string
	profile  *config.ConcreteProtocolProfile
}

func (a *profileTunnelAdapter) Name() string { return "openai_compat" }
func (a *profileTunnelAdapter) Capabilities(_ context.Context) (runtime.Capabilities, error) {
	return runtime.Capabilities{AdapterName: "openai_compat", Targets: []string{"gpt-4"}}, nil
}
func (a *profileTunnelAdapter) Execute(ctx context.Context, spec runtime.ExecutionSpec, sink runtime.EventSink) error {
	return nil
}
func (a *profileTunnelAdapter) TunnelProvider(ctx context.Context, req runtime.ProviderTunnelRequest, sink runtime.ProviderTunnelSink) error {
	var urlStr string
	var err error
	if a.profile != nil {
		urlStr, err = a.profile.ResolveOperationURL(req.Operation)
	} else {
		urlStr = resolveLegacyURLForTest(a.endpoint, req.Path)
	}
	if err != nil {
		return err
	}
	httpReq, err := http.NewRequestWithContext(ctx, req.Method, urlStr, nil)
	if err != nil {
		return err
	}
	resp, err := http.DefaultClient.Do(httpReq)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	_ = sink.EmitTunnelFrame(ctx, runtime.ProviderTunnelFrame{
		RunID:      req.RunID,
		TunnelID:   req.TunnelID,
		Kind:       runtime.ProviderTunnelFrameKindResponseStart,
		StatusCode: resp.StatusCode,
	})
	_ = sink.EmitTunnelFrame(ctx, runtime.ProviderTunnelFrame{
		RunID:    req.RunID,
		TunnelID: req.TunnelID,
		Kind:     runtime.ProviderTunnelFrameKindEnd,
		End:      true,
	})
	return nil
}

// resolveLegacyURLForTest mirrors the legacy URL resolution used by the
// openai_compat adapter when no profile is present.
func resolveLegacyURLForTest(endpoint, path string) string {
	u, err := url.Parse(endpoint)
	if err != nil {
		return strings.TrimRight(endpoint, "/") + path
	}
	basePath := strings.TrimRight(u.Path, "/")
	if strings.HasSuffix(basePath, "/v1") && strings.HasPrefix(path, "/v1/") {
		path = strings.TrimPrefix(path, "/v1")
	}
	u.Path = basePath + path
	return u.String()
}

func TestProtocolProfileTunnelOperationSurvivesHandler(t *testing.T) {
	var gotPath string
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		gotPath = r.URL.Path
		w.Header().Set("Content-Type", "text/event-stream")
	}))
	defer server.Close()

	profile, err := config.ResolveProtocolProfile("openai", "", config.BuiltInProtocolProfiles)
	if err != nil {
		t.Fatalf("ResolveProtocolProfile: %v", err)
	}
	// Override the profile's base_url to point at the test server so the
	// profile-resolved URL reaches the test server.
	profile.BaseURL = server.URL + "/v1"

	adapter := &profileTunnelAdapter{endpoint: server.URL, profile: &profile}
	router := &fixedRouter{adapterName: "openai_compat", adapters: map[string]runtime.Provider{"openai_compat": adapter}}
	n, _ := makeNode(t, router)

	req := &iop.ProviderTunnelRequest{
		RunId:     "run-op-survives",
		TunnelId:  "tunnel-op-survives",
		Adapter:   "openai_compat",
		Target:    "gpt-4",
		Method:    http.MethodPost,
		Operation: string(config.OperationChatCompletions),
	}

	if err := n.OnProviderTunnelRequest(context.Background(), nil, req); err != nil {
		t.Fatalf("OnProviderTunnelRequest failed: %v", err)
	}

	if gotPath != "/v1/chat/completions" {
		t.Errorf("expected /v1/chat/completions, got %q", gotPath)
	}
}

func TestProtocolProfileTunnelLegacyPathFallback(t *testing.T) {
	var gotPath string
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		gotPath = r.URL.Path
		w.Header().Set("Content-Type", "text/event-stream")
	}))
	defer server.Close()

	adapter := &profileTunnelAdapter{endpoint: server.URL, profile: nil}
	router := &fixedRouter{adapterName: "openai_compat", adapters: map[string]runtime.Provider{"openai_compat": adapter}}
	n, _ := makeNode(t, router)

	req := &iop.ProviderTunnelRequest{
		RunId:    "run-legacy-fallback",
		TunnelId: "tunnel-legacy-fallback",
		Adapter:  "openai_compat",
		Target:   "gpt-4",
		Method:   http.MethodPost,
		Path:     "/v1/chat/completions",
	}

	if err := n.OnProviderTunnelRequest(context.Background(), nil, req); err != nil {
		t.Fatalf("OnProviderTunnelRequest failed: %v", err)
	}

	if gotPath != "/v1/chat/completions" {
		t.Errorf("expected /v1/chat/completions, got %q", gotPath)
	}
}
