{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kabinethq.com/protocol/1/actions.schema.json",
  "title": "Actions",
  "description": "What actions answer, and queued runs (`GET /panel/runs/{run}`).",
  "$defs": {
    "Effect": {
      "title": "Effect",
      "description": "What the client does after an action: say something, or open a record (its view page if it has one).",
      "type": "object",
      "required": ["type"],
      "discriminator": { "propertyName": "type" },
      "oneOf": [{ "$ref": "#/$defs/MessageEffect" }, { "$ref": "#/$defs/OpenEffect" }]
    },
    "MessageEffect": {
      "title": "MessageEffect",
      "type": "object",
      "required": ["type", "message"],
      "properties": {
        "type": { "const": "message" },
        "message": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },
    "OpenEffect": {
      "title": "OpenEffect",
      "type": "object",
      "required": ["type", "resource", "record"],
      "properties": {
        "type": { "const": "open" },
        "resource": { "type": "string", "minLength": 1 },
        "record": { "$ref": "common.schema.json#/$defs/Key" }
      },
      "additionalProperties": false
    },
    "RecordActionResult": {
      "title": "RecordActionResult",
      "description": "A record action: the record after it, and `_effect` when the client has something to do.",
      "type": "object",
      "allOf": [{ "$ref": "records.schema.json#/$defs/RecordFields" }],
      "properties": {
        "_effect": { "$ref": "#/$defs/Effect" }
      },
      "unevaluatedProperties": false
    },
    "ActionResult": {
      "title": "ActionResult",
      "description": "`POST /panel/resources/{r}/actions/{name}` answering 200: a bulk action's report, or a standalone action's effect.",
      "oneOf": [{ "$ref": "#/$defs/BulkResult" }, { "$ref": "#/$defs/StandaloneResult" }]
    },
    "StandaloneResult": {
      "title": "StandaloneResult",
      "type": "object",
      "properties": {
        "effect": { "$ref": "#/$defs/Effect" }
      },
      "additionalProperties": false
    },
    "BulkResult": {
      "title": "BulkResult",
      "type": "object",
      "required": ["total", "succeeded", "failed", "failures"],
      "properties": {
        "total": { "type": "integer", "minimum": 0 },
        "succeeded": { "type": "integer", "minimum": 0 },
        "failed": { "type": "integer", "minimum": 0 },
        "failures": { "type": "array", "items": { "$ref": "#/$defs/Failure" } }
      },
      "additionalProperties": false
    },
    "Failure": {
      "title": "Failure",
      "oneOf": [{ "$ref": "#/$defs/RecordFailure" }, { "$ref": "#/$defs/RowFailure" }]
    },
    "RecordFailure": {
      "title": "RecordFailure",
      "description": "A record the action skipped. `id` echoes the request, so it may not be a key the resource has. `reason`: `not_found`, `forbidden`, `in_use`, `deleted`, `not_deleted`, `error`, or a handler's own 4xx text.",
      "type": "object",
      "required": ["id", "reason"],
      "properties": {
        "id": { "type": "string" },
        "reason": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },
    "RowFailure": {
      "title": "RowFailure",
      "description": "An import's skipped row: its number in the file (the header is row 1) and the errors a form would show.",
      "type": "object",
      "required": ["row", "errors"],
      "properties": {
        "row": { "type": "integer", "minimum": 2 },
        "errors": { "$ref": "common.schema.json#/$defs/Errors" }
      },
      "additionalProperties": false
    },
    "RunStatus": {
      "title": "RunStatus",
      "enum": ["queued", "running", "completed", "failed"]
    },
    "Accepted": {
      "title": "Accepted",
      "description": "A queued action's 202: the run to poll, just queued.",
      "type": "object",
      "required": ["run", "status"],
      "properties": {
        "run": { "$ref": "common.schema.json#/$defs/Key" },
        "status": { "const": "queued" }
      },
      "additionalProperties": false
    },
    "Run": {
      "title": "Run",
      "description": "A queued action's run: its bulk report so far. `file`: what it produced (an export), once completed.",
      "type": "object",
      "required": ["id", "action", "status", "total", "succeeded", "failed", "failures"],
      "properties": {
        "id": { "$ref": "common.schema.json#/$defs/Key" },
        "action": { "type": "string", "minLength": 1 },
        "status": { "$ref": "#/$defs/RunStatus" },
        "total": { "type": "integer", "minimum": 0 },
        "succeeded": { "type": "integer", "minimum": 0 },
        "failed": { "type": "integer", "minimum": 0 },
        "failures": { "type": "array", "items": { "$ref": "#/$defs/Failure" } },
        "file": {
          "type": "object",
          "required": ["name", "url"],
          "properties": {
            "name": { "type": "string", "minLength": 1 },
            "url": { "type": "string", "pattern": "^/panel/runs/[^/]+/file$" }
          },
          "additionalProperties": false
        }
      },
      "additionalProperties": false,
      "if": { "required": ["file"] },
      "then": { "properties": { "status": { "const": "completed" } } }
    }
  }
}
