{
  "openapi": "3.0.3",
  "info": {
    "title": "App Control Demo API",
    "version": "1.0.0",
    "description": "Start, stop, restart and check the status of a (simulated) application. Designed to be called from a WhatsApp bot: every response carries a WhatsApp-formatted message or reply string."
  },
  "components": {
    "securitySchemes": {
      "ApiKey": { "type": "apiKey", "in": "header", "name": "X-API-Key" }
    },
    "schemas": {
      "Status": {
        "type": "object",
        "properties": {
          "app": { "type": "string", "example": "Orders Service" },
          "version": { "type": "string", "example": "2.4.1" },
          "status": { "type": "string", "enum": ["running", "stopped", "starting", "stopping", "down"] },
          "healthy": { "type": "boolean" },
          "since": { "type": "string", "format": "date-time" },
          "uptimeSeconds": { "type": "integer" },
          "restarts": { "type": "integer" },
          "lastError": { "type": "string", "nullable": true },
          "metrics": {
            "type": "object",
            "properties": {
              "cpuPercent": { "type": "integer" },
              "memoryMb": { "type": "integer" },
              "requestsPerMin": { "type": "integer" }
            }
          },
          "message": { "type": "string", "description": "WhatsApp-formatted summary, ready to send", "example": "🟢 *Orders Service* is *RUNNING*\nUptime: 1h 4m 2s" }
        }
      },
      "ActionResult": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean" },
          "changed": { "type": "boolean", "description": "false when the app was already in the requested state" },
          "accepted": { "type": "boolean", "description": "true when a transition started (HTTP 202)" },
          "message": { "type": "string", "description": "WhatsApp-formatted reply" },
          "status": { "$ref": "#/components/schemas/Status" }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean", "example": false },
          "error": { "type": "string", "example": "unauthorized" },
          "message": { "type": "string" }
        }
      }
    },
    "responses": {
      "Accepted": { "description": "Transition started", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ActionResult" } } } },
      "NoChange": { "description": "Already in the requested state", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ActionResult" } } } },
      "Unauthorized": { "description": "Missing or invalid API key", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } },
      "Busy": { "description": "App is mid-transition, retry in a few seconds", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ActionResult" } } } }
    }
  },
  "paths": {
    "/api/status": {
      "get": {
        "summary": "Get application status",
        "responses": { "200": { "description": "Current status", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Status" } } } } }
      }
    },
    "/api/start": {
      "post": {
        "summary": "Start the application",
        "security": [{ "ApiKey": [] }],
        "responses": { "202": { "$ref": "#/components/responses/Accepted" }, "200": { "$ref": "#/components/responses/NoChange" }, "401": { "$ref": "#/components/responses/Unauthorized" }, "409": { "$ref": "#/components/responses/Busy" } }
      }
    },
    "/api/stop": {
      "post": {
        "summary": "Stop the application",
        "security": [{ "ApiKey": [] }],
        "responses": { "202": { "$ref": "#/components/responses/Accepted" }, "200": { "$ref": "#/components/responses/NoChange" }, "401": { "$ref": "#/components/responses/Unauthorized" }, "409": { "$ref": "#/components/responses/Busy" } }
      }
    },
    "/api/restart": {
      "post": {
        "summary": "Restart the application",
        "security": [{ "ApiKey": [] }],
        "responses": { "202": { "$ref": "#/components/responses/Accepted" }, "401": { "$ref": "#/components/responses/Unauthorized" }, "409": { "$ref": "#/components/responses/Busy" } }
      }
    },
    "/api/command": {
      "post": {
        "summary": "Run a chat command (status, start, stop, restart, help) and get a reply to send back",
        "security": [{ "ApiKey": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["text"],
                "properties": {
                  "text": { "type": "string", "example": "status" },
                  "from": { "type": "string", "description": "Sender's phone number, used for the event log (last 4 digits only)", "example": "+923001234567" }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Reply for the user",
            "content": { "application/json": { "schema": { "type": "object", "properties": { "ok": { "type": "boolean" }, "command": { "type": "string" }, "reply": { "type": "string" }, "status": { "$ref": "#/components/schemas/Status" } } } } }
          },
          "400": { "description": "Body is not {\"text\": \"...\"}", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "409": { "$ref": "#/components/responses/Busy" }
        }
      }
    },
    "/api/simulate/crash": {
      "post": {
        "summary": "Demo helper: put the application in the DOWN state",
        "security": [{ "ApiKey": [] }],
        "responses": { "200": { "description": "App is now down", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ActionResult" } } } }, "401": { "$ref": "#/components/responses/Unauthorized" } }
      }
    },
    "/api/events": {
      "get": {
        "summary": "Recent state changes",
        "parameters": [{ "name": "limit", "in": "query", "schema": { "type": "integer", "default": 20, "maximum": 100 } }],
        "responses": { "200": { "description": "Event list" } }
      }
    },
    "/health": {
      "get": { "summary": "Liveness probe for the platform (always 200)", "responses": { "200": { "description": "OK" } } }
    }
  }
}
