package connectsrv_test

import (
	"net/http"
	"net/http/httptest"
	"testing"

	"scrabble/gateway/internal/connectsrv"
)

// TestHTTPHandlerMountsAdminConsole verifies the admin proxy is reachable at /_gm/
// through the h2c-wrapped edge mux when configured.
func TestHTTPHandlerMountsAdminConsole(t *testing.T) {
	stub := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		w.WriteHeader(http.StatusTeapot)
	})
	srv := connectsrv.NewServer(connectsrv.Deps{AdminProxy: stub})
	front := httptest.NewServer(srv.HTTPHandler())
	defer front.Close()

	resp, err := http.Get(front.URL + "/_gm/")
	if err != nil {
		t.Fatal(err)
	}
	defer func() { _ = resp.Body.Close() }()
	if resp.StatusCode != http.StatusTeapot {
		t.Fatalf("/_gm status = %d, want 418 (routed to the admin proxy)", resp.StatusCode)
	}
}

// TestHTTPHandlerNoAdminConsole verifies /_gm is not served when no proxy is set.
func TestHTTPHandlerNoAdminConsole(t *testing.T) {
	srv := connectsrv.NewServer(connectsrv.Deps{})
	front := httptest.NewServer(srv.HTTPHandler())
	defer front.Close()

	resp, err := http.Get(front.URL + "/_gm/")
	if err != nil {
		t.Fatal(err)
	}
	defer func() { _ = resp.Body.Close() }()
	if resp.StatusCode != http.StatusNotFound {
		t.Fatalf("/_gm status = %d, want 404 (no admin proxy)", resp.StatusCode)
	}
}
