{
  "openapi": "3.1.0",
  "info": {
    "title": "ZChat API Gateway",
    "version": "1.0.0",
    "description": "REST API and MCP Server for ZChat multi-account Zalo management and CRM automation.",
    "contact": {
      "name": "ZChat Support",
      "email": "support@zachat.vn",
      "url": "https://zachat.vn"
    }
  },
  "servers": [
    {
      "url": "https://api.zachat.vn",
      "description": "Production API Gateway"
    }
  ],
  "paths": {
    "/health": {
      "get": {
        "summary": "Health Check",
        "description": "Returns status of the ZChat gateway and Redis cluster.",
        "responses": {
          "200": {
            "description": "Service is healthy",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "service": { "type": "string" },
                    "status": { "type": "string" },
                    "uptime": { "type": "integer" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/auth/token": {
      "post": {
        "summary": "Issue Access Token",
        "description": "OAuth 2.0 Token endpoint for client credentials or refresh tokens.",
        "requestBody": {
          "required": true,
          "content": {
            "application/x-www-form-urlencoded": {
              "schema": {
                "type": "object",
                "properties": {
                  "grant_type": { "type": "string" },
                  "client_id": { "type": "string" },
                  "client_secret": { "type": "string" },
                  "scope": { "type": "string" }
                },
                "required": ["grant_type", "client_id", "client_secret"]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Token granted",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "access_token": { "type": "string" },
                    "token_type": { "type": "string" },
                    "expires_in": { "type": "integer" },
                    "scope": { "type": "string" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/zalo/sessions": {
      "get": {
        "summary": "List Connected Zalo Accounts",
        "description": "Returns list of active Zalo accounts connected to ZChat.",
        "security": [{ "bearerAuth": [] }],
        "responses": {
          "200": {
            "description": "List of active sessions"
          }
        }
      }
    },
    "/api/broadcast": {
      "post": {
        "summary": "Send Broadcast Messages with Anti-Spam Delays",
        "description": "Sends messages to a list of recipients with random spacing to protect accounts.",
        "security": [{ "bearerAuth": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "sessionIds": { "type": "array", "items": { "type": "string" } },
                  "recipients": { "type": "array", "items": { "type": "string" } },
                  "content": { "type": "string" },
                  "delayRangeSeconds": { "type": "array", "items": { "type": "integer" } }
                },
                "required": ["sessionIds", "recipients", "content"]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Campaign created successfully"
          }
        }
      }
    },
    "/mcp": {
      "post": {
        "summary": "Model Context Protocol Endpoint",
        "description": "Streamable HTTP MCP endpoint for AI Agents and tool execution.",
        "security": [{ "bearerAuth": [] }],
        "responses": {
          "200": {
            "description": "MCP response stream"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT"
      },
      "apiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "X-API-Key"
      }
    }
  }
}
