# Webhook Endpoint Management

## Objective

Let users choose where the app sends events, and which events go there.

Creating, editing, and testing outbound webhook endpoints, with destination validation and per-endpoint event subscriptions.

## 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. Let users register endpoints with a URL and a chosen set of event types. Default the subscription to nothing — an endpoint that silently receives every event is a data leak waiting to happen.
2. Require HTTPS and validate the destination before saving: resolve the host and refuse private, loopback, link-local, and metadata-service addresses.
3. Re-resolve and re-check the destination at delivery time, not only at save time. DNS can be repointed at an internal address after validation passes.
4. Offer a test delivery that sends a clearly-marked sample event and shows the raw response. Keep test deliveries out of production event history and out of any delivery-success metrics.
5. Signing secrets, retries and backoff, per-attempt delivery logs, replay, and auto-disabling repeatedly-failing endpoints are all owned by Webhook Delivery Log and Retry. Extend that feature rather than building a second delivery system here.
6. Show the current state of each endpoint — enabled, disabled by the user, or disabled by the system — and link through to its delivery history.

## 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 redirect can move a validated public URL to a private one. Do not follow redirects on delivery.
- A URL that resolves to different addresses on each lookup will pass validation once and fail safety later. Check every resolved address, not just the first.
- Changing an endpoint's subscribed events must not retroactively change what already-queued deliveries contain.
- Deleting an endpoint with deliveries in flight must stop future attempts without erasing the history someone may be debugging.
- Two endpoints pointing at the same URL are legitimate — different event sets, different environments. Warn, do not block.
- Endpoint management is a privileged action. Someone who can add a destination can exfiltrate every event in the workspace, so gate it by role and audit every change.

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

- [ ] Endpoints require HTTPS and are rejected when the destination resolves to a private or link-local address.
- [ ] Destination safety is re-checked at delivery time and redirects are not followed.
- [ ] Each endpoint subscribes to an explicit list of event types, empty by default.
- [ ] Test deliveries are visibly marked and excluded from production delivery history.
- [ ] The endpoint's enabled or disabled state, and who or what disabled it, is visible.
- [ ] Creating, editing, and deleting endpoints is role-gated and audited.
- [ ] 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.
