# OneDrive File Export

## Objective

Deliver app-generated files into a OneDrive folder or a SharePoint document library.

A Microsoft account connection that writes exports to a drive and folder the user selected, respecting tenant policy.

## 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. Offer OneDrive delivery on the same exports that already produce a file, and make the drive choice explicit: a personal drive, a work drive, and a SharePoint document library are different destinations with different rules.
2. Resolve the drive and folder during connection and store the resolved identifiers rather than the display path, so a renamed team folder does not force everyone to reconnect.
3. Keep tokens and the app's own registration secret on the server, refresh them ahead of expiry, and never place anything capable of writing to a tenant's files into client-side code.
4. Run transfers as background jobs with resumable chunked upload, backoff on throttling, and a visible per-file status. Return the destination to the user as a link that resolves through their existing permissions.
5. Dropbox File Export and Amazon S3 Backup Export cover the other destinations. Share the destination abstraction and the job pipeline with them; this entry owns only the Microsoft-specific drive resolution, tenant policy, and permission handling.

## 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 personal account and a work account behave differently in quota, sharing defaults, administrator policy, and which drives exist at all. Detect which kind of account connected and adjust rather than assuming one shape.
- An organization user may have several drives plus multiple SharePoint libraries. Make them pick one deliberately, and re-verify the drive still exists before each scheduled delivery.
- Uploads above the single-request threshold need a resumable session, and a name collision at the destination must resolve by a rule the app states — replace or rename — rather than being left to chance.
- Tenant policy or conditional access can block the app entirely, or block it from an unmanaged device or an unexpected location. Present that refusal as an administrator decision to escalate, not as a bug in the export.
- A link handed back to the user must rely on that user's existing permissions. Do not create an anonymous sharing link to make the link work for everyone, because it puts the file outside the organization.
- Administrator consent can be granted and later withdrawn for a whole tenant, invalidating every connection at once. Detect that class of failure and tell affected users to reconnect instead of silently retrying.
- The service throttles aggressively during business hours. Respect the retry interval it returns and spread bulk deliveries rather than pushing them all at once.
- When the service is unavailable the generated file stays downloadable in the app and delivery shows as pending with a retry.

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

- [ ] Exports can be delivered to a personal OneDrive, a work drive, or a SharePoint library, chosen explicitly by the user.
- [ ] The destination is stored as a resolved identifier and survives a folder rename.
- [ ] Credentials stay server-side, refresh automatically, and no secret appears in client code.
- [ ] Large files upload resumably and interrupted transfers continue rather than restart.
- [ ] Blocked tenant policy, withdrawn consent, and throttling each produce a distinct, actionable message.
- [ ] Links given to users resolve through their own permissions, with no anonymous sharing links created.
- [ ] A provider outage never loses the generated export.
- [ ] 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.
