<!--
Phase 23 Report View — my fleets section. Renders `localFleets[]`
with the wire fields. `origin` and `range` are nullable (a fleet
in orbit has neither); empty cells in those columns are normal.
-->
<script lang="ts">
	import { getContext } from "svelte";

	import { i18n } from "$lib/i18n/index.svelte";
	import {
		RENDERED_REPORT_CONTEXT_KEY,
		type RenderedReportSource,
	} from "$lib/rendered-report.svelte";
	import { formatFloat, planetLabel } from "./format";

	const rendered = getContext<RenderedReportSource | undefined>(
		RENDERED_REPORT_CONTEXT_KEY,
	);
	const report = $derived(rendered?.report ?? null);
	const rows = $derived(report?.localFleets ?? []);
	const planets = $derived(report?.planets ?? []);
</script>

<section
	id="report-my-fleets"
	class="grid-section"
	data-testid="report-section-my-fleets"
>
	<h2>{i18n.t("game.report.section.my_fleets.title")}</h2>

	{#if report === null}
		<p class="status">{i18n.t("game.report.loading")}</p>
	{:else if rows.length === 0}
		<p class="status" data-testid="my-fleets-empty">
			{i18n.t("game.report.section.my_fleets.empty")}
		</p>
	{:else}
		<table class="grid" data-testid="my-fleets-table">
			<thead>
				<tr>
					<th>{i18n.t("game.report.section.my_fleets.column.name")}</th>
					<th>{i18n.t("game.report.section.my_fleets.column.groups")}</th>
					<th>{i18n.t("game.report.section.my_fleets.column.state")}</th>
					<th>{i18n.t("game.report.section.my_fleets.column.destination")}</th>
					<th>{i18n.t("game.report.section.my_fleets.column.origin")}</th>
					<th>{i18n.t("game.report.section.my_fleets.column.range")}</th>
					<th>{i18n.t("game.report.section.my_fleets.column.speed")}</th>
				</tr>
			</thead>
			<tbody>
				{#each rows as f (f.name)}
					<tr data-testid="my-fleets-row" data-name={f.name}>
						<td>{f.name}</td>
						<td>{f.groupCount}</td>
						<td>{f.state}</td>
						<td>{planetLabel(f.destination, planets)}</td>
						<td>
							{f.origin === null ? "—" : planetLabel(f.origin, planets)}
						</td>
						<td>{f.range === null ? "—" : formatFloat(f.range)}</td>
						<td>{formatFloat(f.speed)}</td>
					</tr>
				{/each}
			</tbody>
		</table>
	{/if}
</section>

<style>
	.grid-section h2 {
		margin: 0 0 0.5rem;
		font-size: 1.05rem;
		color: #e8eaf6;
	}
	.status {
		margin: 0;
		color: #888;
		font-size: 0.9rem;
	}
	.grid {
		border-collapse: collapse;
		width: 100%;
		font-variant-numeric: tabular-nums;
		font-size: 0.9rem;
	}
	.grid th,
	.grid td {
		padding: 0.4rem 0.6rem;
		text-align: left;
		border-bottom: 1px solid #1c2240;
	}
	.grid th {
		color: #aab;
		text-transform: uppercase;
		letter-spacing: 0.04em;
		font-size: 0.75rem;
	}
	.grid tbody tr:hover {
		background: #11172a;
	}
</style>
