# AI Chart Recommendation

## Objective

Recommend a chart that fits the selected fields instead of leaving the choice to guesswork.

A suggestion of chart type and encoding for the fields a user has picked, with a short reason drawn from the shape of the data.

## 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. Enumerate the chart types the app can actually render, with their real constraints on cardinality, field types, and series count. Give the model that catalogue and require it to choose from it.
2. Profile the selected fields in the app before asking for a recommendation: types, distinct counts, null density, whether a field is a continuous time axis, and the row count. The model reasons about the profile, not the data itself.
3. Present the recommendation as a proposal the user applies. Do not silently switch a chart the user has already configured, and keep their choice sticky once they have made one.
4. Return a reason in terms of the data shape, such as too many distinct categories for a proportional chart. A recommendation with no stated reason cannot be argued with or learned from.
5. Do not send the underlying rows to the model. A field profile is enough to pick a chart type, and shipping customer data to a provider to choose between a bar and a line is an unnecessary disclosure.

## 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 recommendation naming a chart type the app cannot render is worse than no recommendation. Validate the returned choice against the catalogue and fall back to the deterministic default when it does not match.
- Some combinations are actively misleading and must be refused outright: a proportional chart across dozens of categories, a line across an unordered categorical axis, or a dual axis chosen to make two unrelated series appear correlated.
- The explanation must reference the actual profile, naming the distinct count or the field type that drove the choice, rather than offering a generic statement about clarity.
- A recommended chart still has to work at small widths, in both themes, and for a colour-blind reader. A type that needs a dozen distinguishable colours is not a valid recommendation regardless of what the model returns.
- When no visualization improves on the raw values, recommend a table. Sparse data, a single row, or a metric with no meaningful axis reads better as numbers.
- Set a token and time ceiling on the call and fall back to a deterministic rule set built from the same field profile. The user must never wait on a model to see a chart.
- A malformed or partial structured response must be discarded whole. Do not apply the chart type from a response whose encoding fields failed to parse.
- Recommendations must be recomputed when the field selection changes, and not left showing a reason about fields the user has since removed.

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

- [ ] Recommendations are drawn only from chart types the app can render, and invalid returns fall back to a deterministic default.
- [ ] The app profiles the selected fields locally and sends no underlying rows to the provider.
- [ ] Known-misleading type and field combinations are refused.
- [ ] Each recommendation states a reason referencing the concrete data shape.
- [ ] A table is recommended when no chart improves comprehension.
- [ ] The recommendation is a proposal the user applies, and never overwrites a chart they configured.
- [ ] A model timeout or failure still produces a usable chart from deterministic rules.
- [ ] 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.
