{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://humanisoffline.com/schemas/human-offline-card.v3.json",
  "title": "Human Offline Card v3",
  "type": "object",
  "additionalProperties": false,
  "required": ["schema", "schemaVersion", "resources", "freshness", "card"],
  "properties": {
    "schema": { "type": "string", "format": "uri" },
    "schemaVersion": { "type": "integer", "const": 3 },
    "freshness": {
      "enum": ["fresh", "stale"],
      "description": "Card validity state from timestamps on the card."
    },
    "resources": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "html",
        "json",
        "markdown",
        "publicKey",
        "requestForm",
        "requestEndpoint",
        "resourceIndex",
        "cardSchema",
        "requestSchema",
        "receiptSchema",
        "publicKeySchema"
      ],
      "properties": {
        "html": { "type": "string", "format": "uri" },
        "json": { "type": "string", "format": "uri" },
        "markdown": { "type": "string", "format": "uri" },
        "publicKey": { "type": "string", "format": "uri" },
        "requestForm": { "type": "string", "format": "uri" },
        "requestEndpoint": { "type": "string", "format": "uri" },
        "resourceIndex": { "type": "string", "format": "uri" },
        "cardSchema": { "type": "string", "format": "uri" },
        "requestSchema": { "type": "string", "format": "uri" },
        "receiptSchema": { "type": "string", "format": "uri" },
        "publicKeySchema": { "type": "string", "format": "uri" }
      }
    },
    "card": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "slug",
        "updatedAt",
        "sections",
        "delegationPolicy",
        "requestPolicy"
      ],
      "properties": {
        "slug": { "type": "string" },
        "updatedAt": { "type": "string", "format": "date-time" },
        "sections": {
          "type": "array",
          "items": { "$ref": "#/$defs/cardSection" }
        },
        "delegationPolicy": { "$ref": "#/$defs/delegationPolicy" },
        "requestPolicy": {
          "type": "object",
          "additionalProperties": false,
          "required": ["acceptsRequests", "endpoint", "encryption"],
          "properties": {
            "acceptsRequests": { "type": "boolean" },
            "endpoint": { "type": "string", "format": "uri" },
            "encryption": { "const": "required" },
            "publicKeyId": { "type": ["string", "null"] },
            "publicEncryptionKey": { "type": ["string", "null"] }
          }
        }
      }
    }
  },
  "$defs": {
    "boundaryOutcome": {
      "enum": ["allowed", "forbidden", "ask_first", "stop"]
    },
    "riskLevel": {
      "enum": ["low", "medium", "high", "irreversible"]
    },
    "policyCategory": {
      "enum": [
        "code",
        "deployments",
        "data",
        "billing",
        "communications",
        "permissions",
        "research",
        "planning"
      ]
    },
    "riskOutcomes": {
      "type": "object",
      "additionalProperties": false,
      "required": ["low", "medium", "high", "irreversible"],
      "properties": {
        "low": { "$ref": "#/$defs/boundaryOutcome" },
        "medium": { "$ref": "#/$defs/boundaryOutcome" },
        "high": { "$ref": "#/$defs/boundaryOutcome" },
        "irreversible": { "$ref": "#/$defs/boundaryOutcome" }
      }
    },
    "delegationPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "version",
        "outcomes",
        "riskLabels",
        "instructionSections",
        "riskPolicy"
      ],
      "properties": {
        "version": { "type": "integer", "const": 1 },
        "outcomes": {
          "type": "array",
          "items": { "$ref": "#/$defs/boundaryOutcome" }
        },
        "riskLabels": {
          "type": "array",
          "items": { "$ref": "#/$defs/riskLevel" }
        },
        "instructionSections": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "id",
              "title",
              "description",
              "outcome",
              "categories",
              "items"
            ],
            "properties": {
              "id": {
                "enum": [
                  "allowed-actions",
                  "forbidden-actions",
                  "requires-approval"
                ]
              },
              "title": { "type": "string" },
              "description": { "type": "string" },
              "outcome": { "$ref": "#/$defs/boundaryOutcome" },
              "categories": {
                "type": "array",
                "items": { "$ref": "#/$defs/policyCategory" }
              },
              "items": {
                "type": "array",
                "items": { "type": "string" }
              }
            }
          }
        },
        "riskPolicy": {
          "oneOf": [
            { "type": "null" },
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["labels"],
              "properties": {
                "labels": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": ["low", "medium", "high", "irreversible"],
                  "properties": {
                    "low": { "$ref": "#/$defs/riskPolicyLabel" },
                    "medium": { "$ref": "#/$defs/riskPolicyLabel" },
                    "high": { "$ref": "#/$defs/riskPolicyLabel" },
                    "irreversible": { "$ref": "#/$defs/riskPolicyLabel" }
                  }
                }
              }
            }
          ]
        }
      }
    },
    "riskPolicyLabel": {
      "type": "object",
      "additionalProperties": false,
      "required": ["guidance", "outcome"],
      "properties": {
        "guidance": { "type": "string" },
        "outcome": { "$ref": "#/$defs/boundaryOutcome" }
      }
    },
    "cardSection": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["id", "kind", "required", "displayName", "status"],
          "properties": {
            "id": { "const": "identity" },
            "kind": { "const": "identity" },
            "required": { "const": true },
            "displayName": { "type": ["string", "null"] },
            "status": {
              "enum": [
                "online",
                "offline",
                "do_not_disturb",
                "sleeping",
                "traveling",
                "available_urgent_only"
              ]
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "id",
            "kind",
            "required",
            "title",
            "description",
            "outcome",
            "categories",
            "items"
          ],
          "properties": {
            "id": {
              "enum": [
                "allowed-actions",
                "forbidden-actions",
                "requires-approval"
              ]
            },
            "kind": { "const": "instruction_list" },
            "required": { "const": false },
            "title": { "type": "string" },
            "description": { "type": "string" },
            "outcome": { "$ref": "#/$defs/boundaryOutcome" },
            "categories": {
              "type": "array",
              "items": { "$ref": "#/$defs/policyCategory" }
            },
            "items": {
              "type": "array",
              "items": { "type": "string", "maxLength": 200 }
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "id",
            "kind",
            "required",
            "timezone",
            "returnsAt",
            "validUntil",
            "staleAfter"
          ],
          "properties": {
            "id": { "const": "availability" },
            "kind": { "const": "availability" },
            "required": { "const": false },
            "timezone": { "type": ["string", "null"] },
            "returnsAt": { "type": ["string", "null"], "format": "date-time" },
            "validUntil": { "type": ["string", "null"], "format": "date-time" },
            "staleAfter": { "type": ["string", "null"], "format": "date-time" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "id",
            "kind",
            "required",
            "low",
            "medium",
            "high",
            "irreversible",
            "outcomes"
          ],
          "properties": {
            "id": { "const": "risk-policy" },
            "kind": { "const": "risk_policy" },
            "required": { "const": false },
            "low": { "type": "string" },
            "medium": { "type": "string" },
            "high": { "type": "string" },
            "irreversible": { "type": "string" },
            "outcomes": { "$ref": "#/$defs/riskOutcomes" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "id",
            "kind",
            "required",
            "visibility",
            "title",
            "content"
          ],
          "properties": {
            "id": { "const": "public-note" },
            "kind": { "const": "note" },
            "required": { "const": false },
            "visibility": { "const": "public" },
            "title": { "type": "string" },
            "content": { "type": "string" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["id", "kind", "required", "acceptsRequests"],
          "properties": {
            "id": { "const": "requests" },
            "kind": { "const": "request_policy" },
            "required": { "const": false },
            "acceptsRequests": { "type": "boolean" }
          }
        }
      ]
    }
  }
}
