# Automation

## Список проектів автоматизації

`GET /automation/projects`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "Список проектів автоматизації",
  "description": "Кожен проект, збережений у панелі запуску, з його блоками та налаштуваннями запуску. Це ті самі проекти, які редагує розділ **Automation**.",
  "responses": {
    "200": {
      "description": "Список проектів.",
      "content": {
        "application/json": {
          "schema": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/AutomationProject"
            }
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    }
  }
}
```

## Створити проект

`POST /automation/projects`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "Створити проект",
  "requestBody": {
    "required": false,
    "content": {
      "application/json": {
        "schema": {
          "type": "object",
          "properties": {
            "name": {
              "type": "string",
              "description": "Використовує `Untitled`, коли порожній."
            }
          }
        }
      }
    }
  },
  "responses": {
    "200": {
      "description": "Створений проект.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/AutomationProject"
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    }
  }
}
```

## Отримати проект

`GET /automation/projects/{id}`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "Отримати проект",
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string",
        "format": "uuid"
      }
    }
  ],
  "responses": {
    "200": {
      "description": "Деталі проекту.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/AutomationProject"
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "404": {
      "description": "Проект не знайдено.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## Замінити проект

`PUT /automation/projects/{id}`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "Замінити проект",
  "description": "Повністю замінює проєкт. Надішліть об’єкт у форматі відповіді `GET`. Ідентифікатор у шляху має пріоритет над ідентифікатором у тілі запиту. Якщо проєкт не існує, повертається `404`; для створення використовуйте `POST /automation/projects`.",
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string",
        "format": "uuid"
      }
    }
  ],
  "requestBody": {
    "required": true,
    "content": {
      "application/json": {
        "schema": {
          "$ref": "#/components/schemas/AutomationProject"
        }
      }
    }
  },
  "responses": {
    "200": {
      "description": "Збережений проект.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/AutomationProject"
          }
        }
      }
    },
    "400": {
      "description": "Недійсний об'єкт проекту.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "404": {
      "description": "Проект не знайдено.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## Видалити проект

`DELETE /automation/projects/{id}`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "Видалити проект",
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string",
        "format": "uuid"
      }
    }
  ],
  "responses": {
    "200": {
      "description": "Видалено.",
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "properties": {
              "deleted": {
                "type": "boolean",
                "example": true
              },
              "id": {
                "type": "string",
                "format": "uuid"
              }
            }
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "404": {
      "description": "Проект не знайдено.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## Дубльований проект

`POST /automation/projects/{id}/duplicate`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "Дубльований проект",
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string",
        "format": "uuid"
      }
    }
  ],
  "responses": {
    "200": {
      "description": "Дубльований проект.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/AutomationProject"
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "404": {
      "description": "Проект не знайдено.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## Експорт пакета проектів

`GET /automation/projects/{id}/export`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "Експорт пакета проектів",
  "description": "Параметри проекту, позначені секретними, спорожняються на виході, тому пакет можна передати, не беручи з собою пароль. Сторона імпорту просить їх знову.",
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string",
        "format": "uuid"
      }
    }
  ],
  "responses": {
    "200": {
      "description": "Експортний комплект.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/AutomationBundle"
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "404": {
      "description": "Проект не знайдено.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## Імпорт пакета проектів

`POST /automation/import`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "Імпорт пакета проектів",
  "description": "Пакет надходить як новий проект із новим ідентифікатором.",
  "requestBody": {
    "required": true,
    "content": {
      "application/json": {
        "schema": {
          "$ref": "#/components/schemas/AutomationBundle"
        }
      }
    }
  },
  "responses": {
    "200": {
      "description": "Імпортований проект.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/AutomationProject"
          }
        }
      }
    },
    "400": {
      "description": "Недійсний пакет або непідтримувана версія формату.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    }
  }
}
```

## Розпочати проект

`POST /automation/projects/{id}/run`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "Розпочати проект",
  "description": "Запускає проєкт і одразу повертає відповідь. Потрібні браузери запускаються блоками самого проєкту. Використовуйте `/status` для відстеження прогресу.\n\nЯкщо проєкт уже запущено або в ньому немає активних кроків, повертається `400`.",
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string",
        "format": "uuid"
      }
    }
  ],
  "responses": {
    "200": {
      "description": "Проект розпочато.",
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "properties": {
              "project_id": {
                "type": "string",
                "format": "uuid"
              },
              "running": {
                "type": "boolean",
                "example": true
              }
            }
          }
        }
      }
    },
    "400": {
      "description": "Проект уже запущено або не має ввімкнених кроків.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "404": {
      "description": "Проект не знайдено.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "501": {
      "description": "Ця збірка не підтримує автоматизацію.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## Зупинити проект

`POST /automation/projects/{id}/stop`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "Зупинити проект",
  "description": "Запитує зупинку запуску. Кожен браузер завершує поточний крок і закривається, тому запуск не зникає одразу після відповіді. Стежте за полем `running` через `/status`.",
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string",
        "format": "uuid"
      }
    }
  ],
  "responses": {
    "200": {
      "description": "Запит на зупинку прийнято.",
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "properties": {
              "project_id": {
                "type": "string",
                "format": "uuid"
              },
              "stopping": {
                "type": "boolean",
                "example": true
              }
            }
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "501": {
      "description": "Ця збірка не підтримує автоматизацію.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## Отримати статус виконання проекту

`GET /automation/projects/{id}/status`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "Отримати статус виконання проекту",
  "description": "Повертає `null`, якщо проєкт ще не запускався після запуску лаунчера. `log` містить останні записи, показані у вікні запусків. Повний журнал зберігається в `automation-runs/`.",
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string",
        "format": "uuid"
      }
    }
  ],
  "responses": {
    "200": {
      "description": "Статус виконання або `null`.",
      "content": {
        "application/json": {
          "schema": {
            "oneOf": [
              {
                "$ref": "#/components/schemas/AutomationRun"
              },
              {
                "type": "null"
              }
            ]
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "501": {
      "description": "Ця збірка не підтримує автоматизацію.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## Список активних прогонів

`GET /automation/runs`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "Список активних прогонів",
  "responses": {
    "200": {
      "description": "Список активних запусків.",
      "content": {
        "application/json": {
          "schema": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/AutomationRun"
            }
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "501": {
      "description": "Ця збірка не підтримує автоматизацію.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## Список встановлених модулів

`GET /automation/modules`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "Список встановлених модулів",
  "description": "Повертає встановлені оператором модулі WebAssembly. Кожен модуль додає блоки з типом `module:<module id>:<block>`. Модулі повертають дії для виконавця і не звертаються до сторінки напряму.",
  "responses": {
    "200": {
      "description": "Список модулів.",
      "content": {
        "application/json": {
          "schema": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/AutomationModule"
            }
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "501": {
      "description": "Ця збірка не підтримує автоматизацію.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## Встановити модуль

`POST /automation/modules`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "Встановити модуль",
  "description": "`path` має вказувати на локальний файл `.wasm` на машині, де запущено лаунчер. Модуль містить нативний код, зібраний оператором.",
  "requestBody": {
    "required": true,
    "content": {
      "application/json": {
        "schema": {
          "type": "object",
          "required": [
            "path"
          ],
          "properties": {
            "path": {
              "type": "string",
              "example": "/home/me/module.wasm"
            }
          }
        }
      }
    }
  },
  "responses": {
    "200": {
      "description": "Встановлений модуль.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/AutomationModule"
          }
        }
      }
    },
    "400": {
      "description": "Відсутній шлях або файл модуля не підтримується.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "501": {
      "description": "Ця збірка не підтримує автоматизацію.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## Зняти модуль

`DELETE /automation/modules/{id}`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "Зняти модуль",
  "description": "Проекти, які використовують модуль, перестають працювати. Існуючі визначення проекту не змінено.",
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string"
      }
    }
  ],
  "responses": {
    "200": {
      "description": "Видалено.",
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "properties": {
              "deleted": {
                "type": "boolean",
                "example": true
              },
              "id": {
                "type": "string"
              }
            }
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "404": {
      "description": "Модуль не знайдено.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "501": {
      "description": "Ця збірка не підтримує автоматизацію.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```
