# Survey Builder

## Objective

Build a multi-question survey, send it out, and read all the answers in one place.

An authoring tool for multi-question surveys, a respondent flow, and a results view with export.

## 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. Ship a small, honest set of question types first: single choice, multiple choice, short text, long text, and a rating scale. That covers most real surveys. Defer matrix grids, file uploads, and calculated fields rather than half-building them.
2. Render the respondent flow with the app's existing Multi-Step Form and Conditional Form Fields, and reuse Form Field Shell for the inputs, so a survey looks and behaves like every other form in the product.
3. Give surveys the app's existing Draft and Publish States. Editing a live survey while responses are arriving is the main way survey data gets corrupted, so publishing must be a deliberate act with a clear boundary.
4. Autosave the author's work through the app's existing Autosave and warn on unsaved changes. Losing a twenty-question survey to a closed tab is the failure that makes people stop using the builder.
5. Do not build your own results export. Extend the app's existing CSV Export so survey results arrive in the same format, with the same permissions and the same delivery path as every other export.

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

- Editing a published survey changes the meaning of answers already collected. Version the survey on publish, store the version against each response, and read historical responses against the version they were answered under so old data stays interpretable.
- Skip logic makes it easy to author a branch that leads nowhere, or a loop that traps the respondent. Validate the branch graph before publishing and refuse to publish a survey where any path fails to reach the end.
- Anonymous and identified responses need different handling throughout, not a flag on a row. Decide per survey, tell respondents plainly which one applies, and make sure an anonymous survey does not quietly retain the identifiers its request carried.
- A public survey URL will be submitted by scripts. Cap responses per survey and per source, apply the app's existing Rate Limiting, and give the author a way to see and discard a suspicious burst without deleting genuine responses alongside it.
- Exporting one row per answer produces a file nobody can analyse. Export one row per response with one column per question, keeping the column order stable across exports so a saved spreadsheet formula does not silently point at the wrong question.
- A partially completed response is real data. Decide whether partials are saved and counted, say so in the results view, and do not let an abandoned response quietly disappear or quietly skew a completion rate.
- Long text answers are user-generated content shown to the survey author. Escape them on display and route them through the app's existing moderation so a response cannot inject markup into the results view.
- Deleting a question after responses exist must not delete the answers. Retire the question so its historical answers remain visible in results and exports, marked as no longer asked.

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

- [ ] An author builds, previews, publishes, and closes a survey with a defined draft and published boundary.
- [ ] Each response records the survey version it was answered under, and results remain interpretable after edits.
- [ ] Skip logic is validated before publish, with no branch that fails to reach the end.
- [ ] Anonymous surveys retain no respondent identifiers, and respondents are told which mode applies.
- [ ] Submissions are rate limited and capped, and suspicious bursts can be reviewed and discarded separately.
- [ ] Results export through the app's existing export path as one row per response with stable columns.
- [ ] Author work autosaves and warns before unsaved changes are lost.
- [ ] 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.
