{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://you-agent-factory.dev/schemas/ui/factory-emulator/scenario/v1",
  "title": "Factory Emulator Scenario v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "id",
    "factory",
    "seed",
    "startAt",
    "rules",
    "unmatched"
  ],
  "properties": {
    "schemaVersion": { "const": "factory-emulator-scenario/v1" },
    "id": { "$ref": "#/$defs/stableIdentity" },
    "factory": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name"],
      "properties": { "name": { "$ref": "#/$defs/nonEmptyText128" } }
    },
    "seed": { "type": "string", "minLength": 1, "maxLength": 256 },
    "startAt": {
      "type": "string",
      "format": "date-time",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}Z$"
    },
    "rules": {
      "type": "array",
      "items": { "$ref": "#/$defs/rule" }
    },
    "unmatched": { "$ref": "#/$defs/unmatched" },
    "initialSubmissions": {
      "oneOf": [
        {
          "type": "array",
          "items": { "$ref": "#/$defs/initialSubmission" }
        },
        { "$ref": "#/$defs/submissionBatch" }
      ]
    }
  },
  "$defs": {
    "stableIdentity": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._-]*$"
    },
    "nonEmptyText128": { "type": "string", "minLength": 1, "maxLength": 128 },
    "plainText": { "type": "string", "maxLength": 65536 },
    "feedbackText": { "type": "string", "maxLength": 4096 },
    "activityLabel": { "type": "string", "minLength": 1, "maxLength": 120 },
    "inputSelector": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "workType": { "$ref": "#/$defs/nonEmptyText128" },
        "state": { "$ref": "#/$defs/nonEmptyText128" },
        "name": { "$ref": "#/$defs/nonEmptyText128" }
      }
    },
    "selector": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "workstation": { "$ref": "#/$defs/nonEmptyText128" },
        "worker": { "$ref": "#/$defs/nonEmptyText128" },
        "input": { "$ref": "#/$defs/inputSelector" }
      }
    },
    "cursor": {
      "type": "object",
      "additionalProperties": false,
      "required": ["scope", "input"],
      "properties": {
        "scope": { "const": "lineage" },
        "input": { "const": "rootWorkId" }
      }
    },
    "outcome": {
      "type": "object",
      "additionalProperties": false,
      "required": ["result", "durationMs"],
      "properties": {
        "result": { "enum": ["accepted", "continued", "rejected", "failed"] },
        "durationMs": { "type": "number", "minimum": 0 },
        "output": { "$ref": "#/$defs/plainText" },
        "feedback": { "$ref": "#/$defs/feedbackText" },
        "activityLabel": { "$ref": "#/$defs/activityLabel" },
        "error": { "$ref": "#/$defs/feedbackText" }
      }
    },
    "rule": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "selector", "cursor", "outcomes", "exhaustion"],
      "properties": {
        "id": { "$ref": "#/$defs/stableIdentity" },
        "selector": { "$ref": "#/$defs/selector" },
        "cursor": { "$ref": "#/$defs/cursor" },
        "outcomes": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/outcome" }
        },
        "exhaustion": { "enum": ["repeat-last", "fail"] }
      }
    },
    "unmatched": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["behavior"],
          "properties": { "behavior": { "const": "error" } }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["behavior", "outcome"],
          "properties": {
            "behavior": { "const": "outcome" },
            "outcome": { "$ref": "#/$defs/outcome" }
          }
        }
      ]
    },
    "initialSubmission": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "workType", "state"],
      "properties": {
        "name": { "$ref": "#/$defs/stableIdentity" },
        "workType": { "$ref": "#/$defs/nonEmptyText128" },
        "state": { "$ref": "#/$defs/nonEmptyText128" },
        "input": { "$ref": "#/$defs/plainText" },
        "parent": { "$ref": "#/$defs/stableIdentity" }
      }
    },
    "submissionRelation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type", "sourceWorkName", "targetWorkName"],
      "properties": {
        "type": { "enum": ["DEPENDS_ON", "PARENT_CHILD", "SPAWNED_BY"] },
        "sourceWorkName": { "$ref": "#/$defs/stableIdentity" },
        "targetWorkName": { "$ref": "#/$defs/stableIdentity" },
        "requiredState": { "$ref": "#/$defs/nonEmptyText128" }
      }
    },
    "submissionBatch": {
      "type": "object",
      "additionalProperties": false,
      "required": ["works"],
      "properties": {
        "works": {
          "type": "array",
          "items": { "$ref": "#/$defs/initialSubmission" }
        },
        "relations": {
          "type": "array",
          "items": { "$ref": "#/$defs/submissionRelation" }
        }
      }
    }
  }
}
