# `gamesrn-tw-defense-plan` — version 1

The JSON a TW planning session hands to the **Defense Plan** page (`/TW/defense-plan/`). Officers see everything; a member sees only their own accounts' squads.
Import it with **Import plan → Validate → Publish**, or POST the same body to the page's functions
(same logged-in officer session; no API keys):

| Endpoint | What it does |
|---|---|
| `POST /api/tw-plan/validate` | Checks the plan. Saves nothing. Returns `{ ok, errors, warnings, summary, diff, snapshot, current }`. |
| `POST /api/tw-plan/publish` | Validates again. With errors → HTTP 422 and the same result. Otherwise saves it as the **current** plan for its war and archives the previous one. Returns `{ ok, id, published_at, summary, url, archived }`. |
| `GET /api/tw-plan` / `GET /api/tw-plan?id=` | Plan list / one plan with its zone reviews. |

Every issue is `{ level: "error" | "warn", code, text, zone?, member?, ref? }` (`ref` = `"Z1#<index in squads>"` or `"hold#<index>"`).

**The war** is `guildId` + `twDate`. Publishing a second plan for the same war archives the first; nothing is deleted.

Unknown extra keys are ignored (they're kept with the stored plan).

## Top level

| Key | Type | Required | Notes |
|---|---|---|---|
| `format` | string | ✔ | Exactly `"gamesrn-tw-defense-plan"`. |
| `version` | number | ✔ | `1`. |
| `guild` | string | | Guild name, shown in the plan picker. |
| `guildId` | string | ✔ (or `guild`) | Comlink guild id. Used for the war key and to find the lock snapshot. |
| `opponent` | string | | |
| `twDate` | `YYYY-MM-DD` | ✔ | The TW's date. |
| `lockAt` | ISO time | | The TW lock. Used to find the snapshot when `lockSnapshot.eventId` doesn't match a capture. |
| `planName` | string | | |
| `generatedAt`, `generatedBy` | string | | Stored with the plan. |
| `lockSnapshot` | `{ eventId, file }` | | `eventId` = the Lock Snapshots event id (e.g. `plan:tw:2026-11-16T20:00:15.000Z`). |
| `squadsPerZone` | integer 1–100 | ✔ | Slots per zone. |
| `strategy` | string | | One paragraph; `**bold**` works. |
| `zones` | array | | All 10 zones (see below). |
| `squads` | array | ✔ | Defense squads (see below). |
| `offenseHolds` | array | | Squads and crons kept back for offense. |
| `flags` | array | | `{ severity: "error" \| "warn" \| "info", member, text }`. |

## `zones[]`

`{ "zone": "Z1", "type": "ground" | "fleet", "label": "Top front", "role": "hard front", "notes": "" }`

Zone numbers are **HotUtils'**: Z1 (top) and Z2 (bottom) are the front row, then Z3 (top) / Z4 (bottom),
Z6 (middle) / Z7 (bottom), Z9 / Z10 at the back. **Z5 and Z8 are the fleet zones** (top lane).
The map's type wins if `type` disagrees (warning).

## `squads[]`

| Key | Notes |
|---|---|
| `zone` | `"Z1"`…`"Z10"` |
| `order` | Order within the zone. The page groups a zone's squads by `lead`, in first-appearance order. |
| `member`, `allyCode` | `allyCode` is 9 digits; members are matched by ally code, else by name. |
| `squadSet`, `hotutilsSquad` | Labels. `squadSet` is the group heading and lead-chip fallback. |
| `lead` | Lead `baseId` (should equal `units[0].baseId`). |
| `units` | Ground: exactly 5 `{ baseId, name, relic }`, lead first. Fleet: capital ship first, then up to 7 ships (`{ baseId, name }`). |
| `datacron` | `null`, or the object below. |
| `why` | One line. |
| `flags` | Strings (or `{ severity, text }`) shown as warning chips. |

### `datacron`

```json
{ "id": "…", "set": 36, "level": 9, "focused": false, "character": "General Kenobi",
  "display": "Set 36 L9 \"General Kenobi\" — Defense 108.72 · …",
  "stats": [ { "stat": "Defense", "value": 108.72 } ],
  "l3": { "id": "…", "label": "Light Side", "text": "…" },
  "l6": { "id": "…", "label": "Jedi Vanguard", "text": "…" },
  "l9": { "id": "…", "label": "General Kenobi", "text": "…" },
  "cap": 9, "required": false }
```

- The page writes it character-first from `set`, `level`, `character` and `stats` (values are display numbers, already scaled), then the L3 / L6 / L9 ability labels on their own line. `display` is used only when `set`/`level` are missing.
- `required: true` shows **Required**, `false` shows **Suggested**.
- `id` is used for checks only and is never shown.
- `cap` = the highest tier the five unlock (3/6/9).

## `offenseHolds[]` — offense reservations

`{ member, allyCode, squadSet, units: [ { baseId, name, relic } … ] (or bare base ids), datacron: { …same object as a squad's datacron… } | null, note }`

- Plan a datacron for every reservation that should carry one, with the **full datacron object** (`id`, `set`, `level`, `character`, `stats`, `l3`/`l6`/`l9`) so the page can draw it; the short form `{ id, display, l3: { label }, l6: { label } }` still works.
- Reservations show in rows under the map for the selected account.

## Required datacrons

Set `datacron.required: true` on every squad (and reservation) whose datacron is marked **required in the HotUtils orders**. The page locks those: neither the member nor an officer can swap them. `required: false` (or absent) = suggested, and the member may swap it for any other datacron in their inventory that isn't on another of their squads.

## Edits made on the page

Members (their own accounts) and officers (anyone) can change things on the page; every change saves immediately as a row in `tw_defense_plan_edits` on top of the published plan — the plan JSON is never rewritten, and publishing a new plan for the war starts clean.

- **Datacron** (unless required): picked from the account's inventory at its latest TW lock.
- **Units**: officers, on any squad; members only on squads they added. Picked from the account's roster at the lock.
- **Added squads**: members add offense squads; officers can also add a squad to a zone for the selected member.

## Validation

**Errors (block publishing)**

- Bad JSON, wrong `format` or `version`; missing `guildId`/`guild`, `twDate` or `squadsPerZone`; `squads` not an array.
- A squad in a zone that isn't Z1–Z10; a zone with more squads than `squadsPerZone`.
- A fleet in a ground zone, or a ground squad in Z5/Z8.
- A ground squad without exactly 5 units; a fleet without a capital ship first, with two capital ships, or with more than 7 other ships.
- The same member using the same unit twice — across all squads **and** offense holds.
- The same datacron `id` used twice — across squads and holds.

**Warnings (shown, don't block)**

- A zone below `squadsPerZone`.
- Ezra (Exile) (`EZRAEXILE`) in any squad other than GL Ahsoka (`GLAHSOKATANO`).
- Datacron tier gate-locked: L7–L9 when any of the five is below R6; L4–L6 when any is below R4 (from the snapshot's relics when available, else the plan's).
- With a lock snapshot: a member not in it; a unit the member doesn't own or whose relic differs; a datacron id not in the member's inventory.
- Zone list problems (missing / duplicated zones, type disagreeing with the map), `lead` not matching the first unit, a datacron on a fleet, a datacron with no `id`.

**Lock snapshot.** The functions look up this guild's capture in Lock Snapshots by `lockSnapshot.eventId`, else the TW lock nearest `lockAt` (±12 h). If none is found, the snapshot checks are skipped and the preview says so.

## Test files

- `sample-plan.json` — synthetic guild and members, 3 slots per zone, with deliberate **errors** (Z1 over, a fleet in Z3, a 4-unit squad in Z7, JMK reused in an offense hold, a datacron id reused) and **warnings** (Z2 short, Ezra (Exile) outside GL Ahsoka in Z9, a gate-locked L9 cron in Z2).
- `sample-plan-fixed.json` — the same plan with the errors fixed; publishes with warnings only.
