{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://interchange.stellarview.net/schemas/trigger.schema.json",
  "title": "A trigger: a rule that traps an incoming reading or event and names the event it becomes. Defined, not coded.",
  "type": "object",
  "required": [
    "id",
    "kind",
    "basis",
    "output"
  ],
  "additionalProperties": false,
  "properties": {
    "id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9_.-]*$"
    },
    "kind": {
      "enum": [
        "time",
        "area",
        "value",
        "trend",
        "image",
        "ai",
        "other",
        "interval_trend"
      ]
    },
    "about": {
      "type": "string"
    },
    "basis": {
      "type": "string",
      "description": "Where the trigger's numbers come from: a policy, a maker's limit, or this repo's example value."
    },
    "inputs": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "common.schema.json#/$defs/event_type_id"
      }
    },
    "where": {
      "type": "object",
      "additionalProperties": {
        "type": [
          "string",
          "number",
          "boolean"
        ]
      },
      "description": "Field equalities an input must meet."
    },
    "time": {
      "type": "object",
      "additionalProperties": false,
      "description": "kind time: silence (no reading from a sensor for silence_s) or a schedule (every every_s from start).",
      "properties": {
        "silence_s": {
          "type": "integer",
          "minimum": 1
        },
        "sensor_types": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "description": "For silence: which sensor assets are watched."
        },
        "every_s": {
          "type": "integer",
          "minimum": 1
        },
        "start": {
          "$ref": "common.schema.json#/$defs/time"
        },
        "round": {
          "type": "string"
        }
      }
    },
    "area": {
      "type": "object",
      "required": [
        "zones"
      ],
      "additionalProperties": false,
      "properties": {
        "zones": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "string"
          }
        },
        "zone_field": {
          "enum": [
            "building",
            "level",
            "section",
            "room"
          ]
        }
      }
    },
    "value": {
      "type": "object",
      "required": [
        "field"
      ],
      "additionalProperties": false,
      "description": "Fires when the field is above, below, or outside the band.",
      "properties": {
        "field": {
          "type": "string"
        },
        "above": {
          "type": "number"
        },
        "below": {
          "type": "number"
        }
      }
    },
    "trend": {
      "type": "object",
      "required": [
        "field",
        "direction",
        "window_s",
        "min_change",
        "min_points"
      ],
      "additionalProperties": false,
      "properties": {
        "field": {
          "type": "string"
        },
        "direction": {
          "enum": [
            "rising",
            "falling"
          ]
        },
        "window_s": {
          "type": "integer",
          "minimum": 1
        },
        "min_change": {
          "type": "number",
          "minimum": 0
        },
        "min_points": {
          "type": "integer",
          "minimum": 2
        }
      }
    },
    "ai": {
      "type": "object",
      "required": [
        "judge",
        "question",
        "min_confidence"
      ],
      "additionalProperties": false,
      "description": "A model judges the input; a person confirms before the event is raised.",
      "properties": {
        "judge": {
          "type": "string"
        },
        "question": {
          "type": "string"
        },
        "min_confidence": {
          "type": "number",
          "minimum": 0,
          "maximum": 100
        }
      }
    },
    "output": {
      "type": "object",
      "required": [
        "event_type",
        "fields"
      ],
      "additionalProperties": false,
      "properties": {
        "event_type": {
          "$ref": "common.schema.json#/$defs/event_type_id"
        },
        "fields": {
          "type": "object",
          "description": "Constants, or $value, $limit, $change, $window_s, $points, $device, $zone, $seen, $trigger, $silent_s, $last_seen, $due_at, $round, $finding_id, $subject, $judgement, $confidence, $confirmed_by. For interval_trend: $event_type, $baseline_days, $recent_days, $change_days, $change_percent, $repeats, $threshold."
        },
        "about": {
          "enum": [
            "target",
            "sensor"
          ],
          "description": "target (default): the event is about what the input was about. sensor: about the sensor asset itself."
        }
      }
    },
    "path": {
      "type": "string",
      "description": "A path this trigger pins. It wins among exclusive paths."
    },
    "interval_trend": {
      "type": "object",
      "required": [
        "thresholds"
      ],
      "additionalProperties": false,
      "description": "Tracks the time between repeats of an input event type on one asset. The baseline window, recent window and minimum alarms come from the threshold's parameters in the Mission Studio export; a missing parameter is a gap. Fires when the recent interval is shorter than the baseline by the threshold's value or more.",
      "properties": {
        "thresholds": {
          "type": "object",
          "required": [
            "from",
            "rule_kind",
            "unit"
          ],
          "additionalProperties": false,
          "description": "Where the threshold comes from: the Mission Studio's export, by the target's asset class and this rule kind, in this unit. Only a usable row fires.",
          "properties": {
            "from": {
              "const": "studio"
            },
            "rule_kind": {
              "const": "interval_trend"
            },
            "unit": {
              "type": "string",
              "description": "The unit the value must be in. A row in another unit is a gap."
            }
          }
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "kind": {
            "const": "time"
          }
        }
      },
      "then": {
        "required": [
          "time"
        ]
      },
      "else": {
        "required": [
          "inputs"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "kind": {
            "const": "area"
          }
        }
      },
      "then": {
        "required": [
          "area"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "kind": {
            "const": "value"
          }
        }
      },
      "then": {
        "required": [
          "value"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "kind": {
            "const": "trend"
          }
        }
      },
      "then": {
        "required": [
          "trend"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "kind": {
            "const": "ai"
          }
        }
      },
      "then": {
        "required": [
          "ai"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "kind": {
            "const": "interval_trend"
          }
        }
      },
      "then": {
        "required": [
          "interval_trend"
        ]
      }
    }
  ]
}