package integration_test

import (
	"context"
	"encoding/json"
	"net/http"
	"testing"
	"time"

	"galaxy/integration/testenv"
)

// TestLobbyFlow_FreeUserCreateGameForbidden asserts the F8-04b backend
// tier gate: a freshly registered (free-tier) account is rejected with
// `403 forbidden` when it tries to create a private game through the
// user-facing surface. The matching paid sibling
// `TestLobbyFlow_PrivateGameInviteRedeem` covers the success path with
// `testenv.PromoteToPaid`.
func TestLobbyFlow_FreeUserCreateGameForbidden(t *testing.T) {
	plat := testenv.Bootstrap(t, testenv.BootstrapOptions{})
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
	defer cancel()

	owner := testenv.RegisterSession(t, plat, "owner+free@example.com")
	ownerID, err := owner.LookupUserID(ctx, plat)
	if err != nil {
		t.Fatalf("resolve owner: %v", err)
	}
	ownerHTTP := testenv.NewBackendUserClient(plat.Backend.HTTPURL, ownerID)

	gameBody := map[string]any{
		"game_name":             "Free Tier Game",
		"visibility":            "private",
		"min_players":           2,
		"max_players":           4,
		"start_gap_hours":       1,
		"start_gap_players":     2,
		"enrollment_ends_at":    time.Now().Add(24 * time.Hour).UTC().Format(time.RFC3339),
		"turn_schedule":         "0 * * * *",
		"target_engine_version": "v1.0.0",
	}
	raw, resp, err := ownerHTTP.Do(ctx, http.MethodPost, "/api/v1/user/lobby/games", gameBody)
	if err != nil {
		t.Fatalf("create private game: %v", err)
	}
	if resp.StatusCode != http.StatusForbidden {
		t.Fatalf("expected 403 forbidden, got status=%d body=%s", resp.StatusCode, string(raw))
	}
	var envelope struct {
		Error struct {
			Code    string `json:"code"`
			Message string `json:"message"`
		} `json:"error"`
	}
	if err := json.Unmarshal(raw, &envelope); err != nil {
		t.Fatalf("decode error envelope: %v body=%s", err, string(raw))
	}
	if envelope.Error.Code != "forbidden" {
		t.Fatalf("expected code=forbidden, got %q body=%s", envelope.Error.Code, string(raw))
	}
}
