# Cancellation Waitlist

## Objective

Offer a freed-up slot to the people waiting for it, in order.

A per-slot queue that automatically offers a released booking to waiting people one at a time.

## 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. Let someone join the queue from the same screen where they found the slot full, capturing only what a booking needs, and confirm their position back to them.
2. When a booking is cancelled or released, offer the slot to the first person in the queue with a stated claim deadline, and move to the next person only when that deadline passes or the offer is declined.
3. Hold the slot for the person holding the live offer. If the slot stays openly bookable during the offer window, the queue means nothing and whoever refreshes fastest wins.
4. If the app already has a Waitlist feature for signup or capacity, extend it with slot-level queues and offer expiry rather than building a second waiting-list concept with its own table and its own notification path.
5. Do not notify everyone in the queue at once and let them race. It generates a burst of disappointment for everyone but one person and trains people to ignore the notification.

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

- Every offer carries a claim deadline, and the slot moves to the next person automatically when it lapses. Without a deadline one unresponsive person blocks the queue until the appointment has passed.
- Two people must never claim the same released slot. Claim under a lock or a conditional update so the second claim loses cleanly and returns the person to their place in the queue.
- Entries whose slot has already started must be dropped rather than offered, and the person told the queue has closed for that time.
- Cap how many offers one person receives in a period. Someone waiting on a busy calendar can otherwise be notified repeatedly through the night.
- Every notification must carry a one-tap way to leave the queue that works without signing in, and leaving must take effect before the next offer is generated.
- A person holding a live offer who cancels their own place must release the slot immediately rather than letting the deadline run down.
- Respect the app's Quiet Hours if it has them, and decide explicitly whether a short claim deadline overrides them or the offer skips to the next person.
- Someone who books the slot through the normal flow while an offer is outstanding must be blocked, not silently allowed to jump the queue.

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

- [ ] People can join a waitlist for a full slot and see their position.
- [ ] A cancellation offers the slot to the first waiting person with a visible claim deadline.
- [ ] The slot is held for the duration of an offer and cannot be booked around it.
- [ ] Concurrent claims on one released slot resolve to a single booking.
- [ ] Entries for slots that have started are dropped rather than offered.
- [ ] Offer notifications are rate limited per person and each carries a working leave-the-queue action.
- [ ] The feature extends the app's existing waitlist rather than duplicating it.
- [ ] 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.
