{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://rangka-os.github.io/rangka-os/schema/0.5/flow.json",
  "title": "Rangka flow",
  "description": "Durable flow: steps run in order on an event or a schedule, through the queue.",
  "type": "object",
  "required": [
    "name",
    "steps"
  ],
  "additionalProperties": false,
  "properties": {
    "$schema": {
      "type": "string"
    },
    "name": {
      "$ref": "model.json#/$defs/name"
    },
    "label": {
      "$ref": "model.json#/$defs/label"
    },
    "description": {
      "$ref": "model.json#/$defs/description",
      "description": "Owner sentence of what this flow does, conditions included (contract 0060 §1). Changes no behaviour."
    },
    "on": {
      "type": "string",
      "description": "Event: <connector>.<trigger>, <model>.created|updated|submitted|cancelled, or omitted with schedule"
    },
    "schedule": {
      "type": "string",
      "pattern": "^(@(hourly|daily|weekly|monthly)|([0-9*,/-]+\\s+){4}[0-9*,/-]+)$",
      "description": "Cron (5 fields) in system.scheduler.timezone"
    },
    "steps": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/step"
      }
    },
    "idempotency": {
      "type": "string"
    },
    "queue": {
      "type": "object"
    },
    "if": {
      "$ref": "model.json#/$defs/cel"
    }
  },
  "$defs": {
    "step": {
      "oneOf": [
        {
          "type": "object",
          "title": "find",
          "description": "",
          "required": [
            "find"
          ],
          "additionalProperties": false,
          "properties": {
            "find": {
              "$ref": "model.json#/$defs/name",
              "description": "Model to search; the first match"
            },
            "if": {
              "$ref": "model.json#/$defs/cel",
              "description": "Run the step only when this is true"
            },
            "where": {
              "$ref": "model.json#/$defs/cel"
            },
            "as": {
              "$ref": "model.json#/$defs/name",
              "description": "Variable that holds the result"
            }
          }
        },
        {
          "type": "object",
          "title": "find_all",
          "description": "",
          "required": [
            "find_all"
          ],
          "additionalProperties": false,
          "properties": {
            "find_all": {
              "$ref": "model.json#/$defs/name",
              "description": "Model to search; every match"
            },
            "if": {
              "$ref": "model.json#/$defs/cel",
              "description": "Run the step only when this is true"
            },
            "where": {
              "$ref": "model.json#/$defs/cel"
            },
            "limit": {
              "type": "integer",
              "minimum": 1
            },
            "sort": {
              "type": "string",
              "pattern": "^-?[a-z_]+$"
            },
            "as": {
              "$ref": "model.json#/$defs/name",
              "description": "Variable that holds the result"
            }
          }
        },
        {
          "type": "object",
          "title": "create",
          "description": "",
          "required": [
            "create"
          ],
          "additionalProperties": false,
          "properties": {
            "create": {
              "$ref": "model.json#/$defs/name",
              "description": "Model of the new record"
            },
            "if": {
              "$ref": "model.json#/$defs/cel",
              "description": "Run the step only when this is true"
            },
            "set": {
              "type": "object",
              "additionalProperties": {
                "type": [
                  "string",
                  "number",
                  "boolean",
                  "null"
                ]
              },
              "description": "field → CEL expression"
            },
            "as": {
              "$ref": "model.json#/$defs/name",
              "description": "Variable that holds the result"
            }
          }
        },
        {
          "type": "object",
          "title": "update",
          "description": "",
          "required": [
            "update",
            "set"
          ],
          "additionalProperties": false,
          "properties": {
            "update": {
              "$ref": "model.json#/$defs/name",
              "description": "Variable holding the record to change"
            },
            "if": {
              "$ref": "model.json#/$defs/cel",
              "description": "Run the step only when this is true"
            },
            "set": {
              "type": "object",
              "additionalProperties": {
                "type": [
                  "string",
                  "number",
                  "boolean",
                  "null"
                ]
              },
              "description": "field → CEL expression"
            }
          }
        },
        {
          "type": "object",
          "title": "notify",
          "description": "",
          "required": [
            "notify"
          ],
          "additionalProperties": false,
          "properties": {
            "notify": {
              "type": "string",
              "description": "Who: a variable, a field of the records found, a role or an email"
            },
            "if": {
              "$ref": "model.json#/$defs/cel",
              "description": "Run the step only when this is true"
            },
            "channel": {
              "enum": [
                "email",
                "push",
                "whatsapp",
                "in_app"
              ]
            },
            "template": {
              "oneOf": [
                {
                  "type": "string"
                },
                {
                  "$ref": "model.json#/$defs/label"
                }
              ]
            },
            "to": {
              "type": "string"
            }
          }
        },
        {
          "type": "object",
          "title": "call",
          "description": "",
          "required": [
            "call"
          ],
          "additionalProperties": false,
          "properties": {
            "call": {
              "type": "string",
              "pattern": "^[a-z][a-z0-9_]*\\.[a-z][a-z0-9_]*$",
              "description": "<connector>.<action>"
            },
            "if": {
              "$ref": "model.json#/$defs/cel",
              "description": "Run the step only when this is true"
            },
            "with": {
              "type": "object",
              "additionalProperties": {
                "type": [
                  "string",
                  "number",
                  "boolean",
                  "null"
                ]
              }
            },
            "as": {
              "$ref": "model.json#/$defs/name",
              "description": "Variable that holds the result"
            }
          }
        },
        {
          "type": "object",
          "title": "if",
          "required": [
            "if",
            "then"
          ],
          "additionalProperties": false,
          "properties": {
            "if": {
              "$ref": "model.json#/$defs/cel",
              "description": "Run the step only when this is true"
            },
            "then": {
              "type": "array",
              "minItems": 1,
              "items": {
                "$ref": "#/$defs/step"
              }
            },
            "else": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/step"
              }
            }
          }
        },
        {
          "type": "object",
          "title": "for_each",
          "description": "Run the steps under do once per item of a list (in the flow's one transaction; at most 2000 items; no wait or approve inside)",
          "required": [
            "for_each",
            "as",
            "do"
          ],
          "additionalProperties": false,
          "properties": {
            "for_each": {
              "$ref": "model.json#/$defs/cel",
              "description": "A list, usually a find_all variable"
            },
            "as": {
              "$ref": "model.json#/$defs/name",
              "description": "Variable that holds the current item"
            },
            "if": {
              "$ref": "model.json#/$defs/cel",
              "description": "Run the step only when this is true"
            },
            "do": {
              "type": "array",
              "minItems": 1,
              "items": {
                "$ref": "#/$defs/step"
              }
            }
          }
        },
        {
          "type": "object",
          "title": "wait",
          "description": "",
          "required": [
            "wait"
          ],
          "additionalProperties": false,
          "properties": {
            "wait": {
              "oneOf": [
                {
                  "type": "string",
                  "pattern": "^[0-9]+(s|m|h|d)$"
                },
                {
                  "type": "object",
                  "required": [
                    "until"
                  ],
                  "additionalProperties": false,
                  "properties": {
                    "until": {
                      "$ref": "model.json#/$defs/cel"
                    },
                    "timeout": {
                      "type": "string",
                      "pattern": "^[0-9]+(s|m|h|d)$"
                    }
                  }
                }
              ]
            },
            "if": {
              "$ref": "model.json#/$defs/cel",
              "description": "Run the step only when this is true"
            }
          }
        },
        {
          "type": "object",
          "title": "approve",
          "description": "Wait for approval before the flow goes on",
          "required": [
            "approve"
          ],
          "additionalProperties": false,
          "properties": {
            "approve": {
              "type": "object",
              "required": [
                "approvers"
              ],
              "additionalProperties": false,
              "properties": {
                "approvers": {
                  "type": "array",
                  "minItems": 1,
                  "items": {
                    "$ref": "model.json#/$defs/name"
                  }
                },
                "when": {
                  "$ref": "model.json#/$defs/cel"
                },
                "label": {
                  "$ref": "model.json#/$defs/label"
                }
              }
            },
            "if": {
              "$ref": "model.json#/$defs/cel",
              "description": "Run the step only when this is true"
            },
            "as": {
              "$ref": "model.json#/$defs/name"
            }
          }
        }
      ]
    }
  }
}
