{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:orbiplex:schema:contact-lookup-result:v1",
  "title": "ContactLookupResult v1",
  "description": "Route-set result of a Contact Catalog lookup. A positive result is a lookup-safe route candidate, not identity assurance and not proof that a relationship exists.",
  "type": "object",
  "additionalProperties": true,
  "x-dia-workflow": "project",
  "x-dia-status": "draft",
  "x-dia-basis": [
    "doc/project/40-proposals/058-contact-catalog.md",
    "doc/project/30-stories/story-010-message-to-a-friend.md",
    "doc/project/60-solutions/023-artifact-delivery/023-artifact-delivery.md"
  ],
  "required": [
    "schema",
    "schema/v",
    "lookup/id",
    "lookup/mode",
    "match/class",
    "result/routes",
    "issued/at"
  ],
  "properties": {
    "schema": { "const": "contact-lookup-result.v1" },
    "schema/v": { "const": 1 },
    "lookup/id": {
      "type": "string",
      "pattern": "^contact-lookup:[A-Za-z0-9_-]+$"
    },
    "catalog/id": {
      "type": "string",
      "pattern": "^contact-catalog:[a-z0-9][a-z0-9:-]*$"
    },
    "lookup/mode": {
      "type": "string",
      "enum": ["invitation-only", "blinded-digest", "psi"]
    },
    "match/class": {
      "type": "string",
      "enum": ["invitation-available", "ambiguous", "no-match", "policy-denied", "rate-limited"]
    },
    "result/routes": {
      "type": "array",
      "items": { "$ref": "#/$defs/route" }
    },
    "selected/route": { "$ref": "#/$defs/route" },
    "valid/until": {
      "type": "string",
      "format": "date-time"
    },
    "policy/ref": {
      "type": "string",
      "minLength": 1
    },
    "issued/at": {
      "type": "string",
      "format": "date-time"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": { "match/class": { "const": "invitation-available" } },
        "required": ["match/class"]
      },
      "then": {
        "required": ["selected/route"],
        "properties": {
          "result/routes": { "minItems": 1 }
        }
      }
    }
  ],
  "$defs": {
    "route": {
      "type": "object",
      "additionalProperties": true,
      "required": ["route/type", "purposes"],
      "properties": {
        "route/type": {
          "type": "string",
          "enum": ["routing-subject", "contact-nym", "invitation-route"]
        },
        "routing-subject/id": {
          "type": "string",
          "pattern": "^routing:did:key:z[1-9A-HJ-NP-Za-km-z]+$"
        },
        "contact-nym/id": {
          "type": "string",
          "pattern": "^contact-nym:[a-z0-9][a-z0-9:-]*$"
        },
        "node/id": {
          "type": "string",
          "pattern": "^node:did:key:z[1-9A-HJ-NP-Za-km-z]+$"
        },
        "invitation/ref": {
          "type": "string",
          "minLength": 1
        },
        "purposes": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "enum": ["contact", "inbox", "direct-delivery", "messaging", "recovery"]
          }
        },
        "valid/until": {
          "type": "string",
          "format": "date-time"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": { "route/type": { "const": "routing-subject" } },
            "required": ["route/type"]
          },
          "then": { "required": ["routing-subject/id"] }
        },
        {
          "if": {
            "properties": { "route/type": { "const": "contact-nym" } },
            "required": ["route/type"]
          },
          "then": { "required": ["contact-nym/id"] }
        },
        {
          "if": {
            "properties": { "route/type": { "const": "invitation-route" } },
            "required": ["route/type"]
          },
          "then": { "required": ["invitation/ref", "node/id"] }
        }
      ]
    }
  }
}
