{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://ydalen.ru/schema/agent-door-script-v1.json",
  "title": "Ydalenka Agent Door Scenario v1",
  "type": "object",
  "additionalProperties": false,
  "required": ["version", "scenarioId", "targetId", "schedule", "steps"],
  "properties": {
    "version": { "const": 1 },
    "scenarioId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128,
      "pattern": "^[^<>:\"/\\\\|?*]+$",
      "description": "Имя сценария и файла; допускаются кириллица и пробелы"
    },
    "targetId": { "type": "string", "pattern": "^[0-9]{1,10}$" },
    "createdBy": { "type": "string", "maxLength": 254 },
    "enabled": { "type": "boolean", "default": true },
    "schedule": { "$ref": "#/$defs/schedule" },
    "roots": {
      "type": "object", "additionalProperties": false,
      "properties": {
        "local": { "$ref": "#/$defs/windowsPath" },
        "remote": { "$ref": "#/$defs/windowsPath" }
      }
    },
    "steps": {
      "type": "array", "minItems": 1, "maxItems": 100,
      "items": { "$ref": "#/$defs/step" }
    }
  },
  "$defs": {
    "windowsPath": { "type": "string", "pattern": "^[A-Za-z]:\\\\", "maxLength": 32767 },
    "schedule": {
      "oneOf": [
        {
          "type": "object", "additionalProperties": false,
          "required": ["kind"],
          "properties": { "kind": { "const": "manual" } }
        },
        {
          "type": "object", "additionalProperties": false,
          "required": ["kind", "runAt"],
          "properties": {
            "kind": { "const": "once" },
            "runAt": { "type": "string", "format": "date-time" },
            "expiresAt": { "type": "string", "format": "date-time" }
          }
        },
        {
          "type": "object", "additionalProperties": false,
          "required": ["kind", "everyMinutes"],
          "properties": {
            "kind": { "const": "interval" },
            "everyMinutes": { "type": "integer", "minimum": 1, "maximum": 10080 },
            "startAt": { "type": "string", "format": "date-time" },
            "expiresAt": { "type": "string", "format": "date-time" }
          }
        },
        {
          "type": "object", "additionalProperties": false,
          "required": ["kind", "localTime", "timeZone"],
          "properties": {
            "kind": { "const": "daily" },
            "localTime": { "type": "string", "pattern": "^([01][0-9]|2[0-3]):[0-5][0-9]$" },
            "timeZone": { "type": "string", "minLength": 1, "maxLength": 128 },
            "expiresAt": { "type": "string", "format": "date-time" }
          }
        },
        {
          "type": "object", "additionalProperties": false,
          "required": ["kind", "days", "localTime", "timeZone"],
          "properties": {
            "kind": { "const": "weekly" },
            "days": { "type": "array", "minItems": 1, "uniqueItems": true,
              "items": { "enum": ["mon", "tue", "wed", "thu", "fri", "sat", "sun"] } },
            "localTime": { "type": "string", "pattern": "^([01][0-9]|2[0-3]):[0-5][0-9]$" },
            "timeZone": { "type": "string", "minLength": 1, "maxLength": 128 },
            "expiresAt": { "type": "string", "format": "date-time" }
          }
        }
      ]
    },
    "step": {
      "type": "object", "additionalProperties": true,
      "required": ["operation"],
      "anyOf": [
        { "required": ["id"] },
        { "required": ["requestId"] }
      ],
      "properties": {
        "id": { "type": "string", "minLength": 1, "maxLength": 128 },
        "requestId": { "type": "string", "minLength": 1, "maxLength": 128 },
        "retryCount": { "type": "integer", "minimum": 0, "maximum": 5 },
        "retryDelayMs": { "type": "integer", "minimum": 0, "maximum": 60000 },
        "onError": { "enum": ["stop", "continue"] },
        "whenPreviousOk": { "type": "boolean" },
        "operation": {
          "enum": ["capabilities", "fs.roots", "fs.list", "fs.stat", "fs.read_text", "fs.search_text", "fs.find", "process.list", "process.query", "process.stop", "process.start", "network.tcp_probe", "service.query", "service.start", "service.stop", "service.restart", "service.ensure_port", "sync.manifest", "sync.read_chunk", "sync.write_begin", "sync.write_chunk", "sync.write_commit", "sync.ensure_dir", "local.sync.manifest", "local.sync.read_chunk", "local.sync.write_begin", "local.sync.write_chunk", "local.sync.write_commit", "local.sync.ensure_dir", "sync.bidirectional", "sync.upload_changed", "sync.upload_file", "sync.replace_running_exe", "package.verify_signature", "package.install_msi"]
        }
      }
    }
  }
}
