# Share Image Generator

## Objective

Give every shared link a proper preview image instead of a blank card.

Server-side generation of a per-page preview image, referenced by the page's existing share metadata.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Find where the app already sets its share metadata, most likely alongside Social Sharing or Shareable Deep Links, and point the image tag at the generated URL rather than adding a parallel metadata block.
2. Generate the image on the server from the record's own title, author, and a brand mark. Do not render it in the visitor's browser; the services that fetch previews do not run scripts.
3. Design one template that degrades well, then add variants only where the content type genuinely differs. Three good templates beat a configurable layout engine nobody tunes.
4. Store generated images with the record and regenerate on save, so the first person to share a page is not the one who waits for the render.
5. Do not generate a preview for anything the requester could not otherwise read. The image is public by definition and leaks the title of a private draft to anyone who guesses the URL.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- Cache each generated image and invalidate it when the underlying title, author, or cover changes. A stale preview outlives the edit by weeks because the platforms that fetched it cache aggressively too.
- Long titles and non-Latin scripts must wrap and shrink to fit rather than clip. Test with a title that is one very long word and with a script that needs a different font.
- Load fonts from a fixed, bundled set so two renders of the same record produce the same image. A font that resolves differently between runs makes output silently inconsistent.
- When generation exceeds its time budget or errors, serve a static branded fallback image with the correct dimensions rather than returning nothing and producing a blank card.
- Private, unpublished, and deleted pages must not generate previews at all, and any image already generated for them must stop being served.
- The endpoint is fetched by anonymous crawlers, so it needs rate limiting and a size cap or it becomes an easy way to burn server time.
- Emit correct width, height, and alt text in the metadata so the card reserves its space and describes itself.
- User-supplied text rendered into the image needs the same moderation treatment as the page itself; an abusive title otherwise ships as an image into other people's feeds.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Every publicly shareable page returns a generated preview image at the standard dimensions.
- [ ] Images regenerate when the source content changes and stale versions stop being served.
- [ ] Long titles and non-Latin scripts render fully without clipping.
- [ ] Repeated renders of the same record produce identical output.
- [ ] A generation timeout falls back to a static branded image rather than an empty response.
- [ ] Private and unpublished records produce no preview image.
- [ ] The generation endpoint is rate limited against anonymous traffic.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
