# Mapbox Geocoding

## Objective

Convert typed locations into coordinates and structured addresses the app can use.

Forward and reverse geocoding with confirmed matches, controlled result bias, and coordinate precision rules.

## 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 two directions explicitly: text to coordinates for entry and search, coordinates to address for a dropped pin or a device location. They take different inputs, carry different confidence, and fail differently.
2. Present ambiguous matches as a list the user confirms. Silently taking the first result is how a record ends up in the wrong country with nobody noticing for months.
3. Bias results toward a location only when the app genuinely knows one, such as a workspace address or the map viewport the user is already looking at.
4. Keep the request credential off the public page, or scope it to one capability and origin. Store returned coordinates at a precision that suits the use, and record who is permitted to see them.
5. Google Places Address Lookup covers address autocomplete for form entry. Where both exist, use one provider per field and keep their stored identifiers separate, because identifiers from the two providers are not interchangeable.

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

- Bias applied without real context distorts results — a user searching for a place in another country gets local noise instead of the answer. Guessed bias is worse than none.
- Two plausible matches must be offered as a choice. Automatic selection is acceptable only when the match is unambiguous and the app can explain why.
- The provider's terms govern attribution and how long results may be cached. Show the required attribution where results appear and respect the caching window rather than building a permanent local copy.
- Reverse lookup of a coordinate in open country may return only a broad region or nothing at all. Treat an empty result as a valid outcome, not an error.
- Coordinates describing a person are personal data. Round to the precision the feature actually needs, state who can see the exact value, and do not store street-level precision for a feature that only needs a city.
- Rate limits apply per credential. Debounce interactive lookups, run bulk work through the app's existing background-job system, and back off on throttling instead of hammering.
- A geocode failure must not block a save. Keep the typed text, mark the record as not yet geocoded, and retry later.
- Coordinates that arrive reversed or outside valid bounds must be rejected before they reach a map or a distance calculation.

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

- [ ] Forward and reverse lookup are separate paths with their own error handling.
- [ ] Ambiguous matches are confirmed by the user rather than auto-selected.
- [ ] Bias is applied only from known context and never from a guessed source.
- [ ] Required attribution is shown and caching stays within the provider's terms.
- [ ] Stored coordinate precision matches the feature's need and is covered by an access rule.
- [ ] A failed or empty geocode leaves the record saveable with the typed text intact.
- [ ] Bulk geocoding runs as background jobs that back off when throttled.
- [ ] 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.
