# Contentful Entry Sync

## Objective

Create and update Contentful entries against the connected space, environment, and locale.

A sync that writes app records into Contentful entries using the connected space's content model and locale configuration.

## 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. Have the operator select the space, the environment, and the default locale during connection, and store all three, because writing production content into a sandbox environment is an easy and expensive mistake.
2. Fetch the content model at connection time and again before each sync run, map app fields to entry fields by their stable identifiers, and validate the value type against the model before attempting the write.
3. Write every localized field under an explicit locale rather than relying on whatever the space treats as default, and state clearly which locales the app owns and which it leaves to editors.
4. Resolve and confirm every referenced entry and asset before writing the entry that points at them, and hold the write if a target does not exist rather than creating an entry with dangling references.
5. Do not blindly overwrite a published entry that has been changed by an editor since the app last wrote it. Compare against the version the app last saw and, on divergence, stop and report the conflict rather than discarding a person's work.

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

- Fields must be addressed by their stable identifiers, and the value type must be checked against the model first. A string sent into a field the model now defines as a reference fails after the request has already partially applied.
- Localized fields fall back between locales in ways that hide missing content. Writing only the default locale can leave other locales displaying stale text that looks current.
- References and assets must exist and be processed before the entry referencing them is written, and asset processing is asynchronous, so a link created immediately after upload can point at something not yet usable.
- An entry can be draft, published with no changes, published with unpublished changes, or archived. The app must decide which of these it may transition and must not resurrect an archived entry as a side effect of a routine sync.
- Editorial changes must not be silently clobbered. Detect that the entry moved on since the app last wrote it, and surface the conflict to a human rather than resolving it by overwriting.
- Rate limits apply per space and are easy to hit during an initial backfill. Batch, back off with jitter, and make a long backfill resumable so a limit part way through does not require starting over.
- The management credential expires or is revoked. Detect authentication failure distinctly from other errors, mark the connection as needing reconnection, and surface it in the app instead of failing quietly in a background job.
- When the destination is unavailable, the app's own content must remain saved and usable, with the sync marked as pending rather than the user's save being rejected.

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

- [ ] Space, environment, and locale are chosen explicitly at connection and stored.
- [ ] Field mappings use stable identifiers and are type-checked against the fetched content model before writing.
- [ ] Localized fields are written under an explicit locale, with the app's ownership of each locale stated.
- [ ] References and assets are confirmed to exist and be processed before the referencing entry is written.
- [ ] An entry changed by an editor since the last sync raises a conflict instead of being overwritten.
- [ ] Entry states including archived are handled explicitly, with no unintended state transitions.
- [ ] Rate limits and expired credentials surface as recoverable, operator-visible conditions.
- [ ] 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.
