package httpapi

import (
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"github.com/easyai/easyai-ai-gateway/apps/api/internal/config"
	"github.com/easyai/easyai-ai-gateway/apps/api/internal/identity"
)

func TestRegisterIsStructurallyForbiddenWhenIdentityRevisionIsActive(t *testing.T) {
	server := &Server{
		cfg: config.Config{IdentityMode: "hybrid"},
		identityTestRevision: identity.Revision{
			State: identity.RevisionActive, Issuer: "https://auth.example.test",
			LegacyJWTEnabled: true,
		},
	}
	request := httptest.NewRequest(http.MethodPost, "/api/v1/auth/register", strings.NewReader(`{}`))
	recorder := httptest.NewRecorder()

	server.register(recorder, request)

	if recorder.Code != http.StatusForbidden {
		t.Fatalf("status=%d, want 403", recorder.Code)
	}
	var envelope struct {
		Error struct {
			Code string `json:"code"`
		} `json:"error"`
	}
	if err := json.Unmarshal(recorder.Body.Bytes(), &envelope); err != nil {
		t.Fatal(err)
	}
	if envelope.Error.Code != "LOCAL_REGISTRATION_DISABLED" {
		t.Fatalf("code=%q", envelope.Error.Code)
	}
}
