# Surch Assistant — Action API spec

The assistant (`src/agent.js`) never changes data directly. Every read and write goes through
`window.SurchAPI`, a set of async functions with a fixed request/response contract.
In this prototype they run against the in-browser data store; to go live, replace each
function body with a `fetch()` to the endpoint below. No other assistant code needs to change.

## Conventions

- Auth: session cookie or `Authorization: Bearer <token>`. Every endpoint is scoped to the caller's
  organisation server-side (`org_id` comes from the token, **never** from the request body).
- Permissions are enforced on the server. The frontend check is only for a friendly message.
- Every write takes an `Idempotency-Key` header (the assistant sends one per confirmed action) so a
  double-click or retry can't run twice.
- Response shape:

```json
{ "ok": true, "data": { } }
{ "ok": false, "code": "forbidden|not_found|conflict|validation|noop|server", "error": "Human-readable reason" }
```

- Batch endpoints return per-item results and the assistant reports partial success from them:

```json
{ "ok": false, "partial": true, "results": [ { "id": "c1", "ok": true }, { "id": "c15", "ok": false, "error": "Only the organiser can cancel it." } ] }
```

- Every successful write appends an audit-log row (see the last section).

## Roles

| Permission | Admin | Recruiter | Hiring manager | Viewer |
|---|---|---|---|---|
| calendar.write | ✓ | ✓ | ✓ | – |
| candidates.write | ✓ | ✓ | ✓ | – |
| jobs.write | ✓ | ✓ | – | – |
| company.write | ✓ | – | – | – |

## Endpoints

| SurchAPI function | Method + path | Permission | Server-side rules |
|---|---|---|---|
| `listEvents({from,to,types,pid,withName})` | `GET /api/events?from&to&type&candidate_id&q` | read | Only events the user organises or attends |
| `cancelEvents(ids)` | `POST /api/events/cancel` `{ids:[]}` | calendar.write | Per item: company-wide and deadline events can be cancelled only by their organiser; past events can't be cancelled. Notify attendees. |
| `rescheduleEvent(id,{date,start})` | `PATCH /api/events/:id` | calendar.write | Working hours 07:00–20:00, not in the past, no overlap with the organiser's calendar. A conflict returns `409` with `clash` and `suggest` (next free slot). |
| `scheduleInterview(pid,{date,start,mins,kind})` | `POST /api/interviews` | candidates.write + calendar.write | Candidate must belong to the org and not be archived. Same time rules as above. Sends the invite. |
| `listApplicants(jobId)` | `GET /api/jobs/:id/applicants` | read | Org-scoped |
| `moveCandidate(pid,stage)` | `POST /api/candidates/:id/stage` `{stage}` | candidates.write | Stage must be in the job's flow; a same-stage move returns `noop` |
| `rejectCandidate(pid,{email})` | `POST /api/candidates/:id/reject` `{send_email}` | candidates.write | Archives the candidate, cancels their future interviews, and queues the rejection email from the org's template. Returns the cancelled events. |
| `restoreCandidate(pid)` | `POST /api/candidates/:id/restore` | candidates.write | Only archived candidates |
| `createJob({title,status})` | `POST /api/jobs` | jobs.write | Title 3–80 chars; status `active` or `draft` |
| `setJobStatus(id,status)` | `PATCH /api/jobs/:id` `{status}` | jobs.write | `active` / `previous` (closed) / `draft` |
| `updateCompany(fields)` | `PATCH /api/company` | company.write | Field-level validation (website, email, phone, year, headcount). Returns `done[]` and `errors[]`, so partial saves are reported. |
| `activity(days)` | `GET /api/reports/activity?days=7` | read | Aggregates: new applicants, stage moves, hires, rejections, interviews ahead |

## Audit log

`POST` is implicit on every successful write. Exposed as `GET /api/audit?actor=assistant`.

```json
{ "at": "2026-10-16T09:12:03Z", "org_id": "…", "user_id": "…", "role": "Recruiter",
  "via": "assistant", "action": "event.cancel", "target": "Meeting with John Carter",
  "result": "ok", "request_id": "…" }
```

Action names in use: `event.cancel`, `event.reschedule`, `interview.schedule`, `candidate.move`,
`candidate.reject`, `candidate.restore`, `job.create`, `job.status`, `company.update`.
Ask the assistant "what have you done?" to see the session's log.
