{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/akeda-ru/json-schema/akeda-ru-automation-rule-document-schema.json",
  "title": "AutomationRuleDocument",
  "x-generated": "2026-11-10",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/akeda-ru-automation-api-openapi.yml#/components/schemas/AutomationRuleDocument",
  "type": "object",
  "required": [
    "id",
    "name",
    "event_type",
    "condition",
    "conditions",
    "actions",
    "executor_user_id",
    "is_enabled",
    "origin",
    "version"
  ],
  "properties": {
    "id": {
      "type": "string",
      "format": "uuid"
    },
    "name": {
      "type": "string"
    },
    "event_type": {
      "type": "string"
    },
    "condition": {
      "type": "string",
      "description": "Выражение вычислителя; у правила из конструктора собрано из conditions"
    },
    "conditions": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "field",
          "op"
        ],
        "properties": {
          "field": {
            "type": "string"
          },
          "op": {
            "type": "string"
          },
          "value": {
            "type": "string"
          },
          "value_to": {
            "type": "string"
          },
          "of": {
            "type": "string"
          },
          "group": {
            "type": "integer"
          }
        }
      }
    },
    "actions": {
      "type": "array",
      "description": "Путь «Если да»: действия и паузы по порядку (ERP-1932)",
      "items": {
        "$ref": "#/$defs/AutomationRuleBlock"
      }
    },
    "branches": {
      "type": "array",
      "description": "«Если ещё …»: до трёх путей со своим условием, проверяются по порядку",
      "items": {
        "$ref": "#/$defs/AutomationRuleBranch"
      }
    },
    "otherwise": {
      "type": "array",
      "description": "«Иначе»: блоки, когда не подошло ни одно условие; пусто — правило промолчит",
      "items": {
        "$ref": "#/$defs/AutomationRuleBlock"
      }
    },
    "after_rule_id": {
      "type": "string",
      "description": "Очередь (ERP-1613): правило ждёт, пока выполнится правило с этим id на тот же факт; пусто — сразу"
    },
    "doc_version": {
      "type": "integer",
      "description": "Версия языка документа правила (rule.schema.json); 2 — пути, паузы и результаты шагов"
    },
    "executor_user_id": {
      "type": "integer",
      "format": "int64"
    },
    "is_enabled": {
      "type": "boolean"
    },
    "origin": {
      "type": "string",
      "enum": [
        "manual",
        "configuration"
      ]
    },
    "version": {
      "type": "integer"
    },
    "created_by": {
      "type": "integer",
      "format": "int64"
    },
    "created_at": {
      "type": "string",
      "format": "date-time"
    },
    "updated_at": {
      "type": "string",
      "format": "date-time"
    }
  },
  "$defs": {
    "AutomationRuleBlock": {
      "type": "object",
      "description": "Шаг пути правила (ERP-1932): действие каталога или пауза «Подождать»\n(`kind: wait`). `id` — устойчивый id шага: журнал, ключ повтора и\nссылки `{{step:<id>.<результат>}}` во входах следующих шагов того же\nпути. Пусто — сервер ставит a1, a2 … (пауза — w1 …).\n",
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{0,63}$"
        },
        "kind": {
          "type": "string",
          "enum": [
            "action",
            "wait"
          ],
          "description": "Пусто — действие"
        },
        "command": {
          "type": "string"
        },
        "inputs": {
          "type": "object",
          "additionalProperties": {
            "type": "string"
          }
        },
        "wait": {
          "type": "object",
          "description": "Пауза: after_event — через amount единиц unit от начала паузы; relative_to_field — до даты поля field (± amount); cancel_on — события, факт которых по той же записи прекращает правило",
          "additionalProperties": true,
          "properties": {
            "kind": {
              "type": "string",
              "enum": [
                "after_event",
                "relative_to_field"
              ]
            },
            "amount": {
              "type": "integer",
              "minimum": 0
            },
            "unit": {
              "type": "string",
              "enum": [
                "minutes",
                "hours",
                "days",
                "working_days"
              ]
            },
            "direction": {
              "type": "string",
              "enum": [
                "before",
                "after"
              ]
            },
            "field": {
              "type": "string"
            },
            "at": {
              "type": "string"
            },
            "cancel_on": {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          }
        }
      }
    },
    "AutomationRuleBranch": {
      "type": "object",
      "description": "«Если ещё …»: своё условие сравнениями и свои шаги",
      "required": [
        "conditions",
        "actions"
      ],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{0,63}$"
        },
        "condition": {
          "type": "string",
          "description": "Выражение, собранное сервером из conditions"
        },
        "conditions": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "field",
              "op"
            ],
            "properties": {
              "field": {
                "type": "string"
              },
              "op": {
                "type": "string"
              },
              "value": {
                "type": "string"
              },
              "value_to": {
                "type": "string"
              },
              "of": {
                "type": "string"
              },
              "group": {
                "type": "integer",
                "minimum": 0
              }
            }
          }
        },
        "actions": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/AutomationRuleBlock"
          }
        }
      }
    }
  }
}