// Vitest coverage for the Phase 23 Report View's bombings section.
// Representative for grid-shape sections (foreign/uninhabited
// planets, fleets, ship-groups, ships-in-production). Three
// scenarios — empty list, populated row, wiped row with badge —
// cover the empty-state copy and the conditional row state.

import "@testing-library/jest-dom/vitest";
import { render } from "@testing-library/svelte";
import { beforeEach, describe, expect, test } from "vitest";

import { i18n } from "../src/lib/i18n/index.svelte";
import type {
	GameReport,
	ReportBombing,
} from "../src/api/game-state";
import { RENDERED_REPORT_CONTEXT_KEY } from "../src/lib/rendered-report.svelte";
import { EMPTY_SHIP_GROUPS } from "./helpers/empty-ship-groups";

import SectionBombings from "../src/lib/active-view/report/section-bombings.svelte";

beforeEach(() => {
	i18n.resetForTests("en");
});

function bombing(
	overrides: Partial<ReportBombing> &
		Pick<ReportBombing, "planetNumber" | "planet" | "attacker">,
): ReportBombing {
	return {
		owner: "Owner",
		production: "Capital",
		industry: 0,
		population: 0,
		colonists: 0,
		industryStockpile: 0,
		materialsStockpile: 0,
		attackPower: 0,
		wiped: false,
		...overrides,
	};
}

function makeReport(rows: ReportBombing[]): GameReport {
	return {
		turn: 1,
		mapWidth: 1000,
		mapHeight: 1000,
		planetCount: 0,
		planets: [],
		race: "Self",
		localShipClass: [],
		routes: [],
		localPlayerDrive: 0,
		localPlayerWeapons: 0,
		localPlayerShields: 0,
		localPlayerCargo: 0,
		...EMPTY_SHIP_GROUPS,
		bombings: rows,
	};
}

function mountSection(report: GameReport | null) {
	const context = new Map<unknown, unknown>([
		[RENDERED_REPORT_CONTEXT_KEY, { get report() {
			return report;
		} }],
	]);
	return render(SectionBombings, { context });
}

describe("report bombings section", () => {
	test("renders the loading placeholder before the report lands", () => {
		const ui = mountSection(null);
		expect(ui.getByTestId("report-section-bombings")).toHaveTextContent(
			"loading report",
		);
	});

	test("renders the empty-state copy when there are no bombings", () => {
		const ui = mountSection(makeReport([]));
		expect(ui.getByTestId("bombings-empty")).toBeInTheDocument();
	});

	test("renders a non-wiped row without the wiped badge", () => {
		const ui = mountSection(
			makeReport([
				bombing({
					planetNumber: 17,
					planet: "Castle",
					attacker: "Ricksha",
					owner: "Earthlings",
					production: "Capital",
					industry: 500.25,
					population: 200,
					colonists: 12,
					industryStockpile: 30,
					materialsStockpile: 5,
					attackPower: 250,
					wiped: false,
				}),
			]),
		);
		const rows = ui.getAllByTestId("report-bombing-row");
		expect(rows).toHaveLength(1);
		expect(rows[0]).toHaveAttribute("data-planet", "17");
		expect(rows[0]).toHaveAttribute("data-wiped", "false");
		expect(rows[0]).not.toHaveClass("wiped");
		expect(rows[0]).toHaveTextContent("#17 (Castle)");
		expect(rows[0]).toHaveTextContent("Ricksha");
		expect(ui.queryByTestId("report-bombing-wiped-badge")).not.toBeInTheDocument();
	});

	test("renders a wiped row with the wiped badge and the row state", () => {
		const ui = mountSection(
			makeReport([
				bombing({
					planetNumber: 20,
					planet: "DW-1207",
					attacker: "Ricksha",
					owner: "KnightErrants",
					production: "Dron",
					industry: 1.5,
					attackPower: 7.62,
					wiped: true,
				}),
			]),
		);
		const row = ui.getByTestId("report-bombing-row");
		expect(row).toHaveAttribute("data-wiped", "true");
		expect(row).toHaveClass("wiped");
		expect(ui.getByTestId("report-bombing-wiped-badge")).toHaveTextContent(
			"wiped",
		);
	});
});
