{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://reachabilitylabs.org/schemas/result-to-decision-test-card/v0.2",
  "title": "Result-to-Decision Test Card v0.2",
  "description": "Canonical pre-action contract for mapping test-result branches to decisions, path updates, and execution effects.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "instrument_type",
    "record_status",
    "card_id",
    "revision",
    "case_binding",
    "protocol_ref",
    "target_ref",
    "governing_decision",
    "no_test_action",
    "mandatory_test_basis",
    "branches",
    "measurement_readiness",
    "run_impact",
    "authority",
    "seal"
  ],
  "properties": {
    "schema_version": { "const": "0.2" },
    "instrument_type": { "const": "result_to_decision_test_card" },
    "record_status": { "enum": ["draft", "sealed"] },
    "card_id": { "type": "string", "minLength": 1, "maxLength": 128 },
    "revision": { "type": "string", "minLength": 1, "maxLength": 32 },
    "case_binding": { "$ref": "#/$defs/case_binding" },
    "protocol_ref": { "type": "string", "minLength": 1, "maxLength": 512 },
    "target_ref": { "type": "string", "minLength": 1, "maxLength": 512 },
    "decision_due": { "type": ["string", "null"], "format": "date-time" },
    "planned_start": { "type": ["string", "null"], "format": "date-time" },
    "governing_decision": { "type": "string", "minLength": 1, "maxLength": 4000 },
    "no_test_action": { "type": "string", "minLength": 1, "maxLength": 4000 },
    "mandatory_test_basis": { "type": ["string", "null"], "maxLength": 4000 },
    "branches": {
      "type": "array",
      "minItems": 3,
      "maxItems": 12,
      "contains": {
        "type": "object",
        "required": ["inconclusive"],
        "properties": { "inconclusive": { "const": true } }
      },
      "items": { "$ref": "#/$defs/result_branch" }
    },
    "measurement_readiness": {
      "type": "object",
      "additionalProperties": false,
      "required": ["ready", "quality_gate", "stop_conditions"],
      "properties": {
        "ready": { "type": "boolean" },
        "quality_gate": { "type": "string", "minLength": 1, "maxLength": 4000 },
        "stop_conditions": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "string", "minLength": 1, "maxLength": 2000 }
        }
      }
    },
    "run_impact": {
      "type": "object",
      "additionalProperties": false,
      "required": ["direct_consumption", "path_effects", "preservation_actions"],
      "properties": {
        "direct_consumption": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/consumption_item" }
        },
        "path_effects": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/path_effect" }
        },
        "preservation_actions": {
          "type": "array",
          "items": { "type": "string", "minLength": 1, "maxLength": 2000 }
        }
      }
    },
    "authority": {
      "type": "object",
      "additionalProperties": false,
      "required": ["test_owner", "decision_authority"],
      "properties": {
        "test_owner": { "type": "string", "minLength": 1, "maxLength": 256 },
        "decision_authority": { "type": "string", "minLength": 1, "maxLength": 256 }
      }
    },
    "seal": { "oneOf": [{ "$ref": "#/$defs/seal" }, { "type": "null" }] }
  },
  "allOf": [
    {
      "if": { "properties": { "record_status": { "const": "draft" } } },
      "then": { "properties": { "seal": { "type": "null" } } }
    },
    {
      "if": { "properties": { "record_status": { "const": "sealed" } } },
      "then": { "properties": { "seal": { "$ref": "#/$defs/seal" } } }
    }
  ],
  "$defs": {
    "case_binding": {
      "type": "object",
      "additionalProperties": false,
      "required": ["case_id", "case_revision"],
      "properties": {
        "case_id": { "type": "string", "minLength": 1, "maxLength": 128 },
        "case_revision": { "type": "string", "minLength": 1, "maxLength": 64 },
        "contract_refs": {
          "type": "array",
          "items": { "type": "string", "minLength": 1, "maxLength": 512 }
        }
      }
    },
    "path_standing": {
      "enum": ["open", "conditional", "unknown", "assessed_unreachable", "exactly_unreachable"]
    },
    "recovery_status": {
      "enum": ["none_identified", "candidate", "supported", "verified"]
    },
    "path_effect_value": {
      "enum": ["opens", "preserves", "narrows", "conditions", "forecloses", "reopens", "unknown"]
    },
    "path_update": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path_ref", "path_standing_if_observed", "recovery_status_if_observed", "basis"],
      "properties": {
        "path_ref": { "type": "string", "minLength": 1, "maxLength": 256 },
        "path_standing_if_observed": { "$ref": "#/$defs/path_standing" },
        "recovery_status_if_observed": { "$ref": "#/$defs/recovery_status" },
        "basis": { "type": "string", "minLength": 1, "maxLength": 4000 }
      }
    },
    "result_branch": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "branch_id",
        "result_predicate",
        "predicted_observation",
        "path_updates",
        "decision_disposition",
        "decision_consequence",
        "next_action",
        "inconclusive"
      ],
      "properties": {
        "branch_id": { "type": "string", "minLength": 1, "maxLength": 64 },
        "result_predicate": { "type": "string", "minLength": 1, "maxLength": 4000 },
        "predicted_observation": { "type": "string", "minLength": 1, "maxLength": 4000 },
        "path_updates": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/path_update" }
        },
        "decision_disposition": {
          "enum": ["proceed", "hold", "redesign", "stop", "repeat", "release", "reject", "mandatory_verification_only", "other"]
        },
        "decision_consequence": { "type": "string", "minLength": 1, "maxLength": 4000 },
        "next_action": { "type": "string", "minLength": 1, "maxLength": 4000 },
        "inconclusive": { "type": "boolean" }
      }
    },
    "consumption_item": {
      "type": "object",
      "additionalProperties": false,
      "required": ["category", "description"],
      "properties": {
        "category": {
          "enum": ["none_identified", "specimen", "time", "budget", "safety_margin", "component_life", "configuration_state", "evidence_lineage", "access_window", "qualification_window", "other"]
        },
        "description": { "type": "string", "minLength": 1, "maxLength": 2000 },
        "quantity": { "type": ["number", "null"] },
        "unit": { "type": ["string", "null"], "maxLength": 64 }
      }
    },
    "path_effect": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path_ref", "effect", "mechanism"],
      "properties": {
        "path_ref": { "type": "string", "minLength": 1, "maxLength": 256 },
        "effect": { "$ref": "#/$defs/path_effect_value" },
        "mechanism": { "type": "string", "minLength": 1, "maxLength": 4000 }
      }
    },
    "seal": {
      "type": "object",
      "additionalProperties": false,
      "required": ["algorithm", "hash", "sealed_at", "sealed_by"],
      "properties": {
        "algorithm": { "const": "sha256-canonical-json-v1" },
        "hash": { "type": "string", "pattern": "^[0-9a-f]{64}$" },
        "sealed_at": { "type": "string", "format": "date-time" },
        "sealed_by": { "type": "string", "minLength": 1, "maxLength": 256 }
      }
    }
  }
}
