# SPECIFICATIONS/06_API/06_07_NOTIFICATIONS_ENDPOINTS.md

**PURPOSE:** Specifies the notification template management and text generation API surface.

**Important:** The system generates text ONLY — no actual message sending occurs.

---

## GET /api/v1/notifications/templates

**Permission:** `notifications.view`

**Inputs:** None

**Outputs:**
```json
{
    "success": true,
    "message": "string",
    "data": [
        {
            "id": 1,
            "type": "overdue|due_soon_1day|due_later|paid_receipt",
            "body_template": "string with {{variables}}",
            "created_at": "timestamp",
            "updated_at": "timestamp"
        }
    ]
}
```

---

## POST /api/v1/notifications/templates

**Permission:** `notifications.manage`

**Inputs:**

| Field | Type | Required | Validation |
|:---|:---|:---|:---|
| `type` | string | Yes | unique |
| `body_template` | text | Yes | — |

**Outputs:** Created template data

---

## PUT /api/v1/notifications/templates/{id}

**Permission:** `notifications.manage`

**Inputs:**

| Field | Type | Required | Validation |
|:---|:---|:---|:---|
| `body_template` | text | Yes | — |

**Outputs:** Updated template data

**Constraint:** Type identifier must not change.

---

## POST /api/v1/notifications/generate

**Permission:** `notifications.generate`

**Inputs:**

| Field | Type | Required | Validation |
|:---|:---|:---|:---|
| `client_id` | integer | Yes | exists in clients |
| `installment_id` | integer | Yes | exists in installments |

**Outputs:**
```json
{
    "success": true,
    "message": "string",
    "data": {
        "generated_text": "string with variables replaced"
    }
}
```

**Behavior:**
1. Fetches installment data and calculates days remaining (today vs due date)
2. Determines template type automatically:
   - `overdue` — if today > due_date
   - `due_soon_1day` — if due date is tomorrow
   - `due_later` — if due date is more than 1 day away
3. Fetches template by type
4. Replaces all variable placeholders with actual data: `{{customer_name}}`, `{{product_name}}`, `{{installment_amount}}`, `{{current_installment_number}}`, `{{total_installments}}`, `{{overdue_amount}}`
5. Returns final generated text (no sending occurs)