# Abandoned Cart Recovery

## Objective

Bring back shoppers who left items behind, with a link that restores what they had.

A short reminder sequence triggered by an inactive cart with a captured email, carrying a link that rebuilds the cart.

## 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. Define abandonment as a cart with contents, a known email address, and no activity for a set period. Schedule the first reminder from the last activity timestamp, not from when the cart was created.
2. Keep the sequence short and stop it at the first sign of return. Two messages is usually the whole feature; the third mostly generates unsubscribes.
3. Give each reminder a signed, single-cart, expiring link that reconstructs the cart from the stored line items and drops the shopper onto the cart page, not the homepage.
4. Send through the app's existing transactional email path and honour the Email Preference Center. A recovery reminder is marketing, so it belongs to the marketing preference and the existing unsubscribe, not to a new one.
5. Do not send to an address the shopper merely typed into a checkout field and abandoned without a consent basis. Capture consent explicitly and record where and when it was given, because an unconsented reminder is a legal problem, not a growth tactic.

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

- The sequence must stop the moment the order is placed by any route, including a different device, a phone order, or a second cart. Check order state at send time as well as at schedule time, because a reminder queued an hour ago may already be wrong.
- An address with no captured consent basis must never be emailed. Store the consent record alongside the address, and treat a missing record as a refusal rather than a default yes.
- Restore links must expire and must refuse to load if the cart has since been claimed by a different account. A forwarded link that hands over someone else's cart exposes what they were buying and where they were shipping it.
- A shopper who abandons carts every week is browsing, not hesitating. Suppress the sequence for repeat abandoners and enforce a frequency cap so nobody receives more than one recovery message in a defined window.
- By the time the link is opened, items may be unavailable or repriced. Rebuild what is still purchasable, show clearly what changed or dropped out, and never restore a cart at the old price.
- A cart abandoned at the payment step and one abandoned after adding a single item are different situations, and sending the same message to both wastes the more valuable one.
- Every message needs a working unsubscribe that suppresses future recovery sends immediately, and unsubscribing must not also stop order confirmations.
- Sends must be idempotent. A retried background job must not produce a second copy of the same reminder.

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

- [ ] Reminders send only to addresses with a recorded consent basis.
- [ ] The sequence stops immediately once an order is placed by any route, checked at send time.
- [ ] Restore links are signed, expiring, and refused when the cart has changed owner.
- [ ] A restored cart reflects current prices and availability and names what changed.
- [ ] A frequency cap and repeat-abandoner suppression are enforced.
- [ ] Messages go through the existing email path and honour the existing preference center and unsubscribe.
- [ ] Retried jobs do not produce duplicate sends.
- [ ] 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.
