{
  "openapi": "3.0.0",
  "paths": {
    "/rules/events": {
      "get": {
        "operationId": "listEvents",
        "summary": "List rule events",
        "description": "List rule events.\n\nRequired Permission:\n\n - Resource: ```ruleengine:rules```\n\n - Action: ```ruleengine:rules:list```",
        "parameters": [
          {
            "name": "ruleId",
            "required": false,
            "in": "query",
            "deprecated": true,
            "schema": { "type": "string" }
          },
          {
            "name": "ruleIds",
            "required": false,
            "in": "query",
            "schema": { "type": "array", "items": { "type": "string" } }
          },
          {
            "name": "from",
            "required": false,
            "in": "query",
            "schema": { "format": "date-time", "type": "string" }
          },
          {
            "name": "to",
            "required": false,
            "in": "query",
            "schema": { "format": "date-time", "type": "string" }
          },
          {
            "name": "status",
            "required": false,
            "in": "query",
            "schema": { "enum": ["active", "inactive"], "type": "string" }
          },
          {
            "name": "page",
            "required": false,
            "in": "query",
            "schema": { "minimum": 0, "default": 0, "type": "number" }
          },
          {
            "name": "pageSize",
            "required": false,
            "in": "query",
            "schema": { "minimum": 0, "default": 1000, "type": "number" }
          },
          {
            "name": "sort",
            "required": false,
            "in": "query",
            "schema": {
              "pattern": "sortingStringRegex",
              "default": "createdAt:desc",
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/PagedRulesEventsDto" }
              }
            }
          }
        },
        "tags": ["Rules Events"],
        "security": [{ "chx_auth_session": [] }, { "bearer": [] }]
      }
    },
    "/rules/bulk": {
      "post": {
        "operationId": "createBulk",
        "summary": "Bulk create rules",
        "description": "Bulk create rules.\n\nRequired Permission:\n\n - Resource: ```ruleengine:rules```\n\n - Action: ```ruleengine:rules:create```",
        "parameters": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/BulkCreateRulesDto" }
            }
          }
        },
        "responses": {
          "201": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": { "$ref": "#/components/schemas/ReadRuleDto" }
                }
              }
            }
          },
          "400": { "description": "Missing user identity id" }
        },
        "tags": ["Rules"],
        "security": [{ "chx_auth_session": [] }, { "bearer": [] }]
      }
    },
    "/rules": {
      "post": {
        "operationId": "create",
        "summary": "Create rule",
        "description": "Create rule.\n\nRequired Permission:\n\n - Resource: ```ruleengine:rules```\n\n - Action: ```ruleengine:rules:create```",
        "parameters": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CreateRuleDto" }
            }
          }
        },
        "responses": {
          "201": {
            "description": "",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ReadRuleDto" }
              }
            }
          },
          "400": { "description": "Missing user identity id" }
        },
        "tags": ["Rules"],
        "security": [{ "chx_auth_session": [] }, { "bearer": [] }]
      },
      "get": {
        "operationId": "list",
        "summary": "List rules",
        "description": "List rules.\n\nRequired Permission:\n\n - Resource: ```ruleengine:rules```\n\n - Action: ```ruleengine:rules:list```",
        "parameters": [
          {
            "name": "ids",
            "required": false,
            "in": "query",
            "schema": { "type": "array", "items": { "type": "string" } }
          },
          {
            "name": "tagLabels",
            "required": false,
            "in": "query",
            "schema": { "type": "array", "items": { "type": "string" } }
          },
          {
            "name": "tagLabelValues",
            "required": false,
            "in": "query",
            "schema": { "type": "array", "items": { "type": "string" } }
          },
          {
            "name": "severity",
            "required": false,
            "in": "query",
            "schema": {
              "enum": ["info", "warning", "critical"],
              "type": "string"
            }
          },
          {
            "name": "status",
            "required": false,
            "in": "query",
            "schema": { "enum": ["active", "inactive"], "type": "string" }
          },
          {
            "name": "page",
            "required": false,
            "in": "query",
            "schema": { "minimum": 0, "default": 0, "type": "number" }
          },
          {
            "name": "pageSize",
            "required": false,
            "in": "query",
            "schema": { "minimum": 0, "default": 1000, "type": "number" }
          },
          {
            "name": "createdAfter",
            "required": false,
            "in": "query",
            "schema": { "format": "date-time", "type": "string" }
          },
          {
            "name": "sort",
            "required": false,
            "in": "query",
            "schema": {
              "pattern": "sortingStringRegex",
              "default": "createdAt:desc",
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/PagedRulesDto" }
              }
            }
          }
        },
        "tags": ["Rules"],
        "security": [{ "chx_auth_session": [] }, { "bearer": [] }]
      }
    },
    "/rules/{id}": {
      "get": {
        "operationId": "read",
        "summary": "Read rule",
        "description": "Read rule.\n\nRequired Permission:\n\n - Resource: ```ruleengine:rules:{id}```\n\n - Action: ```ruleengine:rules:get```",
        "parameters": [
          {
            "name": "id",
            "required": true,
            "in": "path",
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ReadRuleDto" }
              }
            }
          },
          "404": { "description": "" }
        },
        "tags": ["Rules"],
        "security": [{ "chx_auth_session": [] }, { "bearer": [] }]
      },
      "patch": {
        "operationId": "patch",
        "summary": "Update rule",
        "description": "Update rule.\n\nRequired Permission:\n\n - Resource: ```ruleengine:rules:{id}```\n\n - Action: ```ruleengine:rules:update```",
        "parameters": [
          {
            "name": "id",
            "required": true,
            "in": "path",
            "schema": { "type": "string" }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/PatchRuleDto" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ReadRuleDto" }
              }
            }
          },
          "404": { "description": "" }
        },
        "tags": ["Rules"],
        "security": [{ "chx_auth_session": [] }, { "bearer": [] }]
      },
      "delete": {
        "operationId": "delete",
        "summary": "Delete rule",
        "description": "Delete rule.\n\nRequired Permission:\n\n - Resource: ```ruleengine:rules:{id}```\n\n - Action: ```ruleengine:rules:delete```",
        "parameters": [
          {
            "name": "id",
            "required": true,
            "in": "path",
            "schema": { "type": "number" }
          }
        ],
        "responses": {
          "200": { "description": "" },
          "404": { "description": "" }
        },
        "tags": ["Rules"],
        "security": [{ "chx_auth_session": [] }, { "bearer": [] }]
      }
    }
  },
  "info": {
    "title": "connhex-rules-engine",
    "description": "",
    "version": "1.2.0",
    "contact": {}
  },
  "tags": [],
  "servers": [{ "url": "https://apis.<domain>" }],
  "components": {
    "securitySchemes": {
      "bearer": { "type": "http", "scheme": "bearer" },
      "chx_auth_session": {
        "type": "apiKey",
        "in": "cookie",
        "name": "chx_auth_session"
      }
    },
    "schemas": {
      "NotificationMessageDto": {
        "type": "object",
        "properties": {
          "target": {
            "type": "string",
            "description": "Notification target, whose format depends on the selected `medium`.\nIf not specified, the notification is sent to the user who created the rule.\n\nSupported formats:\n- `email`: email address\n- `sms`: E.164 phone number (e.g. \"+391234567890\")\n- `telegram`: username (\"@user\")\n- `slack` / `discord` / `msteams`: webhook URL\n- `fcm`: device registration token\n\nThe value must match the expected format for the chosen medium. Invalid or\nincompatible values may result in notification delivery failure.",
            "deprecated": true
          },
          "targets": {
            "description": "Notification targets: an array of target addresses (emails, phone numbers, device tokens, etc.).",
            "example": ["admin@connhex.com", "user@connhex.com"],
            "type": "array",
            "items": { "type": "string" }
          },
          "policy": {
            "type": "string",
            "description": "Defines when the notification should be dispatched:\n- `trigger`: The notification is sent when the rule is triggered (its conditions are satisfied).\n- `reentry`: The notification is sent when the rule re-enters (its associated conditions are no longer satisfied).",
            "enum": ["trigger", "reentry"]
          },
          "medium": {
            "type": "string",
            "enum": [
              "email",
              "telegram",
              "sms",
              "slack",
              "discord",
              "fcm",
              "msteams"
            ]
          },
          "text": { "type": "string" },
          "muted": { "type": "boolean" }
        },
        "required": ["policy", "medium", "text"]
      },
      "NotificationDto": {
        "type": "object",
        "properties": {
          "messages": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/NotificationMessageDto" }
          }
        },
        "required": ["messages"]
      },
      "ReadRuleSnapshotDto": {
        "type": "object",
        "properties": {
          "name": { "type": "string" },
          "severity": {
            "type": "string",
            "enum": ["info", "warning", "critical"]
          },
          "description": { "type": "string" },
          "processable": {
            "type": "string",
            "default": "enabled",
            "enum": ["enabled", "disabled"]
          },
          "notification": { "$ref": "#/components/schemas/NotificationDto" },
          "id": { "type": "string" }
        },
        "required": ["name", "notification", "id"]
      },
      "ReadConditionEventDto": {
        "type": "object",
        "properties": {
          "conditionId": { "type": "string" },
          "conditionSnapshot": { "type": "object" },
          "status": { "type": "string", "enum": ["active", "inactive"] },
          "createdAt": { "format": "date-time", "type": "string" },
          "id": { "type": "string" }
        },
        "required": [
          "conditionId",
          "conditionSnapshot",
          "status",
          "createdAt",
          "id"
        ]
      },
      "ReadRuleEventDto": {
        "type": "object",
        "properties": {
          "ruleId": { "type": "string", "deprecated": true },
          "ruleSnapshot": {
            "$ref": "#/components/schemas/ReadRuleSnapshotDto"
          },
          "conditionsEvents": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/ReadConditionEventDto" }
          },
          "status": { "type": "string", "enum": ["active", "inactive"] },
          "createdAt": { "type": "string" },
          "id": { "type": "string" }
        },
        "required": [
          "ruleId",
          "ruleSnapshot",
          "conditionsEvents",
          "status",
          "createdAt",
          "id"
        ]
      },
      "PagedRulesEventsDto": {
        "type": "object",
        "properties": {
          "results": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/ReadRuleEventDto" }
          },
          "total": { "type": "number" }
        },
        "required": ["results", "total"]
      },
      "CreateTagDto": {
        "type": "object",
        "properties": {
          "label": {
            "type": "string",
            "description": "The key or category of the tag used for grouping or filtering.",
            "example": "deviceID"
          },
          "labelValue": {
            "type": "string",
            "description": "The specific value assigned to the tag label.",
            "example": "e0ce7733-cd7c-415b-8430-4b1a5876362d"
          },
          "metadata": {
            "type": "object",
            "description": "Additional custom properties or contextual data associated with this tag.\nUseful for storing extra structured information.",
            "example": "{ 'alias': 'Main Boiler Sensor', 'location': 'Warehouse A' }"
          }
        },
        "required": ["label", "labelValue"]
      },
      "ActiveWindowDto": {
        "type": "object",
        "properties": {
          "from": {
            "type": "string",
            "example": "08:10",
            "pattern": "/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/"
          },
          "to": {
            "type": "string",
            "example": "13:15",
            "pattern": "/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/"
          },
          "timezone": {
            "type": "string",
            "description": "IANA timezone identifier",
            "example": "Europe/Rome"
          }
        },
        "required": ["from", "to", "timezone"]
      },
      "ThresholdConditionParamsDto": {
        "type": "object",
        "properties": {
          "source": {
            "type": "string",
            "enum": ["messages", "params", "metrics"]
          },
          "metric": {
            "type": "string",
            "description": "URN of the metric to be monitored",
            "example": "urn:cpt:smartdevice:nid:D0CF5EFFFE26FDAB1:onoff"
          },
          "threshold": { "type": "number" },
          "comparisonOperator": {
            "type": "string",
            "enum": [">", ">=", "<", "<=", "==", "!="]
          },
          "unit": { "type": "string", "example": "°C" },
          "durationSecs": { "type": "number", "minimum": 0, "maximum": 86400 },
          "activeWindow": { "$ref": "#/components/schemas/ActiveWindowDto" },
          "channelId": {
            "type": "string",
            "description": "Connectable channel whose messages are evaluated by the condition.\nYou must have access to this channel (action `core:channels:subscribe`)\nto create or update a rule."
          }
        },
        "required": [
          "metric",
          "threshold",
          "comparisonOperator",
          "durationSecs",
          "channelId"
        ]
      },
      "LastMessageOlderThanConditionParamsDto": {
        "type": "object",
        "properties": {
          "source": {
            "type": "string",
            "enum": ["messages", "params", "metrics"]
          },
          "metric": {
            "type": "string",
            "description": "If specified, the condition will be evaluated based on the metric value of the last message in the channel.\nThe value may optionally end with a single wildcard (`*`) to match any suffix.\nNo other wildcard placements are allowed (e.g. \"cpu*idle\", \"*cpu\", or multiple `*` are invalid).",
            "examples": ["urn:cpt:monitor:cpu:idle", "urn:cpt:monitor:cpu:*"],
            "pattern": "SINGLE_TRAILING_WILDCARD_REGEX"
          },
          "durationSecs": { "type": "number", "minimum": 0, "maximum": 86400 },
          "activeWindow": { "$ref": "#/components/schemas/ActiveWindowDto" },
          "channelId": {
            "type": "string",
            "description": "Connectable channel whose messages are evaluated by the condition.\nYou must have access to this channel (action `core:channels:subscribe`)\nto create or update a rule."
          }
        },
        "required": ["durationSecs", "channelId"]
      },
      "DeltaConditionParamsDto": {
        "type": "object",
        "properties": {
          "delta": { "type": "number" },
          "channelId": {
            "type": "string",
            "description": "Connectable channel whose messages are evaluated by the condition.\nYou must have access to this channel (action `core:channels:subscribe`)\nto create or update a rule."
          },
          "source": {
            "type": "string",
            "enum": ["messages", "params", "metrics"]
          },
          "metric": {
            "type": "string",
            "description": "URN of the metric to be monitored",
            "example": "urn:cpt:smartdevice:nid:D0CF5EFFFE26FDAB1:onoff"
          },
          "comparisonOperator": {
            "type": "string",
            "enum": [">", ">=", "<", "<=", "==", "!="]
          },
          "unit": { "type": "string", "example": "°C" },
          "durationSecs": { "type": "number", "minimum": 0, "maximum": 86400 },
          "activeWindow": { "$ref": "#/components/schemas/ActiveWindowDto" }
        },
        "required": [
          "delta",
          "channelId",
          "metric",
          "comparisonOperator",
          "durationSecs"
        ]
      },
      "CreateConditionDto": {
        "type": "object",
        "properties": {
          "params": {
            "oneOf": [
              { "$ref": "#/components/schemas/ThresholdConditionParamsDto" },
              {
                "$ref": "#/components/schemas/LastMessageOlderThanConditionParamsDto"
              },
              { "$ref": "#/components/schemas/DeltaConditionParamsDto" }
            ]
          },
          "type": {
            "type": "string",
            "enum": ["threshold", "lastMessageOlderThan", "delta"]
          }
        },
        "required": ["params", "type"]
      },
      "CreateRuleDto": {
        "type": "object",
        "properties": {
          "name": { "type": "string" },
          "severity": {
            "enum": ["info", "warning", "critical"],
            "type": "string"
          },
          "description": { "type": "string" },
          "processable": {
            "default": "enabled",
            "enum": ["enabled", "disabled"],
            "type": "string"
          },
          "tags": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/CreateTagDto" }
          },
          "notification": { "$ref": "#/components/schemas/NotificationDto" },
          "conditions": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/CreateConditionDto" }
          }
        },
        "required": ["name", "notification", "conditions"]
      },
      "BulkCreateRulesDto": {
        "type": "object",
        "properties": {
          "rules": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/CreateRuleDto" }
          }
        },
        "required": ["rules"]
      },
      "ReadConditionDto": {
        "type": "object",
        "properties": {
          "params": {
            "oneOf": [
              { "$ref": "#/components/schemas/ThresholdConditionParamsDto" },
              {
                "$ref": "#/components/schemas/LastMessageOlderThanConditionParamsDto"
              },
              { "$ref": "#/components/schemas/DeltaConditionParamsDto" }
            ]
          },
          "status": { "type": "string", "enum": ["active", "inactive"] },
          "createdAt": { "format": "date-time", "type": "string" },
          "updatedAt": { "format": "date-time", "type": "string" },
          "deletedAt": { "format": "date-time", "type": "string" },
          "type": {
            "type": "string",
            "enum": ["threshold", "lastMessageOlderThan", "delta"]
          },
          "id": { "type": "string" }
        },
        "required": [
          "params",
          "status",
          "createdAt",
          "updatedAt",
          "deletedAt",
          "type",
          "id"
        ]
      },
      "ReadTagDto": {
        "type": "object",
        "properties": {
          "createdAt": { "format": "date-time", "type": "string" },
          "updatedAt": { "format": "date-time", "type": "string" },
          "deletedAt": { "format": "date-time", "type": "string" },
          "label": {
            "type": "string",
            "description": "The key or category of the tag used for grouping or filtering.",
            "example": "deviceID"
          },
          "labelValue": {
            "type": "string",
            "description": "The specific value assigned to the tag label.",
            "example": "e0ce7733-cd7c-415b-8430-4b1a5876362d"
          },
          "metadata": {
            "type": "object",
            "description": "Additional custom properties or contextual data associated with this tag.\nUseful for storing extra structured information.",
            "example": "{ 'alias': 'Main Boiler Sensor', 'location': 'Warehouse A' }"
          },
          "id": { "type": "string" }
        },
        "required": [
          "createdAt",
          "updatedAt",
          "deletedAt",
          "label",
          "labelValue",
          "id"
        ]
      },
      "ReadRuleDto": {
        "type": "object",
        "properties": {
          "conditions": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/ReadConditionDto" }
          },
          "tags": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/ReadTagDto" }
          },
          "status": { "enum": ["active", "inactive"], "type": "string" },
          "createdAt": { "format": "date-time", "type": "string" },
          "updatedAt": { "format": "date-time", "type": "string" },
          "deletedAt": { "format": "date-time", "type": "string" },
          "name": { "type": "string" },
          "severity": {
            "enum": ["info", "warning", "critical"],
            "type": "string"
          },
          "description": { "type": "string" },
          "processable": {
            "default": "enabled",
            "enum": ["enabled", "disabled"],
            "type": "string"
          },
          "notification": { "$ref": "#/components/schemas/NotificationDto" },
          "id": { "type": "string" }
        },
        "required": [
          "conditions",
          "tags",
          "status",
          "createdAt",
          "updatedAt",
          "deletedAt",
          "name",
          "notification",
          "id"
        ]
      },
      "PatchRuleDto": {
        "type": "object",
        "properties": {
          "conditions": { "type": "array", "items": { "type": "object" } },
          "tags": { "type": "array", "items": { "type": "object" } },
          "name": { "type": "string" },
          "severity": {
            "type": "string",
            "enum": ["info", "warning", "critical"]
          },
          "description": { "type": "string" },
          "processable": {
            "type": "string",
            "default": "enabled",
            "enum": ["enabled", "disabled"]
          },
          "notification": { "$ref": "#/components/schemas/NotificationDto" }
        }
      },
      "PagedRulesDto": {
        "type": "object",
        "properties": {
          "results": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/ReadRuleDto" }
          },
          "total": { "type": "number" }
        },
        "required": ["results", "total"]
      }
    }
  }
}
