# Send-time optimization

Send-time optimization moves a message to the hour of the day a recipient
usually opens or clicks. It reads the real engagement history of an address,
never a model, and it only moves a send when the workspace turned the feature on
and the contact consented to it.

## What it uses

The histogram is built from the delivery events of the workspace: an open counts
once, a click counts twice, because a click says more about the moment than a
preview pane. Events are grouped into 24 hours of the day in the timezone of the
workspace (`Europe/Vienna` by default), over a lookback window of 180 days by
default.

Nothing else is read. There is no prediction model, no external data and no
profile bought from a third party: the hour is simply the hour that had the most
engagement so far.

## Consent comes first

Two conditions must both hold before a send is moved:

1. The workspace switch `enabled` is on.
2. The contact consented, which is stored as `sto_consent_at` on the contact.

A consent is recorded per contact and written to the consent ledger as well
(channel `other`, legal basis `consent`, action `granted` or `withdrawn`, source
`send_time_optimization`), so the proof appears next to every other consent of
that contact:

```
PUT https://werkmail.eu/api/practices/{id}/contacts/{contactID}/send-time-consent
{ "granted": true, "source": "dashboard" }
```

A withdrawal is the same call with `granted: false`. It clears the stored
consent and adds its own ledger row, so a later send is no longer moved.

## The fallback chain

For each contact the hour is decided in this order:

1. The contact histogram, when the contact consented and it holds at least
   `min_events` events (3 by default).
2. The workspace histogram, when it holds at least `min_events` events. This is
   an aggregate over all recipients, not personal data.
3. The configured default hour (9 by default).

The answer always names its source in `basis` (`contact`, `workspace` or
`default`) and the number of events in `samples`, so a client can tell a
personal pick from a general one.

## What does not happen without consent

Without consent nothing is moved and no personal history is read:

- A single send with `send_time_optimization: true` to a recipient without
  consent is refused with 422 and code `sto_not_consented`. It is never sent at
  another time silently.
- While the workspace switch is off, the same request is refused with 422 and
  code `sto_disabled`.
- A broadcast whose recipients have not consented is refused with 422 and code
  `sto_not_available`, and the campaign is not created at all.
- A request that carries its own `scheduled_at` keeps that time. The
  optimization never overwrites a send time the sender chose.

The recommendation endpoint still answers without a consent, so the interface
can show when a message *would* go out. That preview moves nothing.

## Using it

A single send:

```
POST https://werkmail.eu/api/practices/{id}/send
{ "to": ["patient@example.org"], "subject": "Ihr Termin", "text": "...",
  "send_time_optimization": true }
```

The answer is `queued` and carries `scheduled_at`, the next occurrence of the
chosen hour in the workspace timezone. The message then goes through the normal
scheduled path, so it can be inspected and cancelled like any other queued send.

A broadcast: `send_time_optimization: true` on
`POST /api/practices/{id}/broadcasts` picks the hour most of the consenting
recipients share, sets `scheduled_at` to its next window and reports the pick as
`sto_hour` and `sto_basis`. A draft is never scheduled by the flag, because a
draft is not sent yet.

The settings, the histogram and a single recommendation:

```
GET   /api/practices/{id}/send-time-optimization
PATCH /api/practices/{id}/send-time-optimization
GET   /api/practices/{id}/send-time-optimization/distribution
GET   /api/practices/{id}/contacts/{contactID}/send-time
```

`distribution` returns all 24 hours with opens, clicks and the score, plus how
many contacts consented and how many already have enough history for a personal
pick.

## Limits worth knowing

- Opens are noise. Apple Mail privacy protection and image proxies can fetch a
  tracking pixel at a time the reader never saw the message. A click is a
  stronger signal, which is why it counts twice, but neither is a measurement of
  attention.
- The window belongs to the workspace timezone, not to the recipient's own
  location. A contact who reads mail while travelling is read in the workspace
  zone.
- A contact needs `min_events` events in the lookback window before a personal
  hour is used. Below that the workspace hour applies, which is a general
  average and not a statement about that person.
- The pick is a fixed hour of the day, not a continuous stream of per-recipient
  send times. It spreads a broadcast only as far as the hours themselves differ.
- The consent is bound to the contact row of this workspace. Importing the same
  address somewhere else does not carry it over.

Send-time optimization is off by default. Turn it on in **Settings → Sending**
and record the consent per contact in the audience view.
