# Availability Calendar

## Objective

Set the hours you take bookings, with one-off blocks for holidays and busy days.

An owner-facing editor for weekly working hours plus date-specific overrides and blocked dates.

## 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. Model availability in two layers: a weekly pattern of open hours, and date-specific overrides that replace the pattern for a single day. Keep them separate rather than expanding the pattern into thousands of stored days.
2. Generate open slots lazily for the window actually being viewed, and cache the result. Slot generation is a function of the rules, existing bookings, and a bounded date range.
3. Store the owner's time zone on the schedule and interpret every entered hour and blocked date in that zone. Reuse the existing Time Zone-Aware Scheduling rather than converting by hand.
4. If the app already has a Recurring Schedule Builder, extend it for the weekly pattern instead of writing a second recurrence editor, and keep the override layer as the new work.
5. Do not silently cancel bookings when hours shrink. Show the owner exactly which existing bookings now fall outside availability and make them decide, since those bookings have already been promised to someone.

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

- A date-specific override must win over the weekly default for that day, including an override that closes a normally open day and one that opens a normally closed one. Resolution order has to be explicit, not incidental.
- Hours that cross midnight, such as an evening block ending at one in the morning, belong to two calendar days. Split shifts within one day, like a morning and an afternoon block with a lunch gap, must be supported as separate ranges rather than one span.
- Shrinking hours can leave existing bookings outside the new window. Detect the conflicts, list them, and require an explicit choice; recomputing slots without checking silently orphans real appointments.
- Blocked dates are entered in the owner's zone. Interpreting them in the server's zone shifts a holiday block by hours and leaves part of the day bookable.
- Generating slots for every date into the far future is unbounded work. Cap generation to the configured booking horizon and produce slots only for the range being requested.
- An end time equal to or before the start time must be rejected at entry, not stored as a zero-length or negative window.
- A slot duration and buffer that do not divide the open window evenly leave a remainder. Decide whether the trailing partial slot is dropped or the window is extended, and be consistent.
- Changing the schedule's time zone reinterprets every rule. Warn the owner and show what the hours become before saving.

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

- [ ] The owner sets weekly hours and date-specific overrides from one screen.
- [ ] Date-specific overrides take precedence over the weekly pattern in both directions.
- [ ] Midnight-crossing hours and split shifts are stored and rendered correctly.
- [ ] Reducing availability surfaces every conflicting booking and requires an explicit decision.
- [ ] Blocked dates apply to the owner's local day boundaries.
- [ ] Slot generation is bounded by the booking horizon and never runs unbounded for far-future dates.
- [ ] Invalid ranges are rejected at entry with a clear message.
- [ ] 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.
