# Scheduled Publishing

## Objective

Publish or unpublish content automatically at a chosen future time.

Durable, idempotent scheduled transitions that survive worker downtime and stay correct when the content or the author's permissions change.

## 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. Persist the schedule on the record itself, not only in a job queue. A queue that is drained, migrated, or lost must be recoverable by re-reading the pending schedules from the database.
2. Make the transition job idempotent and keyed to the intended run. Running it twice, or late, must produce the same result as running it once on time.
3. On worker recovery, process overdue schedules rather than skipping them — but cap how far back you will reach, and report anything published significantly late instead of doing it silently.
4. Define what happens when the content is edited after being scheduled: publish whatever is current at the moment of firing, or publish the version that was scheduled. Say which, and show the user that rule in the scheduling UI.
5. Do not build your own status model or your own time-zone picker. The draft/published states and their access rules are owned by Draft and Publish States; time-zone selection and DST handling are owned by Time Zone-Aware Scheduling. Extend both rather than adding a second one.

## 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 schedule must be cancellable, and cancelling must remove the pending job as well as the field on the record.
- If the author loses permission to publish before the fire time, the schedule must not run on their behalf — fail it and notify someone.
- Manually publishing before the scheduled time must clear the schedule, not leave a job that republishes later.
- Deleting or archiving the record must cancel every schedule attached to it.
- A scheduled unpublish and a scheduled publish on the same record must resolve in a defined order, not by whichever job wins.
- Show the pending time in the user's own zone alongside the stored intent, and warn when a scheduled time has already passed.

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

- [ ] Pending schedules are stored on the record and can be rebuilt after a queue loss.
- [ ] Running the transition job twice produces the same result as running it once.
- [ ] Overdue schedules are processed on recovery within a defined window and reported when late.
- [ ] The changed-content rule is documented and visible where users schedule.
- [ ] Cancellation, manual publish, permission loss, and deletion all invalidate the schedule.
- [ ] Status model and time-zone handling reuse the existing features rather than duplicating them.
- [ ] 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.
