{
  "openapi": "3.0.3",
  "info": {
    "title": "Chatva API",
    "version": "1.0.0",
    "description": "REST API чат-центра Чатва: диалоги, сообщения, контакты и вебхуки."
  },
  "servers": [{"url": "https://api.chatva.app/v1"}],
  "components": {
    "securitySchemes": {
      "bearerAuth": {"type": "http", "scheme": "bearer", "bearerFormat": "API key"}
    },
    "schemas": {
      "Conversation": {
        "type": "object",
        "properties": {
          "id": {"type": "string", "example": "cv_018f3a"},
          "client": {"type": "string", "example": "Кира Мещерякова"},
          "channel": {"type": "string", "enum": ["site", "telegram", "max", "whatsapp", "vk", "avito", "email"]},
          "assignee": {"type": "string", "nullable": true, "example": "m1"},
          "department": {"type": "string", "nullable": true, "example": "d1"},
          "unread": {"type": "integer", "example": 2},
          "rating": {"type": "integer", "nullable": true, "example": 5},
          "tags": {"type": "array", "items": {"type": "string"}},
          "createdAt": {"type": "string", "format": "date-time"},
          "updatedAt": {"type": "string", "format": "date-time"}
        }
      },
      "Message": {
        "type": "object",
        "properties": {
          "id": {"type": "string"},
          "from": {"type": "string", "enum": ["client", "agent", "bot", "note"]},
          "author": {"type": "string", "nullable": true},
          "text": {"type": "string"},
          "createdAt": {"type": "string", "format": "date-time"}
        }
      },
      "Contact": {
        "type": "object",
        "properties": {
          "id": {"type": "string"},
          "name": {"type": "string"},
          "phone": {"type": "string", "nullable": true},
          "email": {"type": "string", "nullable": true},
          "channel": {"type": "string"},
          "firstSeenAt": {"type": "string", "format": "date-time"}
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "object",
            "properties": {
              "code": {"type": "string", "example": "not_found"},
              "message": {"type": "string"}
            }
          }
        }
      }
    }
  },
  "security": [{"bearerAuth": []}],
  "paths": {
    "/conversations": {
      "get": {
        "summary": "Список диалогов",
        "parameters": [
          {"name": "status", "in": "query", "schema": {"type": "string", "enum": ["all", "new", "open", "closed"]}},
          {"name": "channel", "in": "query", "schema": {"type": "string"}},
          {"name": "assignee", "in": "query", "schema": {"type": "string"}},
          {"name": "limit", "in": "query", "schema": {"type": "integer", "default": 50}},
          {"name": "cursor", "in": "query", "schema": {"type": "string"}}
        ],
        "responses": {
          "200": {
            "description": "Список диалогов",
            "content": {"application/json": {"schema": {"type": "object", "properties": {
              "data": {"type": "array", "items": {"$ref": "#/components/schemas/Conversation"}},
              "nextCursor": {"type": "string", "nullable": true}
            }}}}
          },
          "401": {"description": "Неверный ключ", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}},
          "429": {"description": "Превышен лимит", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}}
        }
      }
    },
    "/conversations/{id}": {
      "get": {
        "summary": "Диалог со всеми сообщениями",
        "parameters": [{"name": "id", "in": "path", "required": true, "schema": {"type": "string"}}],
        "responses": {
          "200": {"description": "Диалог", "content": {"application/json": {"schema": {"type": "object", "properties": {
            "conversation": {"$ref": "#/components/schemas/Conversation"},
            "messages": {"type": "array", "items": {"$ref": "#/components/schemas/Message"}}
          }}}}},
          "404": {"description": "Не найден", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}}
        }
      }
    },
    "/messages": {
      "post": {
        "summary": "Отправить сообщение от оператора",
        "requestBody": {
          "required": true,
          "content": {"application/json": {"schema": {"type": "object", "required": ["conversationId", "text"], "properties": {
            "conversationId": {"type": "string"},
            "text": {"type": "string"},
            "internalNote": {"type": "boolean", "default": false}
          }}}}
        },
        "responses": {
          "201": {"description": "Создано", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Message"}}}},
          "404": {"description": "Диалог не найден", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}}
        }
      }
    },
    "/contacts": {
      "get": {
        "summary": "Контакты клиентов",
        "parameters": [
          {"name": "query", "in": "query", "schema": {"type": "string"}},
          {"name": "limit", "in": "query", "schema": {"type": "integer", "default": 50}}
        ],
        "responses": {
          "200": {"description": "Список контактов", "content": {"application/json": {"schema": {"type": "object", "properties": {
            "data": {"type": "array", "items": {"$ref": "#/components/schemas/Contact"}}
          }}}}}
        }
      }
    },
    "/webhooks": {
      "post": {
        "summary": "Подписка на события диалогов",
        "requestBody": {
          "required": true,
          "content": {"application/json": {"schema": {"type": "object", "required": ["url", "events"], "properties": {
            "url": {"type": "string", "format": "uri"},
            "events": {"type": "array", "items": {"type": "string", "enum": ["conversation.created", "message.created", "conversation.assigned", "conversation.rated"]}},
            "secret": {"type": "string"}
          }}}}
        },
        "responses": {
          "201": {"description": "Подписка создана", "content": {"application/json": {"schema": {"type": "object", "properties": {
            "id": {"type": "string"},
            "url": {"type": "string"}
          }}}}}
        }
      }
    }
  }
}
