{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://masidyn.com/schemas/active-handoff/receiver-profile-v1.schema.json",
  "title": "Active Handoff receiver profile",
  "description": "The contract a crisis-receiving organization must satisfy to be onboarded. Declares an endpoint, an authentication METHOD (never a secret value), consent semantics, and a status mapping. A profile is a description of a real accepted receiver; it confers no capability and grants no connection by itself.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "receiver_id",
    "organization",
    "jurisdiction",
    "endpoint",
    "authentication",
    "consent",
    "status_mapping",
    "capabilities",
    "attestation"
  ],
  "properties": {
    "schema_version": { "const": "active-handoff-receiver-profile/v1" },
    "receiver_id": { "type": "string", "pattern": "^[a-z0-9][a-z0-9._-]{2,63}$" },
    "organization": { "type": "string", "pattern": "^[a-zA-Z0-9 .,'&()_-]{2,160}$" },
    "jurisdiction": {
      "description": "Coarse jurisdiction only, matching the aggregate geography vocabulary. No street address, no precise coordinates.",
      "type": "string",
      "pattern": "^(GLOBAL|[A-Z]{2}|[A-Z]{2}-[A-Z0-9]{1,3})$"
    },
    "endpoint": {
      "type": "object",
      "additionalProperties": false,
      "required": ["url", "protocol", "timeout_seconds"],
      "properties": {
        "url": {
          "description": "HTTPS only. A cleartext receiver endpoint is never acceptable for crisis traffic.",
          "type": "string",
          "pattern": "^https://[a-zA-Z0-9.-]+(:[0-9]{2,5})?(/[A-Za-z0-9._~/-]*)?$"
        },
        "protocol": { "enum": ["https-json", "https-fhir", "sip-tls"] },
        "timeout_seconds": { "type": "integer", "minimum": 1, "maximum": 120 }
      }
    },
    "authentication": {
      "type": "object",
      "additionalProperties": false,
      "required": ["method", "credential_env_var"],
      "properties": {
        "method": { "enum": ["oauth2-client-credentials", "mtls", "bearer-token", "hmac-signature"] },
        "credential_env_var": {
          "description": "The NAME of the environment variable holding the runtime credential. Never the value. There is no universal key; each receiver supplies its own.",
          "type": "string",
          "pattern": "^[A-Z][A-Z0-9_]{2,63}$"
        },
        "token_url": { "type": "string", "pattern": "^https://[a-zA-Z0-9.-]+(:[0-9]{2,5})?(/[A-Za-z0-9._~/-]*)?$" },
        "rotation_days": { "type": "integer", "minimum": 1, "maximum": 730 }
      }
    },
    "consent": {
      "type": "object",
      "additionalProperties": false,
      "required": ["basis", "in_chat_prompt_inserted", "record_retention_days"],
      "properties": {
        "basis": {
          "description": "Invariant 1: consent is ordinary signup Terms/Privacy acceptance. No new in-chat consent prompt is inserted when the floor fires.",
          "enum": ["signup-terms-privacy", "prior-clinical-relationship", "statutory"]
        },
        "in_chat_prompt_inserted": { "const": false },
        "record_retention_days": { "type": "integer", "minimum": 0, "maximum": 3650 }
      }
    },
    "status_mapping": {
      "description": "Invariant 3: accepted, assigned and connected are DISTINCT. Every one must map to a receiver-side status; none may be inferred from another.",
      "type": "object",
      "additionalProperties": false,
      "required": ["accepted", "assigned", "connected"],
      "properties": {
        "accepted": { "type": "string", "minLength": 1, "maxLength": 64 },
        "assigned": { "type": "string", "minLength": 1, "maxLength": 64 },
        "connected": { "type": "string", "minLength": 1, "maxLength": 64 }
      }
    },
    "capabilities": {
      "type": "object",
      "additionalProperties": false,
      "required": ["human_message_channel", "live_channel_offer"],
      "properties": {
        "human_message_channel": { "type": "boolean" },
        "live_channel_offer": { "type": "boolean" },
        "languages": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "string", "pattern": "^[a-z]{2}(-[A-Z]{2})?$" }
        },
        "hours": { "enum": ["24x7", "business-hours", "scheduled"] }
      }
    },
    "attestation": {
      "description": "Who accepted this receiver and on what evidence. An unattested profile is a draft, never an onboarded receiver.",
      "type": "object",
      "additionalProperties": false,
      "required": ["accepted_by", "accepted_on", "evidence_uri"],
      "properties": {
        "accepted_by": { "type": "string", "pattern": "^[a-zA-Z0-9 .,'&()_-]{2,160}$" },
        "accepted_on": { "type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" },
        "evidence_uri": { "type": "string", "pattern": "^https://[a-zA-Z0-9.-]+(:[0-9]{2,5})?(/[A-Za-z0-9._~/-]*)?$" }
      }
    }
  }
}
