# NOMAZRA API Endpoints — Conversations & Chat
Base URL: `https://nomazra.kelthos.com/api/nomazra/`

All endpoints require `Authorization: Bearer <access_token>`

---

## GET /conversations/
List all user conversations (most recent first).

**Response 200:**
```json
[
  {
    "id": "uuid",
    "title": "Land ownership question",
    "country": "KH",
    "legal_area": "",
    "is_voice_session": false,
    "created_at": "2026-03-25T15:03:00Z",
    "updated_at": "2026-03-25T15:04:00Z"
  }
]
```

---

## POST /conversations/
Create a new conversation.

**Content-Type:** application/json

**Request Body:**
```json
{
  "title": "Land Law Question",
  "country": "KH"
}
```

**Response 201:**
```json
{
  "id": "uuid",
  "title": "Land Law Question",
  "country": "KH",
  "legal_area": "",
  "is_voice_session": false,
  "messages": [],
  "created_at": "2026-03-25T15:03:00Z",
  "updated_at": "2026-03-25T15:03:00Z"
}
```

---

## GET /conversations/{id}/
Get conversation details with messages.

**Response 200:** Full conversation object with nested messages array.

**Errors:**
- 404: `{"error": "Not found"}` (wrong ID or not owned by user)

---

## DELETE /conversations/{id}/
Delete a conversation and all its messages.

**Response 204:** No content.

---

## GET /conversations/{id}/messages/
List all messages in a conversation.

**Response 200:**
```json
[
  {
    "id": "uuid",
    "conversation": "conv-uuid",
    "role": "user",
    "content": "What are my land rights?",
    "citations": [],
    "input_type": "text",
    "tokens_used": 0,
    "processing_time_ms": 0,
    "created_at": "2026-03-25T15:03:49Z"
  },
  {
    "id": "uuid",
    "conversation": "conv-uuid",
    "role": "assistant",
    "content": "Under Cambodian Land Law 2001...",
    "citations": [],
    "input_type": "text",
    "tokens_used": 537,
    "processing_time_ms": 11904,
    "created_at": "2026-03-25T15:04:01Z"
  }
]
```

---

## POST /conversations/{id}/messages/
Send a message and get AI response. This is the MAIN CHAT endpoint.

**Content-Type:** application/json

**Request Body:**
```json
{
  "content": "What are my land ownership rights as a foreigner in Cambodia?",
  "input_type": "text"
}
```

`input_type` options: `"text"`, `"voice"`, `"call"`, `"document"`

**Response 201:**
```json
{
  "user_message": {
    "id": "uuid",
    "conversation": "conv-uuid",
    "role": "user",
    "content": "What are my land ownership rights...",
    "citations": [],
    "input_type": "text",
    "tokens_used": 0,
    "processing_time_ms": 0,
    "created_at": "..."
  },
  "ai_message": {
    "id": "uuid",
    "conversation": "conv-uuid",
    "role": "assistant",
    "content": "As a foreigner in Cambodia, your land ownership rights...",
    "citations": [],
    "input_type": "text",
    "tokens_used": 537,
    "processing_time_ms": 11904,
    "created_at": "..."
  }
}
```

**How it works:**
1. Saves user message to DB.
2. Loads last 20 messages as conversation history.
3. If a document is attached to the conversation, includes extracted text.
4. Calls GPT-4o-mini (cheapest model) with Cambodian law system prompt.
5. Saves AI response to DB with token count and processing time.
6. Updates user stats (query count, countries queried).
7. Auto-sets conversation title from first message.

**Errors:**
- 400: `{"error": "Content required"}` / `{"error": "Message too long (max 5000 chars)"}`
- 404: Conversation not found
