# Third-Party Account Import

## Objective

Pull existing data straight from another service instead of asking for a file.

An API-driven import that previews what will be created, then writes it in a resumable, deduplicated way.

## 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 the user pick what to import before authorizing, and request only the scopes those selections need. Do not ask for full account access to import one list.
2. Fetch through the provider's pagination to completion and persist a cursor as you go, so the job resumes from where it stopped rather than restarting.
3. Show a preview before writing anything: how many records will be created, how many match existing ones, and what the duplicate rule will do with them.
4. Store the provider's record ID alongside your own, scoped to the connection. Do NOT treat a provider ID as globally unique — two connections to the same provider will collide.
5. Establishing the connection itself is owned by OAuth Integration Connections, file-based import by CSV Import, and the progress display by Background Job Progress. Use them rather than rebuilding them.
6. Make the whole import reversible or at least identifiable — tag imported records with the import run so a bad import can be found and undone.

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

- Rate limits will be hit mid-import. Respect the provider's retry-after, pause the job, and resume — do not fail the whole run.
- The token can expire partway through a long import. Refresh and continue; if reauthorization is needed, pause and tell the user rather than discarding progress.
- The provider's data may change while you are paginating, producing duplicates or gaps. Deduplicate on provider ID rather than trusting page boundaries.
- Referenced records may point at objects the user did not select or does not have scope for. Import them as placeholders or skip them explicitly; do not fail silently.
- A user may run the same import twice. The second run must update matched records rather than doubling everything.
- Fields the provider allows and yours does not — longer text, unsupported characters, missing required values — need a stated per-record outcome, reported at the end.

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

- [ ] Requested scopes correspond to the data the user selected to import.
- [ ] Pagination completes and the job resumes from a persisted cursor after interruption.
- [ ] A preview shows create, match, and skip counts before any write occurs.
- [ ] Provider IDs are stored scoped to the connection, not as global identifiers.
- [ ] Re-running an import updates matched records instead of duplicating them.
- [ ] Every skipped or failed record is reported with a reason after the run.
- [ ] 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.
