{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://probeward.dev/schemas/config-export-v1.json",
  "title": "Probeward configuration files, format_version 1",
  "description": "What `probeward admin export` and the export API write and the import reads. Validate each YAML file after converting it to JSON. `probeward.yaml` is the `root` definition; in the directory layout the other files are `site`, `group` and `device`. See docs/export-import.md.",
  "anyOf": [
    { "$ref": "#/$defs/root" }
  ],
  "$defs": {
    "uuid": {
      "type": "string",
      "pattern": "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
    },
    "name": { "type": "string", "minLength": 1, "maxLength": 200 },
    "weekday": { "enum": ["mon", "tue", "wed", "thu", "fri", "sat", "sun"] },
    "time_of_day": {
      "type": "string",
      "pattern": "^(([01][0-9]|2[0-3]):[0-5][0-9]|24:00)$"
    },
    "date": { "type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" },
    "path": {
      "type": "array",
      "items": { "type": "string" },
      "description": "Names from the site down."
    },
    "scope": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind"],
      "properties": {
        "kind": { "enum": ["tree", "site", "group", "device"] },
        "parent": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "id": { "$ref": "#/$defs/uuid" },
            "path": { "$ref": "#/$defs/path" }
          }
        }
      }
    },
    "template": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id"],
      "properties": {
        "id": { "type": "string" },
        "version": { "type": "string" }
      }
    },
    "sensor_ref": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/uuid" },
        "path": { "$ref": "#/$defs/path" }
      },
      "anyOf": [{ "required": ["id"] }, { "required": ["path"] }]
    },
    "limits": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "error_min": { "type": "number" },
        "warn_min": { "type": "number" },
        "warn_max": { "type": "number" },
        "error_max": { "type": "number" }
      }
    },
    "notify_rule": {
      "type": "object",
      "additionalProperties": false,
      "required": ["destination"],
      "properties": {
        "destination": { "type": "string", "description": "The name of a destination." },
        "on_warning": { "type": "boolean" },
        "schedule": { "type": "string", "description": "The name of a schedule." }
      }
    },
    "settings": {
      "type": "object",
      "description": "Only what is set at this level. An absent key inherits; a present key overrides, even with the default value. Credentials, destinations and schedules are named. `pause` is operational state and is not part of the files.",
      "additionalProperties": false,
      "properties": {
        "interval_secs": { "type": "integer", "minimum": 1, "maximum": 86400 },
        "timeout_secs": { "type": "integer", "minimum": 1, "maximum": 3600 },
        "failure_count": { "type": "integer", "minimum": 1, "maximum": 100 },
        "repeat_interval_secs": { "type": "integer", "minimum": 0, "maximum": 604800 },
        "max_concurrent_polls": { "type": "integer", "minimum": 1, "maximum": 64 },
        "snmp_credential": { "type": "string" },
        "ssh_credential": { "type": "string" },
        "windows_credential": { "type": "string" },
        "radius_credential": { "type": "string" },
        "vsphere_credential": { "type": "string" },
        "aws_credential": { "type": "string" },
        "secret_credential": { "type": "string" },
        "login_credential": { "type": "string" },
        "tls_client_credential": { "type": "string" },
        "notify": { "type": "array", "items": { "$ref": "#/$defs/notify_rule" } },
        "monitoring_schedule": {
          "oneOf": [
            { "const": "always" },
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["schedule"],
              "properties": { "schedule": { "type": "string" } }
            }
          ]
        }
      }
    },
    "timing": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "time_zone", "days", "start", "duration_minutes"],
          "properties": {
            "type": { "const": "weekly" },
            "time_zone": { "type": "string" },
            "days": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/weekday" } },
            "start": { "$ref": "#/$defs/time_of_day" },
            "duration_minutes": { "type": "integer", "minimum": 1 }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "time_zone", "day", "start", "duration_minutes"],
          "properties": {
            "type": { "const": "monthly" },
            "time_zone": { "type": "string" },
            "day": {
              "oneOf": [
                {
                  "type": "object",
                  "additionalProperties": false,
                  "required": ["type", "day"],
                  "properties": {
                    "type": { "const": "day" },
                    "day": { "type": "integer", "minimum": 1, "maximum": 31 }
                  }
                },
                {
                  "type": "object",
                  "additionalProperties": false,
                  "required": ["type", "week", "weekday"],
                  "properties": {
                    "type": { "const": "weekday" },
                    "week": { "enum": ["first", "second", "third", "fourth", "last"] },
                    "weekday": { "$ref": "#/$defs/weekday" }
                  }
                }
              ]
            },
            "start": { "$ref": "#/$defs/time_of_day" },
            "duration_minutes": { "type": "integer", "minimum": 1 }
          }
        }
      ]
    },
    "window": {
      "type": "object",
      "description": "A recurring maintenance window. One-off windows are not part of the files.",
      "additionalProperties": false,
      "required": ["name", "reason", "timing"],
      "properties": {
        "id": { "$ref": "#/$defs/uuid" },
        "name": { "$ref": "#/$defs/name" },
        "reason": { "type": "string", "minLength": 1, "maxLength": 500 },
        "stop_polling": { "type": "boolean" },
        "timing": { "$ref": "#/$defs/timing" }
      }
    },
    "windows": { "type": "array", "items": { "$ref": "#/$defs/window" } },
    "depends_on": { "type": "array", "items": { "$ref": "#/$defs/sensor_ref" } },
    "sensor": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "kind"],
      "properties": {
        "id": { "$ref": "#/$defs/uuid" },
        "name": { "$ref": "#/$defs/name" },
        "kind": { "type": "string" },
        "enabled": { "type": "boolean", "description": "Absent means true." },
        "master": { "type": "boolean", "description": "This is the device's master check." },
        "discovery_key": { "type": "string" },
        "template": { "$ref": "#/$defs/template" },
        "position": { "type": "integer" },
        "params": { "type": "object", "description": "Checked against the sensor kind's own schema on import." },
        "limits": {
          "type": "object",
          "description": "Limit overrides by channel key.",
          "additionalProperties": { "$ref": "#/$defs/limits" }
        },
        "settings": { "$ref": "#/$defs/settings" },
        "maintenance_windows": { "$ref": "#/$defs/windows" },
        "depends_on": { "$ref": "#/$defs/depends_on" }
      }
    },
    "device": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "address"],
      "properties": {
        "id": { "$ref": "#/$defs/uuid" },
        "name": { "$ref": "#/$defs/name" },
        "address": { "type": "string" },
        "hostname": { "type": "string" },
        "protocol": { "type": "string" },
        "windows_transport": { "enum": ["winrm", "wmi"] },
        "template": { "$ref": "#/$defs/template" },
        "position": { "type": "integer" },
        "settings": { "$ref": "#/$defs/settings" },
        "maintenance_windows": { "$ref": "#/$defs/windows" },
        "depends_on": { "$ref": "#/$defs/depends_on" },
        "sensors": { "type": "array", "items": { "$ref": "#/$defs/sensor" } }
      }
    },
    "group": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name"],
      "properties": {
        "id": { "$ref": "#/$defs/uuid" },
        "name": { "$ref": "#/$defs/name" },
        "position": { "type": "integer" },
        "settings": { "$ref": "#/$defs/settings" },
        "maintenance_windows": { "$ref": "#/$defs/windows" },
        "groups": { "type": "array", "items": { "$ref": "#/$defs/group" } },
        "devices": { "type": "array", "items": { "$ref": "#/$defs/device" } }
      }
    },
    "site": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name"],
      "properties": {
        "id": { "$ref": "#/$defs/uuid" },
        "local": { "type": "boolean", "description": "The installation's own site, matched without an id." },
        "name": { "$ref": "#/$defs/name" },
        "position": { "type": "integer" },
        "settings": { "$ref": "#/$defs/settings" },
        "maintenance_windows": { "$ref": "#/$defs/windows" },
        "groups": { "type": "array", "items": { "$ref": "#/$defs/group" } },
        "devices": { "type": "array", "items": { "$ref": "#/$defs/device" } }
      }
    },
    "credential": {
      "type": "object",
      "description": "A credential the files need, by name. Never a value.",
      "additionalProperties": false,
      "required": ["name", "kind"],
      "properties": {
        "name": { "type": "string" },
        "kind": { "enum": ["snmp_v2c", "snmp_v3", "ssh_password", "ssh_key", "smtp", "login", "secret", "windows", "vsphere", "aws", "radius", "tls_client"] },
        "details": { "type": "object" }
      }
    },
    "destination": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "kind"],
      "properties": {
        "id": { "$ref": "#/$defs/uuid" },
        "name": { "$ref": "#/$defs/name" },
        "kind": { "enum": ["email", "webhook", "discord", "telegram"] },
        "enabled": { "type": "boolean" },
        "config": { "type": "object" },
        "credential": { "type": "string", "description": "The name of the credential holding the secret part." }
      }
    },
    "schedule": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "time_zone", "ranges"],
      "properties": {
        "id": { "$ref": "#/$defs/uuid" },
        "name": { "$ref": "#/$defs/name" },
        "time_zone": { "type": "string" },
        "ranges": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["days", "start", "end"],
            "properties": {
              "days": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/weekday" } },
              "start": { "$ref": "#/$defs/time_of_day" },
              "end": { "$ref": "#/$defs/time_of_day" }
            }
          }
        },
        "exceptions": { "type": "array", "items": { "$ref": "#/$defs/date" } }
      }
    },
    "root": {
      "type": "object",
      "additionalProperties": false,
      "required": ["format", "format_version", "layout", "scope"],
      "properties": {
        "format": { "const": "probeward-config" },
        "format_version": { "const": 1 },
        "layout": { "enum": ["single", "directory"] },
        "scope": { "$ref": "#/$defs/scope" },
        "credentials": { "type": "array", "items": { "$ref": "#/$defs/credential" } },
        "destinations": { "type": "array", "items": { "$ref": "#/$defs/destination" } },
        "schedules": { "type": "array", "items": { "$ref": "#/$defs/schedule" } },
        "sites": { "type": "array", "items": { "$ref": "#/$defs/site" } },
        "groups": { "type": "array", "items": { "$ref": "#/$defs/group" } },
        "devices": { "type": "array", "items": { "$ref": "#/$defs/device" } }
      }
    }
  }
}
