# Session heatmap API colors Implementation Plan

> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.

**Goal:** Render dashboard heatmap cell backgrounds using the upstream `cells[].color` value only and make the average selector navigate three-month windows.

**Architecture:** Extend the normalized heatmap cell type with a validated API color. Require a valid color for every weekly and average heatmap cell. The grid computes contrasting text color from the API color and never uses the client palette; remove the client palette legend. Keep weekly navigation unchanged, and make average navigation step in three-calendar-month periods using the period start as the existing upstream `week_start` anchor. Display the selected average period in the selector and subtitle even if the API returns a different rolling data window, per the user's direction.

**Tech Stack:** Next.js 16, React 19, TypeScript, Tailwind CSS v4.

**Spec:** `docs/superpowers/specs/2026-11-25-session-heatmap-api-colors-design.md`

## Global Constraints

- Treat each upstream cell color as authoritative; do not use the current client palette as a fallback.
- Require six-digit hex colors on all weekly and average cells.
- Keep the existing endpoints, weekly navigation, values, and grid layout.
- Derive readable text contrast from each cell's background color; do not substitute a background palette.
- Advance the average selector by three calendar months and preserve legacy `average_session_week_start` URLs.

## Review Focus

- Missing or malformed colors must reject the payload and reach the existing heatmap error state.
- White (`#FFFFFF`) cells with zero counts must remain visible and their count text readable.
- Dark API colors must use a contrasting foreground color.
- Weekly and average heatmaps must reject missing or malformed colors.
- Color values must be constrained to hex to prevent arbitrary CSS from entering inline styles.

---

### Task 1: Preserve and render upstream cell colors

**Files:**
- Modify: `src/features/dashboard/types.ts`
- Modify: `src/features/dashboard/api-contract.ts`
- Modify: `src/features/dashboard/session-start-heatmap.tsx`

**Interfaces:**
- `DashboardSessionHeatmapCell` gains required `color: string`; values are validated as `^#[0-9A-Fa-f]{6}$` in `parseSessionStartHeatmap`.
- The UI consumes the normalized `color` and keeps existing count and accessible-label behavior.

- [x] Add required `color` to `DashboardSessionHeatmapCell` and validate it for weekly and average cells in `parseSessionStartHeatmap`.
- [x] In `session-start-heatmap.tsx`, use each API color as the background and calculate black-or-white foreground text from relative luminance.
- [x] Remove `HEATMAP_LEVELS` and its legend so the view does not imply the old client palette; preserve the grid, values, and labels.
- [x] Run `npm run typecheck` and `npm run lint` from `tiar-csms`. Typecheck passed; repository lint found an unrelated existing error in `src/features/customers/customer-view.tsx`. ESLint passed on the three changed source files.

### Task 2: Navigate average heatmaps by three-month period

**Files:**
- Modify: `src/features/dashboard/heatmap-period.ts`
- Modify: `src/features/dashboard/api.ts`
- Modify: `src/features/dashboard/types.ts`
- Modify: `src/features/dashboard/session-start-week-navigator.tsx`
- Modify: `src/features/dashboard/session-start-heatmap.tsx`
- Modify: `src/app/(dashboard)/page.tsx`
- Modify: `README.md`

**Interfaces:**
- `DashboardSessionHeatmaps` exposes `selectedPeriodStarts: { weekly: string; average: string }`.
- `SessionStartWeekNavigator` accepts `granularity: "week" | "three-month"` and derives three-month labels from the selected period start.
- Average selections use the query key `average_session_period_start`; the route also accepts legacy `average_session_week_start` values.

- [x] Add validated date-only and calendar-month shift helpers; calculate a three-month window end as three months after the start minus one day.
- [x] Set the latest average start to today minus three calendar months; clamp requested average period starts to that date and send the selected start through upstream `week_start`.
- [x] Extend the navigator so weekly behavior remains unchanged and average navigation shifts by three months, uses period labels and accessibility text, and disables next beyond the latest period.
- [x] Use the selected period start to label the average selector and subtitle; keep the old average query key as an input alias while writing the new key.
- [x] Update the dashboard README to document the weekly and three-month average selectors.
- [x] Run `npm run typecheck` and lint the modified files from `tiar-csms`; both passed. Historical navigation still depends on the API honoring the selected period start supplied as `week_start`.
