{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://paa.dev/paa-decision-artifact.schema.json",
  "title": "PAA Decision Artifact",
  "description": "Immutable, capped admissibility snapshot backing one promotion or demotion decision for a PAA task.",
  "x-paa-schema-version": "paa-decision-artifact/0.1.0-draft",
  "type": "object",
  "required": [
    "artifact_schema",
    "artifact_id",
    "task",
    "declaration_version",
    "scope",
    "decision",
    "policy",
    "window",
    "population",
    "included_evidence",
    "excluded_evidence",
    "generated_at",
    "source_references"
  ],
  "additionalProperties": false,
  "definitions": {
    "sha256": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "observed_window": {
      "description": "The declared task window plus the exact observed range this snapshot covers.",
      "oneOf": [
        {
          "type": "object",
          "required": ["kind", "size", "observed_from", "observed_to"],
          "additionalProperties": false,
          "properties": {
            "kind": { "type": "string", "enum": ["cases"] },
            "size": { "type": "integer", "minimum": 1 },
            "observed_from": { "type": "string", "format": "date-time" },
            "observed_to": { "type": "string", "format": "date-time" }
          }
        },
        {
          "type": "object",
          "required": ["kind", "size", "observed_from", "observed_to"],
          "additionalProperties": false,
          "properties": {
            "kind": { "type": "string", "enum": ["duration"] },
            "size": { "type": "string", "pattern": "^P(?!$).+$" },
            "observed_from": { "type": "string", "format": "date-time" },
            "observed_to": { "type": "string", "format": "date-time" }
          }
        }
      ]
    }
  },
  "properties": {
    "artifact_schema": {
      "type": "string",
      "const": "paa-decision-artifact/0.1.0-draft"
    },
    "artifact_id": {
      "type": "string",
      "minLength": 1
    },
    "task": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_]*$",
      "minLength": 1
    },
    "declaration_version": {
      "type": "integer",
      "minimum": 1
    },
    "scope": {
      "type": ["string", "null"],
      "minLength": 1
    },
    "decision": {
      "type": "object",
      "required": ["kind", "from_position", "to_position", "outcome"],
      "additionalProperties": false,
      "properties": {
        "kind": { "type": "string", "enum": ["promotion", "demotion"] },
        "from_position": {
          "type": "string",
          "enum": ["manual", "hitl", "hotl", "autonomous"]
        },
        "to_position": {
          "type": "string",
          "enum": ["manual", "hitl", "hotl", "autonomous"]
        },
        "outcome": { "type": "string", "enum": ["eligible", "ineligible"] }
      }
    },
    "policy": {
      "type": "object",
      "description": "The declared promotion.report or demotion.trigger identity and version that produced this artifact.",
      "required": ["id", "version"],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "version": { "type": "string", "minLength": 1 }
      }
    },
    "window": { "$ref": "#/definitions/observed_window" },
    "population": {
      "type": "object",
      "required": ["limit", "total_qualifying", "snapshot_at"],
      "additionalProperties": false,
      "properties": {
        "limit": { "type": "integer", "minimum": 1 },
        "total_qualifying": { "type": "integer", "minimum": 0 },
        "snapshot_at": { "type": "string", "format": "date-time" }
      }
    },
    "included_evidence": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["evidence_ref", "evidence_sha256"],
        "additionalProperties": false,
        "properties": {
          "evidence_ref": { "type": "string", "minLength": 1 },
          "evidence_sha256": { "$ref": "#/definitions/sha256" }
        }
      }
    },
    "excluded_evidence": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["evidence_ref", "evidence_sha256", "reason"],
        "additionalProperties": false,
        "properties": {
          "evidence_ref": { "type": "string", "minLength": 1 },
          "evidence_sha256": { "$ref": "#/definitions/sha256" },
          "reason": { "type": "string", "minLength": 1 }
        }
      }
    },
    "generated_at": { "type": "string", "format": "date-time" },
    "source_references": {
      "type": "array",
      "minItems": 1,
      "uniqueItems": true,
      "items": { "type": "string", "minLength": 1 }
    }
  }
}
