# War-room map — the four signals and the data behind each

The state war room (`/bravo-map/view/?state=os`) renders four independent signals on
every polling unit. This is what each one needs, per election cycle, so 2026 → 2027 →
2028 all light up the same way with no code changes.

| # | Signal | Rendered as | Source table | Status today (Osun) |
|---|--------|-------------|--------------|---------------------|
| 1 | Result (margin) | Column height | `results` (`level='pu'`) | Complete — 3,763 PUs |
| 2 | Winning party | Column colour | `results` + `parties.color` | Complete — 11 distinct winners |
| 3 | Election status | Ground disc under the column | `pu_readiness`, else `results` | **Needs a feed** — see below |
| 4 | Incidents | Beacon on the column tip + ground halo | `incidents` | Live — field reports flow in |

---

## 1 + 2 · Results and winning party

Already loaded for `gov-osun-2026`. For each new election, results arrive through the
existing importer / Google-Sheet connection. Per polling unit the map needs:

```
pu_code        29-01-01-001      matches polling_units.pu_code exactly
party_code     APC               matches parties.code
votes          373
```

The map derives winner and margin itself; nothing extra is required.

---

## 3 · Election status — the one signal that needs a new feed

Status is a five-stage ladder. A unit sits at the furthest stage reported for it:

| Code | Stage | Meaning | Colour |
|------|-------|---------|--------|
| 0 | `awaiting` | Nothing reported yet | slate `#64748b` |
| 1 | `arrived` | Officials on ground | amber `#f59e0b` |
| 2 | `voting` | Accreditation / voting running | blue `#38a0ff` |
| 3 | `closed` | Polls closed, counting | violet `#a78bfa` |
| 4 | `declared` | Result submitted | green `#22c55e` |

`declared` is derived automatically the moment a unit has results — no feed needed for
it. **Stages 1–3 are what Osun must supply**, and they only exist while a poll is
actually running.

### How to send it

`POST /api/states/OS/readiness` — permission `incidents.report` or `results.manage`.

```json
{
  "election": "gen-2027",
  "reported_at": "2027-02-27T08:15:00+01:00",
  "units": [
    { "pu_code": "29-01-01-001", "officials_arrived": true },
    { "pu_code": "29-01-01-002", "voting_commenced":  true },
    { "pu_code": "29-01-01-003", "voting_completed":  true }
  ]
}
```

Response:

```json
{ "election": "gen-2027", "applied": 3, "unknown_pu_codes": [] }
```

Properties that make this safe for field use:

- **Monotonic** — reaching a later stage implies the earlier ones. Reporting
  `voting_commenced` also marks `officials_arrived`. A stage never regresses.
- **Idempotent** — replaying the same batch changes nothing, so the offline outbox can
  retry freely.
- **Batched** — up to 2,000 units per call, resolved in one query.
- **Election-scoped** — omit `election` and it is derived from `reported_at` against the
  election calendar, so a report filed on polling day files itself correctly.
- **Unknown codes are returned, not silently dropped** — check `unknown_pu_codes`.

### What we need from Osun

1. **Who reports it.** Presiding officers, party agents, or the existing incident
   officers? Whoever it is needs the `incidents.report` permission and a posting.
2. **How it reaches us.** Three options, all supported by the same endpoint:
   - the field app posts per unit as the day progresses (best fidelity);
   - a coordinator posts LGA-level batches by phone/WhatsApp relay;
   - a Google Sheet with `pu_code, officials_arrived, voting_commenced, voting_completed`
     synced on a timer through the existing sheet connector.
3. **Nothing for past elections.** `gov-osun-2026` has fully declared, so every unit
   correctly reads `declared`. Backfilling readiness for a finished poll would be
   invented data and is deliberately not done.

---

## 4 · Incidents

Already flowing from the field app. Two Osun reports are live and plotted.

One thing changed here: incidents used to be stored with `election_id = NULL` because
the field form never asks which election it is. Every election-scoped view — including
this map — filtered them out, which is why the map appeared to have no incident layer
at all. Attribution is now derived from the incident's date at write time
(`App\Support\ElectionResolver`), and historical rows were repaired with:

```
php artisan incidents:backfill-elections --state=OS        # --dry-run to preview
```

Run that once after importing any historical incident set.

---

## The colour language (why red moved)

Red now means **one** thing on this map: there is an incident here.

It previously meant two. APC's brand colour `#e11d2e` and the critical-incident colour
`#ef4444` measured **ΔE 5.7 for normal vision** (the legibility floor is 15) — and APC
wins 92% of Osun's polling units, so the most urgent signal on the map was hidden
inside the most common one. Two further collisions existed: PDP green against the
"declared" status green (ΔE 11.7), and Accord against the "officials arrived" amber.

The fix, validated rather than eyeballed:

| Channel | Encoding | Colours |
|---|---|---|
| Incident | **Column body** — grey when clear, red when flagged | `#a8adb3` → `#fecaca` / `#f87171` / `#e5484d` |
| Winning party | Cap disc on the column tip | party colour |
| Margin | Column height | — |
| Status | Ground collar | one hue, dim→bright: `#64748b` `#0e7490` `#06b6d4` `#67e8f9` `#cffafe` |

A flagged column is also **wider**, because height is already spoken for by margin and
colour alone can be occluded behind a taller stronghold in a 3D view.

Party colours changed (in `parties`, so every chart and badge follows automatically):

| Party | Was | Now | Why |
|---|---|---|---|
| APC | `#e11d2e` | `#2f7ff0` | red is reserved for incidents |
| Accord | `#b06a1f` | `#f2b134` | too close to incident red (ΔE 13.0) and dim on terrain |
| ADC | `#0d9488` | `#14b8a6` | better tritan separation (4.2 → 9.8) |
| NNPP | `#2563eb` | `#1e3a8a` | was ΔE 7.1 from APC's new blue |

Measured separations for the parties that decide the Osun map (all-pairs, dark
basemap): APC / Accord / ADC worst normal ΔE **18.6**, worst CVD **13.8**; against
incident red, worst normal **15.7**, worst CVD **9.2**. ADC was moved off green
specifically because green against critical red is **ΔE 1.8 under deuteranopia** —
the classic red-green trap.

Re-run these checks after any palette change with the validator in the `dataviz` skill:
`node scripts/validate_palette.js "<hexes>" --mode dark --surface #2a3520 --pairs all`
(a map is the all-pairs case — any two units can sit side by side).

## Realtime

Pusher carries three signals. All three are dispatched **before** any notification
preference is consulted, so muting the chime never stops the map from updating.

| Event | Channel · name | Fired by | Map reaction |
|---|---|---|---|
| `SheetSynced` | `live-results` · `sheet-synced` | `SheetSyncService` after a sheet sync | refetch results + incidents + polling units |
| `ResultsUpdated` | `live-results` · `sheet-synced` | `bravo:import-results` after a portal upload | same |
| `IncidentReported` | `incidents` · `new-incident` | field report | refetch incidents only |

Notes on what was broken here:

- `ResultsUpdated` existed but **was never dispatched** — a portal results upload
  updated the database and busted the cache, but no open war room heard about it. The
  importer now fires it, and the sheet path passes `--no-broadcast` so a sync pushes
  once rather than twice.
- A new field incident only raised a toast; the map layer never reloaded. It now
  dispatches `bravo:incident`, which reloads the incident layer **only** — the results
  query is a multi-second scan and must not run for one report.
- Polling units were fetched once per state, so a sync that rebuilt the PU registry
  (names, coordinates, PVC) left the map drawing stale units until a page reload.
- A sync briefly nulled the results overlay, which tripped the "don't strand the user"
  guard and **switched the map back to Clusters** — every sheet sync threw the operator
  out of the layer they were watching.

Measured end to end (real Pusher socket, real DB write, real repaint): the browser
receives the push, refetches all three payloads, the hover card shows the new winner
without a reload, and the map stays on the layer it was on.

Endpoint timings, for expectation-setting: `/results` ≈ 5.3s (185 KB),
`/polling-units` ≈ 3.0s (1.0 MB), `/incidents` ≈ 0.4s. "Realtime" therefore means
roughly a five-second settle after a sync, not instant.

## Election picker

The map lists an election as soon as it has **either** results **or** readiness data for
the state, and auto-selects whichever the state was most recently active in. That is what
makes a live poll reachable on the morning it runs, before anything has declared.

For a new cycle, create the election first (Portal → Elections) so `election_date` is set
— date is what drives incident attribution and auto-selection.
