{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kazokuai.dev/spec/house-link/link.schema.json",
  "title": "House link, contract 1",
  "description": "Every body and every token's claims in LINK.md (text v1.0), field for field. Open objects: a receiver ignores what it doesn't know (LINK.md §10). Pick the definition with $ref: #/$defs/<name>.",
  "$defs": {
    "origin": {
      "description": "scheme://host[:port], as the URL standard serializes an origin (LINK.md §1).",
      "type": "string",
      "pattern": "^https?://[^/\\s]+$",
      "maxLength": 512
    },
    "nonce": {
      "description": "16 random bytes, base64url without padding (§3.4).",
      "type": "string",
      "pattern": "^[A-Za-z0-9_-]{22}$"
    },
    "time": {
      "description": "Whole seconds since 1970-01-01 UTC.",
      "type": "integer",
      "minimum": 0,
      "maximum": 9007199254740991
    },
    "capabilities": {
      "description": "Strings; this text defines home, games.install and songs.make (§8).",
      "type": "array",
      "items": { "type": "string", "minLength": 1, "maxLength": 64 },
      "maxItems": 32,
      "uniqueItems": true
    },
    "appName": { "type": "string", "minLength": 1, "maxLength": 80 },
    "memberId": { "type": "string", "minLength": 1, "maxLength": 64 },
    "memberName": { "type": "string", "minLength": 1, "maxLength": 80 },
    "hue": { "type": "integer", "minimum": 0, "maximum": 359 },
    "role": { "enum": ["adult", "minor"] },
    "sha256": { "type": "string", "pattern": "^[0-9a-f]{64}$" },

    "pairRequest": {
      "description": "B → A: POST /api/house-link/pair (§2 step 2).",
      "type": "object",
      "required": ["contract", "code", "origin", "name", "capabilities"],
      "properties": {
        "contract": { "const": 1 },
        "code": { "type": "string", "minLength": 8, "maxLength": 16 },
        "origin": { "$ref": "#/$defs/origin" },
        "name": { "$ref": "#/$defs/appName" },
        "capabilities": { "$ref": "#/$defs/capabilities" }
      }
    },
    "pairAnswer": {
      "description": "A → B: 200 to the pair request (§2 step 3).",
      "type": "object",
      "required": ["contract", "key", "origin", "name", "capabilities"],
      "properties": {
        "contract": { "const": 1 },
        "key": { "type": "string", "pattern": "^[A-Za-z0-9_-]{43}$" },
        "origin": { "$ref": "#/$defs/origin" },
        "name": { "$ref": "#/$defs/appName" },
        "capabilities": { "$ref": "#/$defs/capabilities" }
      }
    },
    "refusal": {
      "description": "Any refusal from an API route (§9).",
      "type": "object",
      "required": ["error", "reason"],
      "properties": {
        "error": { "type": "string", "minLength": 1 },
        "reason": {
          "enum": [
            "bad_request", "contract", "already_paired", "bad_code", "expired_code", "malformed", "not_paired",
            "bad_signature", "wrong_type", "wrong_issuer", "wrong_audience", "too_long", "skewed", "expired", "replayed",
            "unknown_link", "unknown_intent", "bad_body"
          ]
        }
      }
    },
    "ok": {
      "description": "200 to an unpair or unlink notice (§2.3, §5.3).",
      "type": "object",
      "required": ["ok"],
      "properties": { "ok": { "const": true } }
    },
    "installAnswer": {
      "description": "The answer to POST /api/house-link/games/install (§8).",
      "oneOf": [
        {
          "type": "object",
          "required": ["installed", "id", "version"],
          "properties": {
            "installed": { "const": true },
            "id": { "type": "string", "pattern": "^[a-z0-9]+(-[a-z0-9]+)*$", "maxLength": 64 },
            "version": { "type": "string", "minLength": 1 }
          }
        },
        {
          "type": "object",
          "required": ["installed", "reason", "code"],
          "properties": {
            "installed": { "const": false },
            "reason": { "type": "string", "minLength": 1 },
            "code": { "type": "string", "minLength": 1 }
          }
        }
      ]
    },
    "songsRequest": {
      "description": "The body of POST /api/house-link/songs/make (§8): a package's briefs, as the game-package contract's songs[].",
      "type": "object",
      "required": ["package", "songs"],
      "properties": {
        "package": { "type": "string", "pattern": "^[a-z0-9]+(-[a-z0-9]+)*$", "maxLength": 64 },
        "songs": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["id", "playlist", "brief", "mood", "lyrics"],
            "properties": {
              "id": { "type": "string", "minLength": 1 },
              "playlist": { "type": "string", "minLength": 1 },
              "brief": { "type": "string", "minLength": 1 },
              "mood": { "type": "array", "items": { "type": "string" } },
              "lyrics": { "type": "boolean" }
            }
          }
        }
      }
    },
    "songsAnswer": {
      "type": "object",
      "required": ["queued"],
      "properties": {
        "queued": { "type": "integer", "minimum": 0 },
        "reason": { "type": "string" },
        "code": { "type": "string" }
      }
    },

    "base": {
      "description": "The five claims every token has, and its type (§4).",
      "type": "object",
      "required": ["typ", "iss", "aud", "iat", "exp", "nonce"],
      "properties": {
        "typ": { "enum": ["link", "linked", "assert", "install", "songs", "unlink", "unpair"] },
        "iss": { "$ref": "#/$defs/origin" },
        "aud": { "$ref": "#/$defs/origin" },
        "iat": { "$ref": "#/$defs/time" },
        "exp": { "$ref": "#/$defs/time" },
        "nonce": { "$ref": "#/$defs/nonce" }
      }
    },
    "member": {
      "description": "A member's four claims, the sender's member as the sender knows them (§4).",
      "type": "object",
      "required": ["member_id", "name", "hue", "role"],
      "properties": {
        "member_id": { "$ref": "#/$defs/memberId" },
        "name": { "$ref": "#/$defs/memberName" },
        "hue": { "$ref": "#/$defs/hue" },
        "role": { "$ref": "#/$defs/role" }
      }
    },
    "link": {
      "description": "A link intent: ≤ 300 s (§5 step 1).",
      "allOf": [{ "$ref": "#/$defs/base" }, { "$ref": "#/$defs/member" }],
      "properties": { "typ": { "const": "link" } }
    },
    "linked": {
      "description": "A link's answer: ≤ 300 s (§5 step 3).",
      "allOf": [{ "$ref": "#/$defs/base" }, { "$ref": "#/$defs/member" }],
      "required": ["intent", "for"],
      "properties": {
        "typ": { "const": "linked" },
        "intent": { "$ref": "#/$defs/nonce" },
        "for": { "$ref": "#/$defs/memberId" }
      }
    },
    "assert": {
      "description": "Open in: ≤ 60 s (§6).",
      "allOf": [{ "$ref": "#/$defs/base" }, { "$ref": "#/$defs/member" }],
      "required": ["sub"],
      "properties": { "typ": { "const": "assert" }, "sub": { "$ref": "#/$defs/memberId" } }
    },
    "install": {
      "description": "An install's assertion: ≤ 60 s (§8).",
      "allOf": [{ "$ref": "#/$defs/base" }, { "$ref": "#/$defs/member" }],
      "required": ["sub", "sha256"],
      "properties": { "typ": { "const": "install" }, "sub": { "$ref": "#/$defs/memberId" }, "sha256": { "$ref": "#/$defs/sha256" } }
    },
    "songs": {
      "description": "Make songs' assertion: ≤ 60 s (§8).",
      "allOf": [{ "$ref": "#/$defs/base" }, { "$ref": "#/$defs/member" }],
      "required": ["sub", "sha256"],
      "properties": { "typ": { "const": "songs" }, "sub": { "$ref": "#/$defs/memberId" }, "sha256": { "$ref": "#/$defs/sha256" } }
    },
    "unlink": {
      "description": "An unlink notice: ≤ 60 s (§5.3).",
      "allOf": [{ "$ref": "#/$defs/base" }],
      "required": ["member_id", "sub"],
      "properties": { "typ": { "const": "unlink" }, "member_id": { "$ref": "#/$defs/memberId" }, "sub": { "$ref": "#/$defs/memberId" } }
    },
    "unpair": {
      "description": "An unpair notice: ≤ 60 s (§2.3).",
      "allOf": [{ "$ref": "#/$defs/base" }],
      "properties": { "typ": { "const": "unpair" } }
    }
  }
}
