# Photo Editor

## Objective

Let people crop, rotate and adjust a photo before it is saved, without leaving the app.

An in-browser editing step between choosing an image and storing it, covering crop, rotation, straightening and basic tone adjustment.

## 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. Attach this to the app's existing upload path rather than adding a second one. File Upload, Avatar Upload and any media upload already in the app should route through the same editor step, and the app's existing size and type limits still apply server-side.
2. Keep the tool list short and useful: crop to a chosen ratio, rotate in quarter turns, straighten by a small angle, and adjust brightness and contrast. Filters and drawing tools are a separate feature and should be deferred, not half-built.
3. Edit against a downscaled proxy for responsiveness, then apply the recorded operations to the full-resolution original on the server. Saving what the browser rendered on screen ships a blurry image.
4. Store the original file untouched and keep the edit as a set of operations, so a crop can be widened again later and a mistake is reversible.
5. Do not build a free-form crop only. If the image feeds a fixed slot such as an avatar or a card header, offer that slot's ratio as the default and let the app's existing focal point handling take over from there.

## 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

- Phone cameras record orientation in metadata rather than in the pixels. Read that orientation, apply it before editing, and write the result upright, or portraits will flip on save.
- The proxy the customer edits is not the file that gets stored. Record crop and rotation as proportions of the image rather than pixel offsets, so the same operations apply cleanly to the full-resolution original.
- The original must survive the edit. Keep it in storage under the app's normal retention and access rules so an edit can be undone, and make clear who is allowed to read the original back.
- A very large image will exhaust browser memory before it ever renders. Decide a ceiling above which the browser downscales before loading, and reject beyond that with a clear message rather than freezing the tab.
- Photos carry GPS coordinates. Strip location and camera metadata on export by default, and treat keeping it as an explicit opt-in.
- Leaving the editor with unsaved crops must warn, and a half-finished upload that is abandoned must be cleaned up rather than left occupying storage.
- Transparency must survive. Flattening a logo onto a white background because the export format has no alpha channel is a silent data loss.
- The edited image still needs alt text and a reserved aspect ratio wherever it is displayed, so the surrounding layout does not jump when it loads.

## 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

- [ ] Editing happens inside the app's existing upload flow, not in a parallel uploader.
- [ ] Crop, rotation, straightening and tone adjustments are applied to the full-resolution original, not the on-screen proxy.
- [ ] Orientation metadata is honoured and images save upright.
- [ ] The original file is retained and every edit can be reverted.
- [ ] Location metadata is removed on export unless the user opts in.
- [ ] Oversized images are downscaled or refused with a message instead of hanging the browser.
- [ ] Abandoned edits warn before discard and leave no orphaned files in storage.
- [ ] 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.
