{
  "openapi": "3.0.3",
  "info": {
    "title": "Тайный гость — Внешний API",
    "version": "1.6.9",
    "description": "Только HTTPS и сервер-сервер. Ключ доступа генерируется в админке. IDs проектов = company.id, точки = rest.id, код точки = rest.restid. ФИО гостя не экспортируется."
  },
  "servers": [
    {
      "url": "https://demo.asa1.kz",
      "description": "Замените на адрес вашей установки"
    }
  ],
  "security": [
    {
      "BearerKey": []
    }
  ],
  "paths": {
    "/api/external/projects.php": {
      "get": {
        "summary": "Список проектов с точками",
        "parameters": [
          {
            "$ref": "#/components/parameters/ProjectIds"
          },
          {
            "$ref": "#/components/parameters/LocationIds"
          }
        ],
        "responses": {
          "200": {
            "description": "Проекты с разрешёнными точками",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": {
                      "type": "boolean"
                    },
                    "total": {
                      "type": "integer"
                    },
                    "items": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Project"
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/AuthError"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        }
      }
    },
    "/api/external/visits.php": {
      "get": {
        "summary": "Список визитов по фильтрам с пагинацией",
        "parameters": [
          {
            "$ref": "#/components/parameters/ProjectIds"
          },
          {
            "$ref": "#/components/parameters/LocationIds"
          },
          {
            "name": "date_from",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "date"
            }
          },
          {
            "name": "date_to",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "date"
            }
          },
          {
            "name": "status",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "all",
                "draft",
                "final"
              ],
              "default": "all"
            }
          },
          {
            "name": "page",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "default": 1
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100,
              "default": 50
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Результаты и пагинация",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": {
                      "type": "boolean"
                    },
                    "items": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Visit"
                      }
                    },
                    "pagination": {
                      "type": "object",
                      "properties": {
                        "page": {
                          "type": "integer"
                        },
                        "limit": {
                          "type": "integer"
                        },
                        "total": {
                          "type": "integer"
                        },
                        "total_pages": {
                          "type": "integer"
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        }
      }
    },
    "/api/external/visit.php": {
      "get": {
        "summary": "Визит по ID с разделами, вопросами, ответами и комментариями",
        "parameters": [
          {
            "name": "id",
            "in": "query",
            "required": true,
            "schema": {
              "type": "integer",
              "minimum": 1
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Детальная анкета визита",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": {
                      "type": "boolean"
                    },
                    "visit": {
                      "$ref": "#/components/schemas/Visit"
                    },
                    "sections": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Section"
                      }
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Не найден или вне разрешённой области"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "BearerKey": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "opaque API key"
      }
    },
    "parameters": {
      "ProjectIds": {
        "name": "project_ids",
        "in": "query",
        "required": false,
        "description": "all или список company.id через запятую. All ограничен разрешениями ключа.",
        "schema": {
          "type": "string",
          "default": "all"
        }
      },
      "LocationIds": {
        "name": "location_ids",
        "in": "query",
        "required": false,
        "description": "all или список rest.id через запятую (не rest.restid)",
        "schema": {
          "type": "string",
          "default": "all"
        }
      }
    },
    "responses": {
      "AuthError": {
        "description": "Недействительный Bearer API key"
      },
      "Forbidden": {
        "description": "Доступ к методу или области запрещён"
      }
    },
    "schemas": {
      "Project": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "name": {
            "type": "string"
          },
          "locations": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Location"
            }
          }
        }
      },
      "Location": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "restid": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "address": {
            "type": "string"
          },
          "type": {
            "type": "string"
          },
          "is_active": {
            "type": "boolean"
          }
        }
      },
      "Visit": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "guest": {
            "type": "string",
            "example": "Тайный гость #25"
          },
          "user_id": {
            "type": "integer"
          },
          "project_id": {
            "type": "integer"
          },
          "location_id": {
            "type": "integer"
          },
          "location_code": {
            "type": "string"
          },
          "status": {
            "type": "string"
          },
          "started_at": {
            "type": "string"
          },
          "finished_at": {
            "type": "string",
            "nullable": true
          },
          "score_percent": {
            "type": "number",
            "nullable": true
          },
          "has_pdf": {
            "type": "boolean"
          },
          "report_pdf_url": {
            "type": "string",
            "nullable": true
          }
        }
      },
      "Section": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "title": {
            "type": "string"
          },
          "questions": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Question"
            }
          }
        }
      },
      "Question": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "question_text": {
            "type": "string"
          },
          "answer_type": {
            "type": "string"
          },
          "answer_value": {
            "type": "string",
            "nullable": true
          },
          "comment": {
            "type": "string",
            "nullable": true
          },
          "media": {
            "type": "array",
            "description": "Выдаётся только если ключ имеет разрешение на медиа",
            "items": {
              "type": "object"
            }
          }
        }
      }
    }
  }
}
