# Shopify Order Import

## Objective

Import Shopify orders into the app with money, fulfilment, and later edits accurately kept.

A resumable import of the connected store's orders, mirroring line items, discounts, taxes, and fulfilment state locally.

## 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. Separate the initial backfill from the ongoing feed. The backfill walks history in a stable order behind a resumable cursor; the feed applies live updates as they arrive.
2. Key every imported order on the shop identifier together with the store's own order identifier, so re-running the import updates existing rows instead of duplicating them.
3. Store money as an exact decimal with its currency alongside it, and keep both the shop currency and the presented currency where they differ. Do not convert to a single display currency on the way in, because the original amounts cannot be recovered afterwards.
4. Treat an order as a state machine rather than a snapshot. Edits, cancellations, refunds, and partial fulfilments all arrive later as new versions of a record you already hold.
5. Customer identity and marketing consent are owned by Shopify Customer Sync. Link to the customer record that brief maintains rather than writing customer fields from an order payload.

## 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 backfill interrupted halfway must resume from its cursor without re-importing what it already wrote. Restarting from the beginning is acceptable only if every write is keyed and idempotent.
- External identifiers are unique per store, not globally. Two connected shops will eventually produce the same order number, and an unscoped key silently overwrites one with the other.
- Test orders placed while a store was in a sandbox or test payment mode must be flagged or excluded, or they will inflate every revenue figure the app reports.
- An order can be edited after the fact, cancelled, refunded in part, and fulfilled across several shipments. Each of those is a later version of the same order and must replace the local state rather than add a second row.
- Money must never pass through a floating-point value. Multi-currency stores also present two amounts for the same order, and the app has to record which is which.
- Updates arrive for an order while the backfill is still running, sometimes before that order has been imported at all. Queue them and apply them by version or timestamp so older backfill data cannot overwrite a newer change.
- Orders reference products and variants that may since have been deleted. Keep the title, price, and identifier the line item captured at the time rather than depending on a live product lookup.
- A store with years of history will hit the provider's rate limits during a backfill. Pace the import and back off when refused rather than retrying in a tight loop.

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

- [ ] A backfill can be interrupted and resumed, and re-running it produces no duplicate orders.
- [ ] Order keys include the shop, so two connected stores cannot collide.
- [ ] Cancelled, refunded, edited, test, and partially fulfilled orders each land in a distinct, queryable local state.
- [ ] Money is stored as exact decimals with explicit currency, and multi-currency orders retain both amounts.
- [ ] Updates arriving mid-backfill are applied in the correct order and are never overwritten by older data.
- [ ] Customer records are linked from Shopify Customer Sync rather than created here.
- [ ] 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.
