package integration_test

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

	"galaxy/integration/testenv"
	lobbymodel "galaxy/model/lobby"
	"galaxy/transcoder"
)

// TestLobbyOpenEnrollment drives `lobby.game.open-enrollment` through
// gateway authenticated edge. Owner moves draft → enrollment_open; non-owner is
// rejected; idempotent re-call on enrollment_open is a no-op (still
// returns enrollment_open).
func TestLobbyOpenEnrollment(t *testing.T) {
	plat := testenv.Bootstrap(t, testenv.BootstrapOptions{})
	ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
	defer cancel()

	admin := testenv.NewBackendAdminClient(plat.Backend.HTTPURL, plat.Backend.AdminUser, plat.Backend.AdminPassword)
	if _, resp, err := admin.Do(ctx, http.MethodPost, "/api/v1/admin/engine-versions", map[string]any{
		"version": "v1.0.0", "image_ref": "galaxy/game:integration", "enabled": true,
	}); err != nil || resp.StatusCode/100 != 2 {
		t.Fatalf("seed engine_version: err=%v resp=%v", err, resp)
	}

	owner := testenv.RegisterSession(t, plat, "owner+enroll@example.com")
	other := testenv.RegisterSession(t, plat, "other+enroll@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":             "Open Enrollment Lobby",
		"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 || resp.StatusCode != http.StatusCreated {
		t.Fatalf("create private game: err=%v status=%d body=%s", err, resp.StatusCode, string(raw))
	}
	var game struct {
		GameID string `json:"game_id"`
	}
	if err := json.Unmarshal(raw, &game); err != nil {
		t.Fatalf("decode: %v", err)
	}

	encode := func(t *testing.T) []byte {
		t.Helper()
		payload, err := transcoder.OpenEnrollmentRequestToPayload(&lobbymodel.OpenEnrollmentRequest{
			GameID: game.GameID,
		})
		if err != nil {
			t.Fatalf("encode payload: %v", err)
		}
		return payload
	}

	// Non-owner attempt — must fail.
	otherGW, err := other.DialAuthenticated(ctx, plat)
	if err != nil {
		t.Fatalf("dial other: %v", err)
	}
	defer otherGW.Close()
	res, err := otherGW.Execute(ctx, lobbymodel.MessageTypeOpenEnrollment, encode(t), testenv.ExecuteOptions{})
	if err != nil {
		t.Fatalf("non-owner execute: %v", err)
	}
	if res.ResultCode == "ok" {
		t.Fatalf("non-owner open-enrollment was accepted: %+v", res)
	}

	// Owner attempt — must succeed and return enrollment_open.
	ownerGW, err := owner.DialAuthenticated(ctx, plat)
	if err != nil {
		t.Fatalf("dial owner: %v", err)
	}
	defer ownerGW.Close()
	res, err = ownerGW.Execute(ctx, lobbymodel.MessageTypeOpenEnrollment, encode(t), testenv.ExecuteOptions{})
	if err != nil {
		t.Fatalf("owner execute: %v", err)
	}
	if res.ResultCode != "ok" {
		t.Fatalf("owner result_code = %q, want ok", res.ResultCode)
	}
	got, err := transcoder.PayloadToOpenEnrollmentResponse(res.PayloadBytes)
	if err != nil {
		t.Fatalf("decode response: %v", err)
	}
	if got.Status != "enrollment_open" {
		t.Fatalf("status after open = %q, want enrollment_open", got.Status)
	}

	// Idempotent re-call — must not error and must still report
	// enrollment_open (or a conflict that the gateway maps to a
	// non-ok result_code without crashing the stream).
	res, err = ownerGW.Execute(ctx, lobbymodel.MessageTypeOpenEnrollment, encode(t), testenv.ExecuteOptions{})
	if err != nil {
		t.Fatalf("idempotent execute: %v", err)
	}
	if res.ResultCode == "" {
		t.Fatalf("idempotent execute returned empty result_code")
	}
}
