{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://bpa.local/schemas/node/v1alpha1",
  "title": "BPA Node Definition",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "apiVersion",
    "kind",
    "metadata",
    "runtime",
    "inputSchema",
    "outputSchema",
    "risk",
    "execution",
    "errors"
  ],
  "allOf": [
    {
      "if": {
        "properties": { "runtime": { "const": "browser" } },
        "required": ["runtime"]
      },
      "then": {
        "properties": {
          "risk": {
            "type": "object",
            "properties": {
              "domains": {
                "type": "array"
              }
            },
            "required": ["domains"]
          }
        }
      }
    }
  ],
  "properties": {
    "apiVersion": { "const": "bpa/v1alpha1" },
    "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",
          "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",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "pattern": "^[a-z][a-z0-9]*(?:[.:_-][a-z0-9]+)*$"
          }
        },
        "domains": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "format": "uri"
          }
        }
      }
    },
    "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",
          "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",
      "uniqueItems": true,
      "items": { "type": "string", "pattern": "^[A-Z][A-Z0-9_]+$" }
    },
    "evidence": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "required": {
          "type": "array",
          "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,
          "items": { "type": "string", "minLength": 1, "maxLength": 100 }
        }
      }
    }
  }
}
