package connector_callout

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"
)

// TestNoopProfileDrivesEngine demonstrates AC8: a new connector profile
// (here constructed directly, as if a second L1 pack installed it) drives the
// same Engine code path without any engine-code change.
//
// The profile is "__test_noop__" — a zero-config placeholder that proves the
// Engine is vendor-neutral and does not require knowledge of the profile name.
func TestNoopProfileDrivesEngine(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(200)
		_, _ = fmt.Fprint(w, `{"status":"ok"}`)
	}))
	defer srv.Close()

	// Construct a ConnectorCalloutProfile as a second pack would install it.
	profile := &ConnectorCalloutProfile{
		OwnerPack:     "test-noop-pack",
		ProfileName:   "__test_noop__",
		CredentialRef: "noop-nc",
		EndpointClass: "noop.read", // declared on the profile, not hardcoded at the call site
		RetryPolicy: RetryPolicy{
			MaxAttempts: 1,
			BaseDelay:   10 * time.Millisecond,
			Multiplier:  1,
		},
	}

	// authType = "Custom": noop profile sends no server-known credentials,
	// so the HTTPS guard (B44) does not apply to this demonstration test.
	// Production callouts with BearerToken/OAuth2/BasicAuth auth types
	// require HTTPS; see TestEngineDoRequiresHTTPSForCredentialedAuth.
	creds := &mockCredentialResolver{endpoint: srv.URL, token: "noop-token", authType: "Custom"}
	engine := NewEngine(creds, &mockThrottler{}, loopbackHTTPClient(), testAllowlistForEngine())

	req := CalloutRequest{
		Method:        "GET",
		CredentialRef: profile.CredentialRef,
		EndpointClass: profile.EndpointClass, // drives throttle from the profile
	}
	resp, err := engine.Do(context.Background(), req, profile.RetryPolicy)
	if err != nil {
		t.Fatalf("noop profile engine call failed: %v", err)
	}
	if resp.StatusCode != 200 {
		t.Errorf("expected 200, got %d", resp.StatusCode)
	}
}
