{
  "$id": "https://docs.cowliss.com/schemas/automation.json",
  "title": "Automation definition",
  "description": "A Cowliss automation: a trigger and a closed list of steps, with JSONLogic in a branch condition and in sendEmail props or sendWebhook payload values.",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "trigger": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "event": {
              "anyOf": [
                {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 200
                },
                {
                  "minItems": 1,
                  "maxItems": 20,
                  "type": "array",
                  "items": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 200
                  }
                }
              ]
            }
          },
          "required": [
            "event"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "segment": {
              "type": "object",
              "properties": {
                "match": {
                  "default": "all",
                  "type": "string",
                  "enum": [
                    "all",
                    "any"
                  ]
                },
                "predicates": {
                  "minItems": 1,
                  "type": "array",
                  "items": {
                    "oneOf": [
                      {
                        "type": "object",
                        "properties": {
                          "kind": {
                            "type": "string",
                            "const": "trait"
                          },
                          "name": {
                            "type": "string",
                            "minLength": 1,
                            "maxLength": 200
                          },
                          "op": {
                            "type": "string",
                            "enum": [
                              "eq",
                              "neq",
                              "gt",
                              "gte",
                              "lt",
                              "lte",
                              "exists",
                              "notExists",
                              "contains",
                              "before",
                              "after"
                            ]
                          },
                          "value": {}
                        },
                        "required": [
                          "kind",
                          "name",
                          "op"
                        ]
                      },
                      {
                        "type": "object",
                        "properties": {
                          "kind": {
                            "type": "string",
                            "const": "event"
                          },
                          "name": {
                            "anyOf": [
                              {
                                "type": "string",
                                "minLength": 1,
                                "maxLength": 200
                              },
                              {
                                "minItems": 1,
                                "maxItems": 20,
                                "type": "array",
                                "items": {
                                  "type": "string",
                                  "minLength": 1,
                                  "maxLength": 200
                                }
                              }
                            ]
                          },
                          "op": {
                            "type": "string",
                            "enum": [
                              "performed",
                              "notPerformed"
                            ]
                          },
                          "atLeast": {
                            "default": 1,
                            "type": "integer",
                            "minimum": 1,
                            "maximum": 9007199254740991
                          },
                          "withinDays": {
                            "type": "integer",
                            "minimum": 1,
                            "maximum": 9007199254740991
                          }
                        },
                        "required": [
                          "kind",
                          "name",
                          "op"
                        ]
                      }
                    ]
                  }
                }
              },
              "required": [
                "predicates"
              ],
              "additionalProperties": false
            }
          },
          "required": [
            "segment"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "segment": {
              "type": "string",
              "maxLength": 64,
              "pattern": "^[a-z0-9]+(-[a-z0-9]+)*$"
            }
          },
          "required": [
            "segment"
          ],
          "additionalProperties": false
        }
      ]
    },
    "purpose": {
      "type": "string",
      "enum": [
        "marketing",
        "transactional"
      ]
    },
    "from": {
      "type": "string",
      "maxLength": 200
    },
    "tags": {
      "default": [],
      "maxItems": 20,
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 50
      }
    },
    "description": {
      "type": "string",
      "maxLength": 500
    },
    "enrollment": {
      "type": "object",
      "properties": {
        "cooldown": {
          "anyOf": [
            {
              "type": "string",
              "minLength": 1
            },
            {
              "type": "integer",
              "minimum": 0,
              "maximum": 9007199254740991
            }
          ]
        }
      },
      "required": [
        "cooldown"
      ],
      "additionalProperties": false
    },
    "steps": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/AutomationStep"
      }
    }
  },
  "required": [
    "trigger",
    "purpose",
    "from",
    "steps"
  ],
  "additionalProperties": false,
  "$defs": {
    "AutomationStep": {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "id": {
              "type": "string",
              "maxLength": 64,
              "pattern": "^[a-zA-Z][a-zA-Z0-9_-]*$"
            },
            "type": {
              "type": "string",
              "const": "sleep"
            },
            "duration": {
              "anyOf": [
                {
                  "type": "string",
                  "minLength": 1
                },
                {
                  "type": "integer",
                  "minimum": 0,
                  "maximum": 9007199254740991
                }
              ]
            }
          },
          "required": [
            "id",
            "type",
            "duration"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "id": {
              "type": "string",
              "maxLength": 64,
              "pattern": "^[a-zA-Z][a-zA-Z0-9_-]*$"
            },
            "type": {
              "type": "string",
              "const": "waitForEvent"
            },
            "event": {
              "anyOf": [
                {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 200
                },
                {
                  "minItems": 1,
                  "maxItems": 20,
                  "type": "array",
                  "items": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 200
                  }
                }
              ]
            },
            "timeout": {
              "anyOf": [
                {
                  "type": "string",
                  "minLength": 1
                },
                {
                  "type": "integer",
                  "minimum": 0,
                  "maximum": 9007199254740991
                }
              ]
            }
          },
          "required": [
            "id",
            "type",
            "event",
            "timeout"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "id": {
              "type": "string",
              "maxLength": 64,
              "pattern": "^[a-zA-Z][a-zA-Z0-9_-]*$"
            },
            "type": {
              "type": "string",
              "const": "sendEmail"
            },
            "template": {
              "type": "string",
              "maxLength": 64,
              "pattern": "^[a-z0-9]+(-[a-z0-9]+)*$"
            },
            "props": {
              "default": {},
              "type": "object",
              "propertyNames": {
                "type": "string"
              },
              "additionalProperties": {
                "$ref": "#/$defs/AutomationRule"
              }
            },
            "from": {
              "type": "string",
              "maxLength": 200
            }
          },
          "required": [
            "id",
            "type",
            "template"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "id": {
              "type": "string",
              "maxLength": 64,
              "pattern": "^[a-zA-Z][a-zA-Z0-9_-]*$"
            },
            "type": {
              "type": "string",
              "const": "sendWebhook"
            },
            "webhook": {
              "description": "The name of a webhook your organization registered, not a URL.",
              "type": "string",
              "minLength": 1
            },
            "destination": {
              "deprecated": true,
              "description": "Deprecated: the old name of webhook, removed at the next major.",
              "type": "string",
              "minLength": 1
            },
            "payload": {
              "default": {},
              "type": "object",
              "propertyNames": {
                "type": "string"
              },
              "additionalProperties": {
                "$ref": "#/$defs/AutomationRule"
              }
            }
          },
          "required": [
            "id",
            "type"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "id": {
              "type": "string",
              "maxLength": 64,
              "pattern": "^[a-zA-Z][a-zA-Z0-9_-]*$"
            },
            "type": {
              "type": "string",
              "const": "branch"
            },
            "if": {
              "$ref": "#/$defs/AutomationRule"
            },
            "then": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/AutomationStep"
              }
            },
            "else": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/AutomationStep"
              }
            }
          },
          "required": [
            "id",
            "type",
            "if",
            "then"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "id": {
              "type": "string",
              "maxLength": 64,
              "pattern": "^[a-zA-Z][a-zA-Z0-9_-]*$"
            },
            "type": {
              "type": "string",
              "const": "exit"
            }
          },
          "required": [
            "id",
            "type"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "id": {
              "type": "string",
              "maxLength": 64,
              "pattern": "^[a-zA-Z][a-zA-Z0-9_-]*$"
            },
            "type": {
              "type": "string",
              "const": "restart"
            }
          },
          "required": [
            "id",
            "type"
          ],
          "additionalProperties": false
        }
      ]
    },
    "AutomationRule": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "number"
        },
        {
          "type": "boolean"
        },
        {
          "type": "null"
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/$defs/AutomationRule"
          }
        },
        {
          "type": "object",
          "propertyNames": {
            "type": "string"
          },
          "additionalProperties": {
            "$ref": "#/$defs/AutomationRule"
          }
        }
      ]
    }
  }
}
