{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:auf:v1:unit-definition.schema.json",
  "title": "AUF Unit Definition",
  "description": "Versioned boundary of an AIFi Unit (ERC Section 3). boundaryHash = keccak256(JCS(document)).",
  "type": "object",
  "required": [
    "auf",
    "kind",
    "unit",
    "elements",
    "relations",
    "ports",
    "authorities"
  ],
  "properties": {
    "auf": {
      "$ref": "urn:auf:v1:common.schema.json#/$defs/aufVersion"
    },
    "kind": {
      "const": "unit-definition"
    },
    "unit": {
      "type": "object",
      "required": [
        "name"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "summary": {
          "type": "string"
        },
        "tags": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "additionalProperties": false
    },
    "elements": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/element"
      }
    },
    "relations": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/relation"
      }
    },
    "ports": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/port"
      }
    },
    "authorities": {
      "type": "object",
      "required": [
        "definitionController"
      ],
      "properties": {
        "definitionController": {
          "$ref": "urn:auf:v1:common.schema.json#/$defs/accountOrNone"
        },
        "representatives": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/authorityEntry"
          }
        },
        "beneficiaries": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/authorityEntry"
          }
        },
        "settlementAccounts": {
          "type": "array",
          "items": {
            "$ref": "urn:auf:v1:common.schema.json#/$defs/caip10"
          }
        },
        "notes": {
          "type": "string"
        }
      },
      "additionalProperties": false
    },
    "validity": {
      "type": "object",
      "properties": {
        "from": {
          "$ref": "urn:auf:v1:common.schema.json#/$defs/timestamp"
        },
        "until": {
          "$ref": "urn:auf:v1:common.schema.json#/$defs/timestamp"
        }
      },
      "additionalProperties": false
    },
    "scale": {
      "type": "object",
      "properties": {
        "window": {
          "type": "string",
          "pattern": "^P(?!$)(\\d+Y)?(\\d+M)?(\\d+W)?(\\d+D)?(T(?=\\d)(\\d+H)?(\\d+M)?(\\d+S)?)?$"
        },
        "notes": {
          "type": "string"
        }
      },
      "additionalProperties": false
    },
    "extensions": {
      "$ref": "urn:auf:v1:common.schema.json#/$defs/extensions"
    },
    "metadata": {
      "type": "object"
    }
  },
  "additionalProperties": false,
  "$defs": {
    "elementKind": {
      "oneOf": [
        {
          "enum": [
            "agent",
            "skill",
            "task",
            "service",
            "model",
            "compute",
            "data",
            "financial-position",
            "unit"
          ]
        },
        {
          "type": "string",
          "pattern": "^x:[A-Za-z0-9._-]+:[A-Za-z0-9._-]+$"
        }
      ]
    },
    "element": {
      "type": "object",
      "required": [
        "kind",
        "ref"
      ],
      "properties": {
        "kind": {
          "$ref": "#/$defs/elementKind"
        },
        "ref": {
          "type": "string",
          "minLength": 1
        },
        "label": {
          "type": "string"
        },
        "controller": {
          "$ref": "urn:auf:v1:common.schema.json#/$defs/caip10"
        },
        "underlying": {
          "type": "object",
          "description": "What the element is a claim against: a Unit reference (optionally a port of it), a CAIP-19 asset, a CAIP-10 account or a URI. Elements with the same underlying and no same-claim or distinct-claim declaration between them are overlap candidates (Section 3.8).",
          "required": [
            "ref"
          ],
          "properties": {
            "ref": {
              "type": "string",
              "minLength": 1
            },
            "port": {
              "type": "string",
              "minLength": 1
            },
            "share": {
              "$ref": "urn:auf:v1:common.schema.json#/$defs/share"
            },
            "basis": {
              "$ref": "urn:auf:v1:common.schema.json#/$defs/evidence"
            }
          },
          "additionalProperties": false
        },
        "expectedAcks": {
          "type": "array",
          "items": {
            "enum": [
              "participation",
              "resource-use",
              "fund-operation",
              "description-endorsement"
            ]
          }
        },
        "evidence": {
          "$ref": "urn:auf:v1:common.schema.json#/$defs/evidence"
        },
        "extensions": {
          "$ref": "urn:auf:v1:common.schema.json#/$defs/extensions"
        }
      },
      "additionalProperties": false
    },
    "relationType": {
      "oneOf": [
        {
          "enum": [
            "contains",
            "owns",
            "licenses",
            "reserves",
            "revenue-share",
            "holds",
            "owes",
            "commits",
            "pledges",
            "depends-on",
            "exchanges",
            "controls",
            "same-claim",
            "distinct-claim"
          ]
        },
        {
          "type": "string",
          "pattern": "^x:[A-Za-z0-9._-]+:[A-Za-z0-9._-]+$"
        }
      ]
    },
    "endpoint": {
      "oneOf": [
        {
          "const": "unit"
        },
        {
          "$ref": "urn:auf:v1:common.schema.json#/$defs/hex32"
        }
      ]
    },
    "relation": {
      "type": "object",
      "required": [
        "type"
      ],
      "oneOf": [
        {
          "required": [
            "from",
            "to"
          ],
          "not": {
            "required": [
              "elements"
            ]
          }
        },
        {
          "required": [
            "elements"
          ],
          "properties": {
            "type": {
              "enum": [
                "same-claim",
                "distinct-claim"
              ]
            }
          },
          "not": {
            "anyOf": [
              {
                "required": [
                  "from"
                ]
              },
              {
                "required": [
                  "to"
                ]
              }
            ]
          }
        }
      ],
      "properties": {
        "type": {
          "$ref": "#/$defs/relationType"
        },
        "from": {
          "$ref": "#/$defs/endpoint"
        },
        "to": {
          "$ref": "#/$defs/endpoint"
        },
        "elements": {
          "type": "array",
          "minItems": 2,
          "uniqueItems": true,
          "items": {
            "$ref": "urn:auf:v1:common.schema.json#/$defs/hex32"
          },
          "description": "Set form of an identity relation (same-claim, distinct-claim): the relation holds between every pair of the listed elements; used in place of from and to (Section 3.4)."
        },
        "scope": {
          "type": "string"
        },
        "validFrom": {
          "$ref": "urn:auf:v1:common.schema.json#/$defs/timestamp"
        },
        "validUntil": {
          "$ref": "urn:auf:v1:common.schema.json#/$defs/timestamp"
        },
        "quantity": {
          "type": "object",
          "required": [
            "amount",
            "unit"
          ],
          "properties": {
            "amount": {
              "$ref": "urn:auf:v1:common.schema.json#/$defs/decimalString"
            },
            "unit": {
              "type": "string",
              "minLength": 1
            }
          },
          "additionalProperties": false
        },
        "share": {
          "$ref": "urn:auf:v1:common.schema.json#/$defs/share"
        },
        "exclusive": {
          "type": "boolean"
        },
        "obligations": {
          "type": "string"
        },
        "counterparty": {
          "type": "string"
        },
        "basis": {
          "$ref": "urn:auf:v1:common.schema.json#/$defs/evidence"
        },
        "extensions": {
          "$ref": "urn:auf:v1:common.schema.json#/$defs/extensions"
        }
      },
      "additionalProperties": false
    },
    "port": {
      "type": "object",
      "required": [
        "id",
        "direction",
        "class"
      ],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[A-Za-z0-9._-]+$"
        },
        "direction": {
          "enum": [
            "in",
            "out",
            "inout"
          ]
        },
        "class": {
          "enum": [
            "funds",
            "resource",
            "deliverable",
            "right",
            "commitment",
            "control"
          ]
        },
        "template": {
          "$ref": "urn:auf:v1:common.schema.json#/$defs/identifier"
        },
        "asset": {
          "$ref": "urn:auf:v1:common.schema.json#/$defs/caip19"
        },
        "account": {
          "$ref": "urn:auf:v1:common.schema.json#/$defs/caip10"
        },
        "conditions": {
          "type": "string"
        },
        "stateEffects": {
          "type": "string"
        },
        "extensions": {
          "$ref": "urn:auf:v1:common.schema.json#/$defs/extensions"
        }
      },
      "additionalProperties": false
    },
    "authorityEntry": {
      "type": "object",
      "required": [
        "account"
      ],
      "properties": {
        "account": {
          "type": "string",
          "minLength": 1
        },
        "scope": {
          "type": "string"
        },
        "basis": {
          "$ref": "urn:auf:v1:common.schema.json#/$defs/evidence"
        }
      },
      "additionalProperties": false
    }
  }
}
