# AI Ticket Routing

## Objective

Send each support request to the queue that can actually resolve it.

Automatic assignment of an inbound support request to one of the currently configured queues, teams, or specialists, with a recorded reason and a fallback.

## 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. Read the destination list from the app's live configuration at the moment of routing, and constrain the decision to that set. A destination that is disabled, deleted, or out of hours is not a valid choice.
2. Pass the current ownership rules and destination descriptions in with each request rather than relying on anything the model may have absorbed earlier, so a change to team ownership takes effect on the next ticket.
3. Record the chosen destination, the confidence, and a one-line reason on the ticket, and give agents a reassign control that logs the correction alongside the original decision.
4. Define a single fallback queue and send anything below the confidence threshold, anything spanning several product areas without a clear primary, and anything the model failed to classify there.
5. Consume the summary, urgency, and problem area produced by AI Support Triage rather than re-deriving them. Triage owns what the ticket is about; this brief owns only where it goes.

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

- Routing to a destination that no longer exists or has been turned off strands the ticket where nobody is looking. Validate the choice against live configuration before the assignment is written.
- A ticket covering two product areas has no single right answer. Pick the area that blocks the customer, note the secondary area on the ticket, and send anything genuinely ambiguous to the fallback queue.
- Stale ownership is the common failure here. When a team is renamed, merged, or takes over an area, the next ticket must route correctly with no rebuild or retraining step.
- Ticket text is written by strangers and may contain instructions aimed at the routing system. Treat the customer's words strictly as data, never as direction, and confirm that a ticket asking to be sent to a named queue does not get sent there.
- Low confidence must produce the fallback queue and a visible flag, not a coin flip between two plausible teams.
- When the model is unavailable or times out, tickets must route by the app's existing deterministic rules or land in the fallback queue. Inbound support cannot stop because a provider is down.
- Rerouting a ticket must not restart its response clock or lose the conversation history already attached to it.
- Repeated retries on a failing ticket must be bounded, and the ticket parked in the fallback queue rather than retried indefinitely at cost.

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

- [ ] Destinations are read from live configuration and only active ones can be chosen.
- [ ] Ownership changes take effect on the next ticket with no rebuild step.
- [ ] Every routed ticket carries its destination, confidence, and a one-line reason.
- [ ] Low confidence, multi-area, and failed classifications all land in a defined fallback queue.
- [ ] Instructions embedded in customer text cannot change the routing decision.
- [ ] Provider outage falls back to deterministic rules and never blocks intake.
- [ ] Agent reassignments are logged against the original decision.
- [ ] 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.
