{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:sun-terminal:north-door:receipt:1",
  "title": "Sun Terminal North Door encounter receipt v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "encounter",
    "draw_facts",
    "provenance",
    "rendering",
    "interpretations",
    "policy"
  ],
  "properties": {
    "schema_version": {
      "const": "sun-terminal.encounter-receipt@1"
    },
    "encounter": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "status",
        "committed_at",
        "finalized_at"
      ],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^enc_[0-9a-f]{32}$"
        },
        "status": {
          "const": "final"
        },
        "committed_at": {
          "$ref": "#/$defs/timestamp"
        },
        "finalized_at": {
          "$ref": "#/$defs/timestamp"
        }
      }
    },
    "draw_facts": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "instrument",
        "raw_result"
      ],
      "properties": {
        "instrument": {
          "$ref": "#/$defs/instrument"
        },
        "raw_result": {
          "$ref": "#/$defs/raw_result"
        }
      }
    },
    "provenance": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "request_fingerprint",
        "instrument_manifest",
        "kernel",
        "deck",
        "entropy",
        "runtime"
      ],
      "properties": {
        "request_fingerprint": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "algorithm",
            "canonicalization",
            "value"
          ],
          "properties": {
            "algorithm": {
              "const": "sha-256"
            },
            "canonicalization": {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "id",
                "version"
              ],
              "properties": {
                "id": {
                  "const": "sun-terminal-consult-request-json"
                },
                "version": {
                  "const": 1
                }
              }
            },
            "value": {
              "$ref": "#/$defs/sha256"
            }
          }
        },
        "instrument_manifest": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "id",
            "version",
            "digest",
            "deck_digest",
            "kernel_profile_digest"
          ],
          "properties": {
            "id": {
              "const": "tarot.rider-waite"
            },
            "version": {
              "const": 1
            },
            "digest": {
              "$ref": "#/$defs/digest"
            },
            "deck_digest": {
              "$ref": "#/$defs/digest"
            },
            "kernel_profile_digest": {
              "$ref": "#/$defs/digest"
            }
          }
        },
        "kernel": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "identity",
            "profile_digest"
          ],
          "properties": {
            "identity": {
              "$ref": "#/$defs/kernel_identity"
            },
            "profile_digest": {
              "$ref": "#/$defs/digest"
            }
          }
        },
        "deck": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "identity",
            "content_digest"
          ],
          "properties": {
            "identity": {
              "$ref": "#/$defs/deck_identity"
            },
            "content_digest": {
              "$ref": "#/$defs/digest"
            }
          }
        },
        "entropy": {
          "const": {
            "source": "web-crypto-get-random-values",
            "bits": 256,
            "draw_samples": 1,
            "seed_disclosure": "not-disclosed",
            "seed_persistence": "not-persisted",
            "independent_noncuration_proof": false
          }
        },
        "runtime": {
          "const": {
            "id": "sun-terminal-worker",
            "version": 1,
            "authenticity": "unsigned-service-assertion",
            "independent_origin_verification": false
          }
        }
      }
    },
    "rendering": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "instrument_url"
      ],
      "properties": {
        "instrument_url": {
          "type": "string",
          "pattern": "^https://[^\\s]+/instruments/tarot-rider-waite$"
        }
      }
    },
    "interpretations": {
      "type": "array",
      "maxItems": 0
    },
    "policy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "replay",
        "privacy",
        "retention"
      ],
      "properties": {
        "replay": {
          "const": {
            "classification": "stored-final-result-retrieval-only",
            "independent_exact_replay": false,
            "terminal_exact_rederivation": false
          }
        },
        "privacy": {
          "const": {
            "question_text_accepted": false,
            "question_text_retained": false,
            "seed_disclosed": false,
            "encounter_public": false,
            "receipt_payload_in_telemetry": false
          }
        },
        "retention": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "profile",
            "active_record_expires_at",
            "retained",
            "not_retained"
          ],
          "properties": {
            "profile": {
              "const": "anonymous-idempotency-24h"
            },
            "active_record_expires_at": {
              "$ref": "#/$defs/timestamp"
            },
            "retained": {
              "const": [
                "idempotency-key-digest",
                "request-fingerprint",
                "resolved-manifest",
                "final-response"
              ]
            },
            "not_retained": {
              "const": [
                "raw-idempotency-key",
                "question-text",
                "draw-seed",
                "auth-headers"
              ]
            }
          }
        }
      }
    }
  },
  "$defs": {
    "timestamp": {
      "type": "string",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}Z$"
    },
    "sha256": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "digest": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "algorithm",
        "value"
      ],
      "properties": {
        "algorithm": {
          "const": "sha-256"
        },
        "value": {
          "$ref": "#/$defs/sha256"
        }
      }
    },
    "instrument": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "version"
      ],
      "properties": {
        "id": {
          "const": "tarot.rider-waite"
        },
        "version": {
          "const": 1
        }
      }
    },
    "structural_inputs": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "count",
        "position_labels"
      ],
      "properties": {
        "count": {
          "type": "integer",
          "minimum": 1,
          "maximum": 78
        },
        "position_labels": {
          "oneOf": [
            {
              "type": "null"
            },
            {
              "type": "array",
              "minItems": 1,
              "maxItems": 78,
              "items": {
                "type": "string",
                "pattern": "^position-([1-9]|[1-6][0-9]|7[0-8])$"
              }
            }
          ]
        }
      }
    },
    "position": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "position",
        "card_id",
        "orientation"
      ],
      "properties": {
        "position": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "index",
            "label"
          ],
          "properties": {
            "index": {
              "type": "integer",
              "minimum": 1,
              "maximum": 78
            },
            "label": {
              "oneOf": [
                {
                  "type": "null"
                },
                {
                  "type": "string",
                  "pattern": "^position-([1-9]|[1-6][0-9]|7[0-8])$"
                }
              ]
            }
          }
        },
        "card_id": {
          "type": "string",
          "pattern": "^(major-(0[0-9]|1[0-9]|2[01])|(wands|cups|swords|pentacles)-(0[1-9]|1[0-4]))$"
        },
        "orientation": {
          "enum": [
            "upright",
            "reversed"
          ]
        }
      }
    },
    "kernel_identity": {
      "const": {
        "id": "committed-permutation",
        "version": 2
      }
    },
    "deck_identity": {
      "const": {
        "id": "rider-waite",
        "version": 1
      }
    },
    "derivation": {
      "const": {
        "id": "hkdf-sha256-counter-stream",
        "version": 1,
        "hash": "sha-256",
        "seedEncoding": "32-byte-raw-from-64-lowercase-hex",
        "saltUtf8": "sun-terminal/tarot/hkdf-sha256-counter-stream/v1",
        "shuffleInfoPrefixUtf8": "sun-terminal/tarot/committed-permutation@2/rider-waite@1/shuffle/fisher-yates-rejection@1",
        "orientationInfoPrefixUtf8": "sun-terminal/tarot/committed-permutation@2/rider-waite@1/orientation/position-word-parity@1",
        "infoEncoding": "utf8-prefix-nul-uint32-be-page-counter",
        "shuffleStream": "shuffle",
        "orientationStream": "orientation",
        "pageBytes": 512,
        "pageCounterStart": 0,
        "wordEncoding": "uint32-be"
      }
    },
    "permutation_commitment": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "kernel",
        "deck",
        "derivation",
        "shuffle",
        "orientation",
        "canonicalization",
        "permutationHash"
      ],
      "properties": {
        "kernel": {
          "$ref": "#/$defs/kernel_identity"
        },
        "deck": {
          "$ref": "#/$defs/deck_identity"
        },
        "derivation": {
          "$ref": "#/$defs/derivation"
        },
        "shuffle": {
          "const": {
            "id": "fisher-yates-rejection",
            "version": 1
          }
        },
        "orientation": {
          "const": {
            "id": "position-word-parity",
            "version": 1
          }
        },
        "canonicalization": {
          "const": {
            "id": "sun-terminal-tarot-permutation-json",
            "version": 2
          }
        },
        "permutationHash": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "algorithm",
            "value"
          ],
          "properties": {
            "algorithm": {
              "const": "sha-256"
            },
            "value": {
              "$ref": "#/$defs/sha256"
            }
          }
        }
      }
    },
    "raw_result": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schema_version",
        "structural_inputs",
        "positions",
        "permutation_commitment",
        "raw_result_hash"
      ],
      "properties": {
        "schema_version": {
          "const": "sun-terminal.tarot.raw-result@1"
        },
        "structural_inputs": {
          "$ref": "#/$defs/structural_inputs"
        },
        "positions": {
          "type": "array",
          "minItems": 1,
          "maxItems": 78,
          "items": {
            "$ref": "#/$defs/position"
          }
        },
        "permutation_commitment": {
          "$ref": "#/$defs/permutation_commitment"
        },
        "raw_result_hash": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "algorithm",
            "canonicalization",
            "value"
          ],
          "properties": {
            "algorithm": {
              "const": "sha-256"
            },
            "canonicalization": {
              "const": {
                "id": "sun-terminal-tarot-raw-result-json",
                "version": 1
              }
            },
            "value": {
              "$ref": "#/$defs/sha256"
            }
          }
        }
      }
    }
  }
}
