# Notion Database Sync

## Objective

Create Notion pages from app records with properties mapped and long content split safely.

A sync that writes app records as pages in a selected database, with mapped properties and structured body content.

## 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. Confirm at setup, and again before each run, that the selected database is still shared with the integration, because sharing is granted per page and can be withdrawn without any notice to the app.
2. Read the database's property identifiers and types from the destination and map each app field by identifier, not by the label a user is free to rename.
3. Format each property according to its own type, and treat select, status, relation, and people properties as closed sets. Resolve the value before the page write; do not send an unknown value hoping the provider will create it.
4. Split body content into blocks that respect the provider's per-block and per-request limits, and preserve order across the several requests a long record will take.
5. Carry a stable app identifier in a dedicated property and look it up before writing, so a retried job updates the existing page rather than creating a second one. This is the same idempotency contract Airtable Record Sync uses; implement it once and share it.

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

- Access is granted by sharing a database with the integration, and any workspace member can unshare it. The sync must detect the loss and pause with a message rather than retrying against a database it can no longer see.
- Each property type accepts a different shape. Sending plain text to a date, number, or multi-select property fails, and the error often names only the property, so validate types before the request.
- Relation, people, select, and status properties reject values that do not already exist. Decide per field whether an unknown value is created, mapped to a fallback, or reported, and make that a setting rather than a guess in code.
- Long records exceed the block and request limits. Split content into valid blocks, send them in ordered batches, and make a partially written page recoverable rather than leaving half a record behind.
- Retries and redelivered events create a second page unless the sync looks up its own key first. Query by the identifier property before every create.
- Properties get renamed and deleted by whoever owns the database. A mapping stored by name breaks invisibly, while a mapping stored by identifier survives a rename and fails loudly on a delete.
- Rate limits are enforced per integration, so a backfill competes with ordinary writes. Pace the backfill and let user-triggered syncs take priority.
- Content copied out of the app may carry markup the destination interprets. Convert it into the destination's block structure rather than pasting raw markup into a text property.

## 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 sync verifies the database is still shared before each run and pauses with a clear message when it is not.
- [ ] Properties are mapped by identifier, so renaming a property in the destination does not break the sync.
- [ ] Each property type receives correctly formatted values, and unknown option values follow a configured rule.
- [ ] Long content is split into valid blocks and written in order, with partial writes recoverable.
- [ ] A stable key property makes repeated runs idempotent, and no retry creates a duplicate page.
- [ ] Backfills respect the provider's rate limits without blocking user-triggered syncs.
- [ ] 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.
