// Phase 14 end-to-end coverage for the rename-planet flow. Boots an
// authenticated session, mocks the lobby + report + order routes,
// drives a click into the renderer to select a planet, opens the
// Rename action, types a new name, submits, and verifies the
// optimistic overlay (inspector + map label). A second test covers
// the rejected path: the engine answers `cmdApplied: false` and the
// inspector keeps the original name while the order tab row reads
// `rejected`.

import { fromJson, type JsonValue } from "@bufbuild/protobuf";
import { expect, test, type Page } from "@playwright/test";
import { ByteBuffer } from "flatbuffers";

import { ExecuteCommandRequestSchema } from "../../src/proto/galaxy/gateway/v1/edge_gateway_pb";
import { UUID } from "../../src/proto/galaxy/fbs/common";
import {
	UserGamesOrder,
	UserGamesOrderGet,
} from "../../src/proto/galaxy/fbs/order";
import { GameReportRequest } from "../../src/proto/galaxy/fbs/report";
import { forgeExecuteCommandResponseJson } from "./fixtures/sign-response";
import {
	buildMyGamesListPayload,
	type GameFixture,
} from "./fixtures/lobby-fbs";
import { buildReportPayload } from "./fixtures/report-fbs";
import {
	buildOrderGetResponsePayload,
	buildOrderResponsePayload,
	type CommandResultFixture,
} from "./fixtures/order-fbs";

const SESSION_ID = "phase-14-rename-session";
const GAME_ID = "14141414-1414-1414-1414-141414141414";
const WORLD = 4000;
const CENTRE = WORLD / 2;
const TURN = 4;

interface MockOpts {
	storedOrder: CommandResultFixture[];
	submitOutcome: "applied" | "rejected";
}

interface MockHandle {
	get submittedRenameName(): string | null;
}

async function mockGateway(page: Page, opts: MockOpts): Promise<MockHandle> {
	const game: GameFixture = {
		gameId: GAME_ID,
		gameName: "Phase 14 Game",
		gameType: "private",
		status: "running",
		ownerUserId: "user-1",
		minPlayers: 2,
		maxPlayers: 8,
		enrollmentEndsAtMs: BigInt(Date.now() + 86_400_000),
		createdAtMs: BigInt(Date.now() - 86_400_000),
		updatedAtMs: BigInt(Date.now()),
		currentTurn: TURN,
	};

	let storedOrder = opts.storedOrder.slice();
	let lastSubmittedName: string | null = null;
	let lastReportName = "Earth";

	await page.route(
		"**/galaxy.gateway.v1.EdgeGateway/ExecuteCommand",
		async (route) => {
			const reqText = route.request().postData();
			if (reqText === null) {
				await route.fulfill({ status: 400 });
				return;
			}
			const req = fromJson(
				ExecuteCommandRequestSchema,
				JSON.parse(reqText) as JsonValue,
			);

			let resultCode = "ok";
			let payload: Uint8Array;
			switch (req.messageType) {
				case "lobby.my.games.list":
					payload = buildMyGamesListPayload([game]);
					break;
				case "user.games.report": {
					GameReportRequest.getRootAsGameReportRequest(
						new ByteBuffer(req.payloadBytes),
					).gameId(new UUID());
					payload = buildReportPayload({
						turn: TURN,
						mapWidth: WORLD,
						mapHeight: WORLD,
						localPlanets: [
							{
								number: 17,
								name: lastReportName,
								x: CENTRE,
								y: CENTRE,
								size: 1000,
								resources: 10,
								capital: 0,
								material: 0,
								population: 850,
								colonists: 25,
								industry: 700,
								production: "drive",
								freeIndustry: 175,
							},
						],
					});
					break;
				}
				case "user.games.order": {
					const decoded = UserGamesOrder.getRootAsUserGamesOrder(
						new ByteBuffer(req.payloadBytes),
					);
					const length = decoded.commandsLength();
					const fixtures: CommandResultFixture[] = [];
					for (let i = 0; i < length; i++) {
						const item = decoded.commands(i);
						if (item === null) continue;
						const cmdId = item.cmdId() ?? "";
						// Decode the embedded planetRename payload to mirror it back
						// in the response.
						const inner = new (await import(
							"../../src/proto/galaxy/fbs/order"
						)).CommandPlanetRename();
						item.payload(inner);
						const submittedName = inner.name() ?? "";
						lastSubmittedName = submittedName;
						const applied = opts.submitOutcome === "applied";
						fixtures.push({
							kind: "planetRename",
							cmdId,
							planetNumber: Number(inner.number()),
							name: submittedName,
							applied,
							errorCode: applied ? null : 1,
						});
					}
					if (opts.submitOutcome === "applied") {
						storedOrder = fixtures;
						const head = fixtures[0];
						if (head !== undefined && head.kind === "planetRename") {
							lastReportName = head.name;
						}
					}
					payload = buildOrderResponsePayload(GAME_ID, fixtures, Date.now());
					break;
				}
				case "user.games.order.get": {
					UserGamesOrderGet.getRootAsUserGamesOrderGet(
						new ByteBuffer(req.payloadBytes),
					);
					payload = buildOrderGetResponsePayload(
						GAME_ID,
						storedOrder,
						Date.now(),
						storedOrder.length > 0,
					);
					break;
				}
				default:
					resultCode = "internal_error";
					payload = new Uint8Array();
			}

			const body = await forgeExecuteCommandResponseJson({
				requestId: req.requestId,
				timestampMs: BigInt(Date.now()),
				resultCode,
				payloadBytes: payload,
			});
			await route.fulfill({
				status: 200,
				contentType: "application/json",
				body,
			});
		},
	);

	await page.route(
		"**/galaxy.gateway.v1.EdgeGateway/SubscribeEvents",
		async () => {
			await new Promise<void>(() => {});
		},
	);

	return {
		get submittedRenameName(): string | null {
			return lastSubmittedName;
		},
	};
}

async function bootSession(page: Page): Promise<void> {
	await page.goto("/__debug/store");
	await expect(page.getByTestId("debug-store-ready")).toBeVisible();
	await page.waitForFunction(() => window.__galaxyDebug?.ready === true);
	await page.evaluate(() => window.__galaxyDebug!.clearSession());
	await page.evaluate(
		(id) => window.__galaxyDebug!.setDeviceSessionId(id),
		SESSION_ID,
	);
	await page.evaluate(
		(gameId) => window.__galaxyDebug!.clearOrderDraft(gameId),
		GAME_ID,
	);
}

async function clickPlanetCentre(page: Page): Promise<void> {
	const canvas = page.locator("canvas");
	const box = await canvas.boundingBox();
	expect(box).not.toBeNull();
	if (box === null) throw new Error("canvas has no bounding box");
	await page.mouse.click(box.x + box.width / 2, box.y + box.height / 2);
}

test("rename a seeded planet auto-syncs and the overlay survives reload", async ({
	page,
}, testInfo) => {
	test.skip(
		testInfo.project.name.startsWith("chromium-mobile"),
		"phase 14 spec covers desktop layout; mobile inherits the same store",
	);

	const handle = await mockGateway(page, {
		storedOrder: [],
		submitOutcome: "applied",
	});
	await bootSession(page);
	await page.goto(`/games/${GAME_ID}/map`);
	await expect(page.getByTestId("active-view-map")).toHaveAttribute(
		"data-status",
		"ready",
	);

	await clickPlanetCentre(page);
	const sidebar = page.getByTestId("sidebar-tool-inspector");
	await expect(sidebar.getByTestId("inspector-planet-name")).toHaveText("Earth");

	await sidebar.getByTestId("inspector-planet-rename-action").click();
	const input = sidebar.getByTestId("inspector-planet-rename-input");
	await input.fill("New-Earth");
	await sidebar.getByTestId("inspector-planet-rename-confirm").click();

	// Overlay applies immediately on `valid` — no Submit click is
	// required because the auto-sync pipeline drives the network.
	await expect(sidebar.getByTestId("inspector-planet-name")).toHaveText(
		"New-Earth",
	);

	// Open the order tab and assert the row plus the synced status bar.
	await page.getByTestId("sidebar-tab-order").click();
	const orderTool = page.getByTestId("sidebar-tool-order");
	await expect(orderTool.getByTestId("order-command-label-0")).toContainText(
		"New-Earth",
	);
	await expect(orderTool.getByTestId("order-command-status-0")).toHaveText(
		"applied",
	);
	await expect(orderTool.getByTestId("order-sync")).toHaveAttribute(
		"data-sync-status",
		"synced",
	);
	expect(handle.submittedRenameName).toBe("New-Earth");

	// Reload: the layout always polls user.games.order.get on boot,
	// so the overlay is rebuilt from the server's stored order even
	// when the local cache was wiped.
	await page.reload();
	await expect(page.getByTestId("active-view-map")).toHaveAttribute(
		"data-status",
		"ready",
	);
	await page.getByTestId("sidebar-tab-order").click();
	await expect(orderTool.getByTestId("order-command-label-0")).toContainText(
		"New-Earth",
	);
});

test("rejected submit keeps the old name and surfaces the failure", async ({
	page,
}, testInfo) => {
	test.skip(
		testInfo.project.name.startsWith("chromium-mobile"),
		"phase 14 spec covers desktop layout; mobile inherits the same store",
	);
	await mockGateway(page, {
		storedOrder: [],
		submitOutcome: "rejected",
	});
	await bootSession(page);
	await page.goto(`/games/${GAME_ID}/map`);
	await expect(page.getByTestId("active-view-map")).toHaveAttribute(
		"data-status",
		"ready",
	);
	await clickPlanetCentre(page);
	const sidebar = page.getByTestId("sidebar-tool-inspector");
	await sidebar.getByTestId("inspector-planet-rename-action").click();
	await sidebar.getByTestId("inspector-planet-rename-input").fill("Mars-2");
	await sidebar.getByTestId("inspector-planet-rename-confirm").click();

	await page.getByTestId("sidebar-tab-order").click();
	const orderTool = page.getByTestId("sidebar-tool-order");

	// The auto-sync pipeline reaches the server immediately after
	// the inline confirm; the rejected verdict surfaces through the
	// per-row status badge and the sync bar.
	await expect(orderTool.getByTestId("order-command-status-0")).toHaveText(
		"rejected",
	);
	await expect(orderTool.getByTestId("order-sync")).toHaveAttribute(
		"data-sync-status",
		"error",
	);

	await page.getByTestId("sidebar-tab-inspector").click();
	// Overlay does not apply rejected commands — old name persists.
	await expect(sidebar.getByTestId("inspector-planet-name")).toHaveText("Earth");
});
