# Image Text Extractor

## Objective

Turn a photo or scan of a document into text the user can copy and edit.

Text extraction from uploaded images and scanned documents, returned as editable, copyable text alongside the original.

## 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. Design around the wait. Extraction is not instant, so accept the upload, show a clear processing state, and let the user leave the page and come back to a finished result rather than holding a spinner.
2. Run extraction as a background job on the app's existing queue, with a bounded retry and a visible failed state. A request that blocks until a multi-page scan finishes will time out.
3. Reuse the app's existing File Upload path and, where one exists, its Multimodal Image Analysis pipeline, rather than standing up a second image ingestion route with its own limits and storage.
4. Present the extracted text next to the original image so the user can check it, and make it editable and copyable. Extraction is never perfect and a result nobody can correct is a result nobody trusts.
5. Do not silently present low-confidence output as fact. Surface the confidence, flag the passages the extractor was unsure about, and prompt the user to review them.

## 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 photos arrive rotated, skewed, shadowed, and noisy. Correct orientation, straighten, and clean the image before reading it, or accuracy collapses on exactly the inputs users supply most.
- Multi-column layouts, tables, and sidebars read as nonsense when scanned line by line across the page. Preserve the reading order of each column rather than interleaving them.
- Return a confidence figure with the result and mark the low-confidence regions, so poor scans can be flagged for review instead of quietly entering the system as correct text.
- A long multi-page file must be processed page by page in the background, with progress shown and partial results kept, rather than one request that runs until it times out.
- Uploaded documents are often sensitive. Delete the source image on the retention schedule the customer was told about, and make that schedule visible in the interface.
- Enforce page count, file size, and type limits on the server, and reject clearly over-limit files before any processing cost is incurred.
- An image containing no readable text must return an explicit empty result with an explanation, not an error and not a blank editor.
- Extracted text may contain personal data. Apply the same access rules to the result as to the source file, and remove both together when the record is deleted.

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

- [ ] A user can upload an image or scan and receive editable, copyable text.
- [ ] Extraction runs in the background with a visible processing, failed, and complete state.
- [ ] Skewed, rotated, and noisy photos are corrected before reading.
- [ ] Multi-column documents preserve their reading order.
- [ ] Results carry a confidence indication and low-confidence passages are flagged.
- [ ] Multi-page files process page by page without timing out and keep partial results.
- [ ] Source images are deleted on the stated retention schedule and share the result's access rules.
- [ ] 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.
