# Natural Language Report Builder

## Objective

Describe the report you want and get a configuration you can check before it runs.

A plain-language entry point to the app's report builder that produces a normal, editable report configuration.

## 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. Expose the app's defined metrics, dimensions, and grouping options to the model as an explicit vocabulary, and have it return a report configuration in the same shape the builder already saves. It must not invent a metric name or a calculation.
2. Show the generated configuration in the builder for review and require the user to run it. Do not execute a potentially expensive aggregation on the model's first guess.
3. Maintain a controlled glossary mapping business terms to defined metrics — what active means, what a qualified lead is, which revenue figure counts — and resolve ambiguous language through that glossary rather than letting the model decide each time.
4. Validate the configuration against the user's data permissions before it can run, and reject combinations of dimensions that would expose restricted records through a small-group breakdown.
5. Once generated, the report must be an ordinary saved report that runs without any model involvement. Filters on a plain list view are owned by Natural Language Filters; this feature covers metric, grouping, and date-range configuration only.

## 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 request for a metric the app does not define must be refused by name. Restricting the model to the declared vocabulary is what prevents a plausible-looking report built on a number nobody computes.
- The configuration must be visible and editable before the query runs, because a misread grouping can turn a cheap report into one that scans the whole dataset.
- Business terms mean different things in different workspaces. Route them through the glossary and show which definition was used, rather than letting the model pick a reasonable meaning silently.
- Some dimension combinations expose individuals even when each dimension alone is permitted. Block breakdowns that fall below a minimum group size or that cross a permission boundary.
- A saved report must reproduce identically later without calling the model again. Persist the resolved configuration, not the original sentence, or the report changes meaning between runs.
- If the model returns a configuration that fails validation, keep the user in the builder with their request intact and explain which part could not be resolved.
- Report runs can be slow and expensive. Estimate the cost of the generated configuration and warn before running anything unusually large.
- When the model is unavailable, the manual report builder must remain fully functional with the natural language entry point clearly disabled.

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

- [ ] Generated configurations use only metrics and dimensions the app defines.
- [ ] The configuration is shown for review and requires an explicit run before any query executes.
- [ ] Ambiguous business terms resolve through the glossary and the definition used is visible.
- [ ] Combinations that would expose restricted data or fall below the minimum group size are blocked.
- [ ] A saved report reruns identically with no further model involvement.
- [ ] Validation failures return the user to the builder with an explanation and their request preserved.
- [ ] The manual builder remains fully usable when the model is unavailable.
- [ ] 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.
