# Gift Cards

## Objective

Sell gift cards that arrive on time and redeem cleanly against a real order total.

Purchasable gift cards with a delivered code, a tracked balance, and redemption applied at checkout alongside other payment.

## 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. Treat a gift card as a balance with an immutable ledger of transactions, not a mutable number. Issue, redeem, refund, and expire are all entries; the balance is their sum, which makes every discrepancy traceable.
2. Generate codes that cannot be guessed or enumerated, store only a hash of the code, and show the full code once at purchase and in the delivery email. A recoverable code is a recoverable bearer instrument.
3. Apply a card at checkout as payment, not as a discount. It reduces the amount due after tax and shipping are calculated, and the remainder goes to the card or wallet on file.
4. If the app already has Credit Balance or Coupon and Promotion Codes, extend the balance ledger and the checkout payment step rather than adding a parallel one. A gift card is a funded balance; a promotion code is a price rule, and conflating them breaks refunds.
5. Do not let redemption be validated on the client. Every application of a card must be a server-side operation that locks the card row, checks the balance, and records the entry atomically.

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

- Two checkouts using the same card at once must not both succeed against the same funds. Serialise redemptions on the card record and make the balance check and the ledger write a single atomic operation so the balance can never go negative.
- A card rarely covers the whole order. Support partial redemption where the card supplies what it has and the remainder is charged to a payment method, and make the split visible on the order and the receipt.
- Refunding an order paid partly by gift card must return the card portion to the card and the charged portion to the payment method. Refunding the whole amount to the card, or to the card holder's payment method, quietly creates or destroys money.
- Delivery can be scheduled for a future date, which means the send can fail long after the buyer has gone. Handle a bounced or rejected recipient address by notifying the buyer with the code so the gift is not simply lost.
- Expiry and dormancy rules differ by jurisdiction and in several places gift card funds may not expire at all. Make expiry configurable rather than hardcoded, keep the balance recoverable after the nominal expiry date, and record which rule applied.
- A card bought and then refunded before delivery must be voided, and a code that has already been sent must be handled as compromised rather than reissued to the same recipient.
- Repeated code entry attempts must be rate limited. Without it, the checkout field is a brute-force oracle against every card you have ever issued.
- The recipient may not have an account. Redemption must work for a guest checkout and must not require registration to spend a balance someone already paid for.

## 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 issue, redemption, refund, and adjustment appears as a ledger entry and the balance is their sum.
- [ ] Concurrent redemptions cannot take the balance below zero.
- [ ] A partial redemption charges the remainder to a payment method and shows the split on the order.
- [ ] Refunds return the gift card portion to the card and the charged portion to the original payment method.
- [ ] Scheduled delivery sends on the chosen date and a bounce notifies the buyer with the code.
- [ ] Codes are stored hashed, rate limited on entry, and unguessable.
- [ ] Expiry rules are configurable and the balance is not silently destroyed.
- [ ] 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.
