# Authoring a new invitation template

Templates are **data + a renderer**. Adding one never touches customer
data, routing, or the editor — the engine picks it up automatically.

Template definitions live in the `templates` table (seeded by
`database/seeders/TemplateSeeder.php`, managed at `/admin?tab=templates`).
Renderers are plain functions returning HTML strings in
`public/js/engine.js`.

## 1. Create the renderer — a function in `public/js/engine.js`

A renderer receives a context `{ tpl, data, invitationId, mode, lang, hidden, first }` and returns an HTML string:

```js
function rMine(ctx) {
  const { data, lang, mode, hidden, first } = ctx
  const n = invNames(data, lang)              // picks BN names when lang='bn'
  return `
    <div class="inv inv-mine">
      <!-- your art direction here -->
      ${!hidden.has('rsvp') && data.rsvp.enabled ? rsvpHtml(data, lang, mode) : ''}
    </div>`
}
// register it in the engine's RENDERERS map, then set
// `renderer: 'mine'` on the template row
```

Shared helpers in `engine.js`: `countdownHtml(iso, lang)`,
`mapLink(ev, lang, mode, cls)`, `rsvpHtml(data, lang, mode)`, `divider()`,
`invNames()`, `firstEventDate()`, `initials()`. Invitation styles go in
`public/css/invite.css` using the `--inv-*` custom properties.

Rules:

- **Style through the CSS vars** — `--inv-bg --inv-surface --inv-ink --inv-muted --inv-accent --inv-accent-soft --inv-line --inv-display --inv-body`. Never hardcode theme colors; variants and accent overrides depend on them.
- **Respect `hiddenSections`** for every optional section and `data.events`/`data.gallery` emptiness — render nothing when absent.
- **Use `MapLink`** for map/directions links (renders inert markup inside previews).
- **`mode === 'preview'`** runs inside scaled, pointer-events-none thumbnails. Avoid focusable elements; use inert stubs.
- Honour `prefers-reduced-motion` for any animation.

## 2. Create the row — admin UI or seeder

At `/admin?tab=templates&new=1` an admin fills in name, tagline,
description, renderer, `min_plan`, optional `price_bdt`, categories,
languages, accent and variants (the form posts the live preview's variant
list as a JSON `variants` field). Server-side validation requires:

- `renderer` ∈ `Template::RENDERERS`
- 1–12 variants, unique ids, `{id, name, tokens}` exactly the
  `VARIANT_TOKENS` keys, each token `#rgb` or `#rrggbb`
- `min_plan` ∈ the plan order; optional `price_bdt` integer

New rows get default sections `['cover','couple','events','rsvp']` and a
conservative cap set; richer defaults belong in `TemplateSeeder`.

Key fields on the seeded records:

| Field | Meaning |
| --- | --- |
| `sections` | Sections the editor may show; `Template::supportedSections()` additionally exposes `gallery` when the renderer supports it (`heritage`, `botanical`, `jewel`) and `caps.galleryMax > 0` |
| `caps` | `colorways`, `customAccent`, `fontPairs`, `reorderSections`, `hideSections`, `photos`, `events`, `galleryMax` — the server validates `WeddingData` against these |
| `variants` | Theme token sets; `Template::effectiveVariants()` falls back to `Template::defaultVariants()` for legacy rows |
| `version` | Bumped on every admin edit |

## 3. Snapshots — edits never rewrite existing invitations

When an invitation is created it stores a whitelisted copy of the
template (`template_snapshot`). `Invitation::renderTemplate()` prefers the
snapshot, falling back to the live row for legacy invitations. Before an
admin saves a template change (edit or publish toggle), every
still-unsnapshotted invitation is frozen to the *pre-change* attributes
inside the same lock — so a design update can never silently alter a
couple's existing invitation, while newly created ones pick up the new
design automatically.

## 4. Artwork

Drop original or properly licensed JPG/PNG assets into `public/assets/`
and record them in `docs/ASSETS.md`. Photos referenced from `WeddingData`
must be curated `/assets/…` paths or canonical `/media/{uuid}` assets the
invitation owner uploaded — arbitrary URLs are rejected by the validator.

## 5. Verify

- The new design appears in `/designs` and `/admin?tab=templates` automatically.
- Admin preview renders it with `Template::defaultData()` seed content.
- Test at 390 px and 1440 px, in `en`, `bn`, and `bilingual`.
