{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://capsule.spec/response/v0.1.0",
  "title": "Capsule Response v0.1.0",
  "description": "Schema for response/feedback files exported from a Capsule.",
  "type": "object",
  "required": ["response_schema_version", "capsule_reference", "response"],
  "properties": {
    "response_schema_version": {
      "type": "string",
      "pattern": "^\\d+\\.\\d+\\.\\d+$"
    },
    "capsule_reference": {
      "type": "object",
      "required": ["artifact_id", "artifact_version", "uuid", "snapshot_id"],
      "properties": {
        "artifact_id": {
          "type": "string",
          "pattern": "^artifact:.+$"
        },
        "artifact_version": {
          "type": "string",
          "pattern": "^\\d+\\.\\d+\\.\\d+$"
        },
        "uuid": {
          "type": "string",
          "format": "uuid"
        },
        "snapshot_id": {
          "type": "string",
          "pattern": "^snapshot:.+$"
        }
      },
      "additionalProperties": false
    },
    "response": {
      "type": "object",
      "required": ["type", "created_at", "payload"],
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "annotation",
            "ranking",
            "selection",
            "decision",
            "feedback",
            "form_data",
            "freeform",
            "patch"
          ]
        },
        "created_at": {
          "type": "string",
          "format": "date-time"
        },
        "created_by": {
          "type": "string",
          "maxLength": 200
        },
        "payload": {
          "oneOf": [
            { "$ref": "#/$defs/annotation_payload" },
            { "$ref": "#/$defs/ranking_payload" },
            { "$ref": "#/$defs/selection_payload" },
            { "$ref": "#/$defs/decision_payload" },
            { "$ref": "#/$defs/feedback_payload" },
            { "$ref": "#/$defs/form_data_payload" },
            { "$ref": "#/$defs/freeform_payload" },
            { "$ref": "#/$defs/patch_payload" }
          ]
        }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false,

  "$defs": {
    "annotation_payload": {
      "type": "object",
      "required": ["record_id", "note"],
      "properties": {
        "record_id": { "type": "string" },
        "note": { "type": "string", "maxLength": 10000 },
        "field": { "type": "string" }
      },
      "additionalProperties": false
    },
    "ranking_payload": {
      "type": "object",
      "required": ["ranked_items"],
      "properties": {
        "ranked_items": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["record_id", "rank"],
            "properties": {
              "record_id": { "type": "string" },
              "rank": { "type": "integer", "minimum": 1 }
            },
            "additionalProperties": false
          }
        }
      },
      "additionalProperties": false
    },
    "selection_payload": {
      "type": "object",
      "required": ["selected"],
      "properties": {
        "selected": {
          "type": "array",
          "items": { "type": "string" }
        },
        "reason": { "type": "string", "maxLength": 5000 }
      },
      "additionalProperties": false
    },
    "decision_payload": {
      "type": "object",
      "anyOf": [
        { "required": ["decision"] },
        { "required": ["decisions"] }
      ],
      "properties": {
        "decision": {
          "type": "string",
          "enum": ["approved", "rejected", "deferred", "conditional"],
          "description": "Single overall decision about the artifact."
        },
        "decisions": {
          "type": "array",
          "description": "Per-record decisions, used by templates like decision_board. An entry must include record_id and at least one of verdict or note (a recipient may comment on a record without rendering a verdict, or may render a verdict without commenting).",
          "items": {
            "type": "object",
            "required": ["record_id"],
            "anyOf": [
              { "required": ["verdict"] },
              { "required": ["note"] }
            ],
            "properties": {
              "record_id": { "type": "string" },
              "verdict": {
                "type": "string",
                "enum": ["approve", "reject", "defer", "skip"]
              },
              "note": { "type": "string", "maxLength": 5000 }
            },
            "additionalProperties": false
          }
        },
        "summary_verdict": {
          "type": "string",
          "enum": ["approved", "rejected", "deferred", "conditional"],
          "description": "Overall verdict when decisions array is also present."
        },
        "summary_notes": { "type": "string", "maxLength": 10000 },
        "conditions": { "type": "string", "maxLength": 5000 },
        "notes": { "type": "string", "maxLength": 10000 }
      },
      "additionalProperties": false
    },
    "feedback_payload": {
      "type": "object",
      "description": "Flexible feedback shape. Recommended fields below; arbitrary additional fields are permitted because feedback takes many forms (a structured form, a rating, a multi-question response, etc.).",
      "properties": {
        "rating": { "type": "integer", "minimum": 1, "maximum": 5 },
        "comments": { "type": "string", "maxLength": 10000 },
        "suggestions": {
          "type": "array",
          "items": { "type": "string", "maxLength": 2000 }
        },
        "position": { "type": "string", "maxLength": 200 },
        "most_important_issue": { "type": "string", "maxLength": 200 },
        "notes": { "type": "string", "maxLength": 10000 }
      },
      "additionalProperties": true
    },
    "form_data_payload": {
      "type": "object",
      "required": ["fields"],
      "properties": {
        "fields": {
          "type": "object",
          "additionalProperties": {
            "type": ["string", "number", "boolean", "null"]
          }
        }
      },
      "additionalProperties": false
    },
    "freeform_payload": {
      "type": "object",
      "required": ["content"],
      "properties": {
        "content": { "type": "string", "maxLength": 50000 },
        "format": {
          "type": "string",
          "enum": ["plaintext", "markdown"],
          "default": "plaintext"
        }
      },
      "additionalProperties": false
    },
    "patch_payload": {
      "type": "object",
      "required": ["operations"],
      "description": "JSON Patch (RFC 6902) operations proposing changes to the capsule's data. The recipient is suggesting edits; the author reviews before applying.",
      "properties": {
        "operations": {
          "type": "array",
          "maxItems": 1000,
          "items": {
            "type": "object",
            "required": ["op", "path"],
            "properties": {
              "op": {
                "type": "string",
                "enum": ["add", "remove", "replace", "move", "copy", "test"]
              },
              "path": { "type": "string" },
              "from": { "type": "string" },
              "value": {}
            },
            "additionalProperties": false
          }
        },
        "summary": {
          "type": "string",
          "maxLength": 5000,
          "description": "Optional explanation of what the patch changes and why."
        }
      },
      "additionalProperties": false
    }
  }
}
