{
  "openapi": "3.0.3",
  "info": {
    "title": "SaM oTPravil MX Router API",
    "version": "1.1.0",
    "description": "Приём почты на MX SaM (Mailu), поиск писем по IMAP, webhook, проверка DNS MX и алиас домена к mlgnr.com.\n\nХост: `https://mx.samotpravil.ru`.\nAuth: заголовок `Authorization: <api-key>` (ключ выдаётся по заявке).\n\nКлиентский MX (allow-лист): `mail.mlgnr.com` или `mx.mail.selcloud.ru`.\n\nМаркетинг: `/tools/mx-router/`. Заявка на ключ — на этой витрине."
  },
  "servers": [
    {
      "url": "https://mx.samotpravil.ru",
      "description": "Production"
    }
  ],
  "tags": [
    { "name": "system", "description": "Health" },
    { "name": "domains", "description": "Домены и MX" },
    { "name": "emails", "description": "Письма" }
  ],
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "Authorization",
        "description": "Сырой API-ключ (без Bearer) или `Bearer <key>`."
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "status": { "type": "string", "example": "error" },
          "message": { "type": "string" }
        }
      },
      "DomainRow": {
        "type": "object",
        "properties": {
          "domain_name": { "type": "string" },
          "verified": { "type": "boolean" }
        }
      }
    }
  },
  "paths": {
    "/health": {
      "get": {
        "tags": ["system"],
        "summary": "Liveness",
        "operationId": "health",
        "security": [],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean" },
                    "service": { "type": "string" },
                    "version": { "type": "string" },
                    "public_host": { "type": "string" },
                    "mailu_ready": { "type": "boolean" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/v2/mx/health": {
      "get": {
        "tags": ["system"],
        "summary": "Liveness (prefixed)",
        "operationId": "healthPrefixed",
        "security": [],
        "responses": {
          "200": { "description": "OK" }
        }
      }
    },
    "/api/v2/mx/domains": {
      "get": {
        "tags": ["domains"],
        "summary": "Список доменов ключа",
        "operationId": "domainsList",
        "security": [{ "ApiKeyAuth": [] }],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": { "type": "boolean" },
                    "domains": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/DomainRow" }
                    }
                  }
                }
              }
            }
          },
          "401": { "description": "unauthorized", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } }
        }
      }
    },
    "/api/v2/mx/domains/add": {
      "post": {
        "tags": ["domains"],
        "summary": "Проверка MX и provision отдельного домена+ящика в Mailu",
        "operationId": "domainsAdd",
        "security": [{ "ApiKeyAuth": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["domain"],
                "properties": {
                  "domain": { "type": "string", "example": "example.com" }
                }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "Успех или MX not found (success:false)" },
          "400": { "description": "bad request / domain unavailable" },
          "401": { "description": "unauthorized" }
        }
      }
    },
    "/api/v2/mx/domains/dns-check": {
      "post": {
        "tags": ["domains"],
        "summary": "Проверка MX в DNS (без записи в Mailu)",
        "description": "Положительный ответ, если среди MX есть `mail.mlgnr.com` или `mx.mail.selcloud.ru`.",
        "operationId": "domainsDnsCheck",
        "security": [{ "ApiKeyAuth": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["domain"],
                "properties": {
                  "domain": {
                    "type": "string",
                    "example": "e.domain.ru",
                    "description": "Домен, у которого проверяем MX"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "MX found / not found",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": { "type": "boolean" },
                    "domain_name": { "type": "string" },
                    "expected_mx_hosts": {
                      "type": "array",
                      "items": { "type": "string" },
                      "example": ["mail.mlgnr.com", "mx.mail.selcloud.ru"]
                    },
                    "message": { "type": "string" }
                  }
                }
              }
            }
          },
          "400": { "description": "bad request / domain unavailable" },
          "401": { "description": "unauthorized" }
        }
      }
    },
    "/api/v2/mx/domains/alias": {
      "post": {
        "tags": ["domains"],
        "summary": "Добавить домен алиасом к mlgnr.com",
        "description": "Сначала та же проверка MX, что у dns-check. Без нужного MX — `success: false`, Mailu не меняется. С MX — Mailu Alternative к primary `mlgnr.com` (идемпотентно).",
        "operationId": "domainsAlias",
        "security": [{ "ApiKeyAuth": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["domain"],
                "properties": {
                  "domain": {
                    "type": "string",
                    "example": "e.domain.ru"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Алиас добавлен или MX not found (success:false)",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": { "type": "boolean" },
                    "domain_name": { "type": "string" },
                    "alias_of": {
                      "type": "string",
                      "example": "mlgnr.com",
                      "description": "Только при success:true"
                    },
                    "message": { "type": "string" }
                  }
                }
              }
            }
          },
          "400": { "description": "bad request / domain unavailable" },
          "401": { "description": "unauthorized" },
          "502": { "description": "ошибка Mailu Admin" },
          "503": { "description": "Mailu не настроен" }
        }
      }
    },
    "/api/v2/mx/domains/check": {
      "get": {
        "tags": ["domains"],
        "summary": "Перепроверка MX домена",
        "operationId": "domainsCheck",
        "security": [{ "ApiKeyAuth": [] }],
        "parameters": [
          {
            "name": "domain",
            "in": "query",
            "required": true,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": { "description": "MX found / not found" },
          "404": { "description": "domain not found for key" }
        }
      }
    },
    "/api/v2/mx/domains/webhook": {
      "post": {
        "tags": ["domains"],
        "summary": "Задать webhook_url и signing key",
        "operationId": "domainsWebhook",
        "security": [{ "ApiKeyAuth": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["domain"],
                "properties": {
                  "domain": { "type": "string" },
                  "webhook_url": { "type": "string", "format": "uri", "description": "Только публичный HTTPS" },
                  "webhook_signing_key": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "OK" },
          "400": { "description": "unsafe webhook_url (SSRF pin)" },
          "404": { "description": "domain not found" }
        }
      }
    },
    "/api/v2/mx/emails": {
      "get": {
        "tags": ["emails"],
        "summary": "Поиск писем",
        "operationId": "emailsFind",
        "security": [{ "ApiKeyAuth": [] }],
        "parameters": [
          { "name": "to", "in": "query", "schema": { "type": "string" } },
          { "name": "from", "in": "query", "schema": { "type": "string" } },
          { "name": "subject", "in": "query", "schema": { "type": "string" } },
          { "name": "message_id", "in": "query", "schema": { "type": "string" } },
          { "name": "reply_to", "in": "query", "schema": { "type": "string" } },
          { "name": "return-path", "in": "query", "schema": { "type": "string" } }
        ],
        "responses": {
          "200": { "description": "Список карточек писем" },
          "400": { "description": "нужен хотя бы один фильтр" }
        }
      }
    },
    "/api/v2/mx/emails/last": {
      "get": {
        "tags": ["emails"],
        "summary": "Самое раннее UNSEEN",
        "operationId": "emailsLast",
        "security": [{ "ApiKeyAuth": [] }],
        "responses": {
          "200": { "description": "Письмо или пусто" }
        }
      }
    },
    "/api/v2/mx/emails/{messageId}": {
      "get": {
        "tags": ["emails"],
        "summary": "Полное письмо по Message-ID",
        "operationId": "emailsGet",
        "security": [{ "ApiKeyAuth": [] }],
        "parameters": [
          {
            "name": "messageId",
            "in": "path",
            "required": true,
            "schema": { "type": "string" },
            "description": "URL-encoded Message-ID (в т.ч. с @)"
          }
        ],
        "responses": {
          "200": { "description": "Полное письмо" },
          "404": { "description": "not found" }
        }
      }
    },
    "/api/v2/mx/emails/{messageId}/mark_read": {
      "post": {
        "tags": ["emails"],
        "summary": "Пометить письмо \\Seen",
        "operationId": "emailsMarkRead",
        "security": [{ "ApiKeyAuth": [] }],
        "parameters": [
          {
            "name": "messageId",
            "in": "path",
            "required": true,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": { "description": "OK" },
          "404": { "description": "not found" }
        }
      }
    }
  }
}
