{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://bpa.local/schemas/node/v1alpha2",
  "title": "BPA Node Definition v1alpha2",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "apiVersion",
    "kind",
    "metadata",
    "runtime",
    "inputSchema",
    "outputSchema",
    "risk",
    "execution",
    "errors"
  ],
  "allOf": [
    {
      "if": {
        "properties": { "runtime": { "const": "browser" } },
        "required": ["runtime"]
      },
      "then": {
        "required": ["resources"],
        "properties": {
          "resources": {
            "type": "object",
            "minProperties": 1
          },
          "risk": {
            "type": "object",
            "properties": {
              "domains": {
                "type": "array"
              }
            },
            "required": ["domains"]
          }
        }
      },
      "else": {
        "not": {
          "required": ["resources"]
        }
      }
    }
  ],
  "properties": {
    "apiVersion": { "const": "bpa/v1alpha2" },
    "kind": { "const": "Node" },
    "metadata": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "version", "title"],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$",
          "maxLength": 160
        },
        "version": {
          "type": "string",
          "maxLength": 100,
          "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(?:-[0-9A-Za-z.-]+)?$"
        },
        "title": { "type": "string", "minLength": 1, "maxLength": 200 },
        "description": { "type": "string", "maxLength": 4000 }
      }
    },
    "runtime": { "enum": ["engine_builtin", "engine_team", "browser", "human", "composite"] },
    "inputSchema": { "type": "object" },
    "outputSchema": { "type": "object" },
    "configSchema": { "type": "object" },
    "risk": {
      "type": "object",
      "additionalProperties": false,
      "required": ["level", "permissions"],
      "properties": {
        "level": { "enum": ["R0", "R1", "R2", "R3", "R4"] },
        "permissions": {
          "type": "array",
          "maxItems": 100,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "pattern": "^[a-z][a-z0-9]*(?:[.:_-][a-z0-9]+)*$"
          }
        },
        "domains": {
          "type": "array",
          "minItems": 1,
          "maxItems": 16,
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/httpsOrigin" }
        }
      }
    },
    "execution": {
      "type": "object",
      "additionalProperties": false,
      "required": ["timeoutDefault", "idempotency"],
      "properties": {
        "timeoutDefault": {
          "type": "string",
          "pattern": "^[1-9][0-9]*(?:ms|s|m)$"
        },
        "idempotency": {
          "enum": ["pure", "repeatable_read", "verified_write", "non_repeatable"]
        },
        "retryableErrors": {
          "type": "array",
          "maxItems": 50,
          "uniqueItems": true,
          "items": { "type": "string", "pattern": "^[A-Z][A-Z0-9_]+$" }
        },
        "cancellable": { "type": "boolean" },
        "timingPolicy": {
          "$ref": "https://bpa.local/schemas/timing-policy/v1"
        }
      }
    },
    "errors": {
      "type": "array",
      "maxItems": 100,
      "uniqueItems": true,
      "items": { "type": "string", "pattern": "^[A-Z][A-Z0-9_]+$" }
    },
    "evidence": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "required": {
          "type": "array",
          "maxItems": 4,
          "uniqueItems": true,
          "items": { "enum": ["before", "after", "result", "error"] }
        }
      }
    },
    "adapter": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "versions"],
      "properties": {
        "id": { "type": "string", "minLength": 1, "maxLength": 160 },
        "versions": {
          "type": "array",
          "minItems": 1,
          "maxItems": 50,
          "uniqueItems": true,
          "items": { "type": "string", "minLength": 1, "maxLength": 100 }
        }
      }
    },
    "resources": {
      "type": "object",
      "minProperties": 1,
      "maxProperties": 16,
      "propertyNames": { "$ref": "#/$defs/resourceName" },
      "additionalProperties": {
        "$ref": "#/$defs/browserResourceRequirement"
      }
    }
  },
  "$defs": {
    "resourceName": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_]*$",
      "maxLength": 64
    },
    "capability": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9]*(?:[.:_-][a-z0-9]+)*$",
      "maxLength": 160
    },
    "httpsOrigin": {
      "type": "string",
      "format": "uri",
      "pattern": "^https://[^/?#@:]+(?::[1-9][0-9]{0,4})?$",
      "maxLength": 2048
    },
    "browserResourceRequirement": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "kind",
        "capabilities",
        "allowedOrigins",
        "authentication",
        "purpose"
      ],
      "properties": {
        "kind": { "const": "browser" },
        "capabilities": {
          "type": "array",
          "minItems": 1,
          "maxItems": 32,
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/capability" }
        },
        "allowedOrigins": {
          "type": "array",
          "minItems": 1,
          "maxItems": 16,
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/httpsOrigin" }
        },
        "authentication": {
          "enum": ["anonymous", "optional", "authenticated", "membership"]
        },
        "purpose": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        }
      }
    }
  }
}
