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40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# API Reference

Base URL (production): `https://3scerror404-nexus.hf.space`  
Base URL (local dev): `http://localhost:8000`  
Interactive docs: `{base}/docs` (Swagger UI), `{base}/redoc`

All endpoints require `Authorization: Bearer <access_token>` unless marked **public**.

---

## Authentication (`/api/v1/auth`)

### `POST /api/v1/auth/register/`
**Public.** Create a new user account. If the workspace does not exist, it is provisioned automatically along with a local admin account.

**Body**
```json
{
  "email": "user@example.com",
  "password": "string",
  "full_name": "string",
  "workspace_name": "Acme Corp"
}
```

**Response `201`**
```json
{
  "id": "uuid",
  "email": "user@example.com",
  "full_name": "string",
  "workspace_id": "uuid",
  "role": "CUSTOMER_USER"
}
```

---

### `POST /api/v1/auth/login/`
**Public.** Exchange credentials for JWT token pair.

**Body** `application/x-www-form-urlencoded`
```
username=user@example.com&password=secret
```

**Response `200`**
```json
{
  "access_token": "eyJ...",
  "refresh_token": "eyJ...",
  "token_type": "bearer"
}
```

---

### `POST /api/v1/auth/refresh/`
Exchange a valid refresh token for a new access token.

**Body**
```json
{ "refresh_token": "eyJ..." }
```

**Response `200`** β€” same shape as `/login/`.

---

### `POST /api/v1/auth/logout/`
Revoke the current access token (adds JTI to Redis blocklist).

**Response `204`** No content.

---

### `POST /api/v1/auth/forgot-password/`
**Public.** Send a password-reset email via Resend.

**Body** `{ "email": "user@example.com" }`

**Response `200`** `{ "message": "Reset email sent" }`

---

### `POST /api/v1/auth/reset-password/`
**Public.** Complete password reset with the token from email.

**Body** `{ "token": "string", "new_password": "string" }`

**Response `200`** `{ "message": "Password updated" }`

---

## Tickets (`/api/v1/tickets`)

### `GET /api/v1/tickets/`
List tickets visible to the current user in their workspace.

**Query params**
| Param | Type | Description |
|---|---|---|
| `status` | string | Filter by status enum value |
| `priority` | string | Filter by priority (P0–P3) |
| `category` | string | Filter by category |
| `assigned_to` | uuid | Filter by assignee |
| `search` | string | Full-text keyword search |

**Response `200`** β€” array of ticket objects.

---

### `GET /api/v1/tickets/paginated/`
Paginated ticket list with the same filters as above.

**Query params** β€” all filters above, plus:
| Param | Type | Default |
|---|---|---|
| `page` | int | 1 |
| `page_size` | int | 20 |
| `sort_by` | string | `created_at` |
| `sort_dir` | string | `desc` |

**Response `200`**
```json
{
  "items": [...],
  "total": 142,
  "page": 1,
  "page_size": 20,
  "pages": 8
}
```

---

### `GET /api/v1/tickets/search/`
Hybrid semantic + keyword search (pgvector + TSVECTOR).

**Query params** `q` (required) β€” natural-language query string.

**Response `200`** β€” ranked array of ticket objects with a `score` field.

---

### `POST /api/v1/tickets/`
Create a new ticket. Triggers background AI summarization and embedding jobs.

**Body**
```json
{
  "title": "string",
  "description": "string",
  "priority": "P1",
  "category": "string",
  "project_id": "uuid"
}
```

**Response `201`** β€” full ticket object.

---

### `GET /api/v1/tickets/{ticket_id}/`
Fetch a single ticket. Field set is role-sanitized (customers omit internal notes).

---

### `PUT /api/v1/tickets/{ticket_id}/`
Update ticket fields. Partial update β€” only supplied fields are changed.

**Body** β€” any subset of ticket fields.

---

### `PUT /api/v1/tickets/{ticket_id}/status/`
Transition ticket status. Enforces valid state machine transitions.

**Body** `{ "status": "IN_PROGRESS", "note": "optional string" }`

Valid transitions:
```
OPEN β†’ ACKNOWLEDGED β†’ IN_PROGRESS β†’ RESOLVED β†’ CLOSED
       ↑_____________________|
```

---

### `PUT /api/v1/tickets/{ticket_id}/assign/`
Assign ticket to a staff user in the same workspace.

**Body** `{ "user_id": "uuid" }`

---

### `GET /api/v1/tickets/statuses/`
Return all valid status enum values.

### `GET /api/v1/tickets/priorities/`
Return all valid priority enum values (P0–P3) with SLA targets.

---

## Messages (`/api/v1/tickets/{ticket_id}/messages`)

### `GET /api/v1/tickets/{ticket_id}/messages/`
Fetch the message thread for a ticket. Internal messages are filtered out for CUSTOMER_* roles.

**Response `200`** β€” array of message objects.

---

### `POST /api/v1/tickets/{ticket_id}/messages/`
Add a message to a ticket thread. Triggers mention notification jobs.

**Body**
```json
{
  "content": "string",
  "is_internal": false,
  "mentions": ["uuid"]
}
```

**Response `201`** β€” message object.

---

## Knowledge Base (`/api/v1/kb`)

### `POST /api/v1/kb/`
Create a KB article. Triggers background chunking + embedding indexing.

**Body**
```json
{
  "title": "string",
  "content": "string (markdown)",
  "project_id": "uuid",
  "tags": ["string"]
}
```

---

### `GET /api/v1/kb/`
List KB articles for the current workspace. Supports `?project_id=`, `?tag=`, `?page=`.

---

### `GET /api/v1/kb/{article_id}/`
Fetch a single article including version history.

---

### `PUT /api/v1/kb/{article_id}/`
Update an article. Creates a new version snapshot before applying changes.

---

### `DELETE /api/v1/kb/{article_id}/`
Soft-delete an article (sets `deleted_at`). Removes its chunks from the vector index.

---

### `GET /api/v1/kb/search/`
Semantic + keyword search across KB chunks.

**Query params** `q` (required), `project_id` (optional), `limit` (default 10).

**Response `200`** β€” array of `{ article, chunk, score }` objects.

---

### `POST /api/v1/kb/bulk-sync/`
Batch import articles from external sources. Deduplicates by `content_hash`.

**Body** β€” array of article objects.

**Response `202`** β€” `{ "queued": N }` β€” each article is indexed asynchronously.

---

## Nexus Master Agent (`/api/v1/nexus`)

### `POST /api/v1/nexus/query/`
Submit a natural-language query to the Nexus agent. The agent resolves intent, routes to the appropriate sub-node (KB search, ticket ops, analytics, remediation), and returns a structured response.

**Body**
```json
{
  "message": "What are the top recurring issues this month?",
  "thread_id": "uuid (optional β€” omit to start new thread)"
}
```

**Response `200`**
```json
{
  "answer": "string",
  "sources": [{ "type": "ticket|kb", "id": "uuid", "title": "string" }],
  "thread_id": "uuid",
  "tokens_used": 420
}
```

Response fields are RBAC-masked β€” customers receive a narrower answer than staff.

---

### `POST /api/v1/nexus/stream/`
Streaming variant of `/query/`. Returns `text/event-stream` (SSE). Each chunk is a delta string; the final chunk contains `sources` and `thread_id` metadata.

**Body** β€” same as `/query/`.

---

### `GET /api/v1/nexus/threads/{thread_id}/history/`
Return the full conversation history for a Nexus thread.

**Response `200`** β€” array of `{ role, content, created_at }` objects.

---

## Dashboard (`/api/v1/dashboard`)

### `GET /api/v1/dashboard/telemetry/`
Workspace ticket counts broken down by status, priority, and category for the current period.

**Response `200`**
```json
{
  "by_status": { "OPEN": 14, "IN_PROGRESS": 6, ... },
  "by_priority": { "P0": 2, "P1": 8, ... },
  "by_category": { "Billing": 9, ... },
  "sla_breached": 3,
  "avg_resolution_hours": 4.2
}
```

---

### `GET /api/v1/dashboard/insights/`
AI-generated narrative insights for the workspace (generated by the heavy worker every 6 hours).

**Response `200`** `{ "insights": "string (markdown)" }`

---

### `GET /api/v1/dashboard/trends/`
Historical ticket volume and resolution time over the past 30 days.

**Query params** `?days=30` (default), `?granularity=day|week`.

---

## Reports (`/api/v1/reports`)

### `GET /api/v1/reports/sla-audit/`
All SLA breaches in the workspace for a given period.

**Query params** `?from=ISO8601&to=ISO8601&priority=P0`

---

### `GET /api/v1/reports/personnel/`
Per-agent metrics: tickets handled, avg resolution time, customer satisfaction score.

**Roles required**: SC_LEAD, SC_ADMIN, SUPERADMIN.

---

### `GET /api/v1/reports/sentiment/`
Sentiment distribution across ticket messages (positive / neutral / negative).

---

## Notifications (`/api/v1/notifications`)

### `GET /api/v1/notifications/`
List unread notifications for the current user.

### `POST /api/v1/notifications/{id}/read/`
Mark a notification as read.

### `POST /api/v1/notifications/read-all/`
Mark all notifications as read.

---

## System & admin

### `GET /api/v1/system/health/`
**Public.** Infrastructure health check.

**Response `200`**
```json
{
  "status": "ok",
  "db": "connected",
  "redis": "connected",
  "arq": "connected",
  "version": "string"
}
```

---

### `GET /api/v1/worker/jobs/`
List active ARQ job records. Requires SC_ADMIN or higher.

---

## WebSockets (`/ws`)

### `WS /ws/{workspace_id}?token=<access_token>`
Subscribe to workspace-level real-time events. The token is validated on connection; invalid or revoked tokens close the socket with code 4001.

**Events received** (JSON)
```json
{ "type": "ticket.status_changed", "payload": { "ticket_id": "uuid", "new_status": "..." } }
{ "type": "ticket.created",        "payload": { ... } }
{ "type": "sla.breach",            "payload": { "ticket_id": "uuid", "priority": "P0" } }
{ "type": "insight.ready",         "payload": { "summary": "..." } }
```

---

### `WS /ws/tickets/{ticket_id}?token=<access_token>`
Subscribe to a single ticket's thread. Fires on new messages and status changes.

**Events received**
```json
{ "type": "message.created", "payload": { "message_id": "uuid", "content": "..." } }
{ "type": "ticket.assigned",  "payload": { "assignee_id": "uuid" } }
```

---

## Error responses

All errors follow the RFC 7807 problem format:

```json
{
  "detail": "human-readable message"
}
```

| HTTP code | Meaning |
|---|---|
| `400` | Validation error or invalid state transition |
| `401` | Missing or expired token |
| `403` | Role does not have permission |
| `404` | Resource not found (workspace-scoped) |
| `409` | Conflict (e.g. duplicate content hash) |
| `422` | Pydantic schema validation failure |
| `429` | Rate limit exceeded |
| `500` | Unhandled server error (check logs) |