{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://vibebase.me/contracts/principal/0.2.0",
  "title": "Principal",
  "type": "object",
  "additionalProperties": false,
  "$defs": {
    "kind": { "enum": ["member", "app-user"] },
    "role": { "enum": ["owner", "admin", "builder", "viewer"] }
  },
  "required": ["subjectId", "kind", "workspace", "exp"],
  "properties": {
    "subjectId": { "type": "string", "minLength": 1 },
    "kind": { "$ref": "#/$defs/kind" },
    "workspace": { "type": "string", "minLength": 1 },
    "app": { "type": "string", "minLength": 1 },
    "roles": { "type": "array", "items": { "$ref": "#/$defs/role" } },
    "exp": { "type": "integer", "minimum": 0 }
  },
  "allOf": [
    {
      "if": { "properties": { "kind": { "const": "member" } }, "required": ["kind"] },
      "then": { "required": ["roles"], "properties": { "roles": { "type": "array", "minItems": 1 }, "app": false } }
    },
    {
      "if": { "properties": { "kind": { "const": "app-user" } }, "required": ["kind"] },
      "then": { "required": ["app"], "properties": { "roles": false } }
    }
  ]
}
