{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://bpa.local/schemas/element-contract/v1alpha1",
  "title": "BPA Element Contract v1alpha1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "apiVersion",
    "kind",
    "metadata",
    "intent",
    "scope",
    "expectedCount",
    "candidates",
    "preconditions",
    "postconditions",
    "volatility",
    "validatedSnapshots"
  ],
  "properties": {
    "apiVersion": { "const": "bpa.page/v1alpha1" },
    "kind": { "const": "ElementContract" },
    "metadata": { "$ref": "#/$defs/metadata" },
    "intent": { "type": "string", "minLength": 1, "maxLength": 1000 },
    "scope": {
      "type": "object",
      "additionalProperties": false,
      "required": ["origins", "pathPattern", "pageState", "frame"],
      "properties": {
        "origins": {
          "type": "array",
          "minItems": 1,
          "maxItems": 20,
          "uniqueItems": true,
          "items": { "type": "string", "format": "uri" }
        },
        "pathPattern": {
          "type": "string",
          "minLength": 1,
          "maxLength": 500
        },
        "pageState": { "$ref": "#/$defs/id" },
        "frame": { "enum": ["top", "same-origin-child"] }
      }
    },
    "expectedCount": {
      "type": "object",
      "additionalProperties": false,
      "required": ["minimum", "maximum"],
      "properties": {
        "minimum": { "type": "integer", "minimum": 0, "maximum": 10000 },
        "maximum": { "type": "integer", "minimum": 1, "maximum": 10000 }
      }
    },
    "candidates": {
      "type": "array",
      "minItems": 1,
      "maxItems": 20,
      "items": { "$ref": "#/$defs/candidate" },
      "contains": {
        "type": "object",
        "required": ["strategy"],
        "properties": {
          "strategy": {
            "enum": [
              "business-id",
              "role-name",
              "label",
              "attribute",
              "relative-anchor"
            ]
          }
        }
      }
    },
    "preconditions": {
      "type": "array",
      "maxItems": 50,
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/id" }
    },
    "postconditions": {
      "type": "array",
      "maxItems": 50,
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/id" }
    },
    "volatility": { "enum": ["low", "medium", "high"] },
    "validatedSnapshots": {
      "type": "array",
      "minItems": 2,
      "maxItems": 100,
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/digest" }
    }
  },
  "$defs": {
    "metadata": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "version", "title"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "version": { "$ref": "#/$defs/semver" },
        "title": { "type": "string", "minLength": 1, "maxLength": 200 },
        "description": { "type": "string", "maxLength": 4000 }
      }
    },
    "id": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$",
      "maxLength": 160
    },
    "semver": {
      "type": "string",
      "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(?:-[0-9A-Za-z.-]+)?$"
    },
    "digest": { "type": "string", "pattern": "^sha256:[a-f0-9]{64}$" },
    "candidate": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["strategy", "value"],
          "properties": {
            "strategy": { "const": "business-id" },
            "value": { "type": "string", "minLength": 1, "maxLength": 500 }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["strategy", "role", "name"],
          "properties": {
            "strategy": { "const": "role-name" },
            "role": { "type": "string", "minLength": 1, "maxLength": 100 },
            "name": { "type": "string", "minLength": 1, "maxLength": 500 }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["strategy", "label"],
          "properties": {
            "strategy": { "const": "label" },
            "label": { "type": "string", "minLength": 1, "maxLength": 500 }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["strategy", "name", "value"],
          "properties": {
            "strategy": { "const": "attribute" },
            "name": {
              "type": "string",
              "pattern": "^(?:data-[a-z0-9_-]+|name|href|type|aria-[a-z0-9_-]+)$",
              "maxLength": 100
            },
            "value": { "type": "string", "minLength": 1, "maxLength": 1000 }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["strategy", "anchor", "role", "name"],
          "properties": {
            "strategy": { "const": "relative-anchor" },
            "anchor": { "$ref": "#/$defs/id" },
            "role": { "type": "string", "minLength": 1, "maxLength": 100 },
            "name": { "type": "string", "minLength": 1, "maxLength": 500 }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["strategy", "selector"],
          "properties": {
            "strategy": { "const": "css-diagnostic" },
            "selector": {
              "type": "string",
              "minLength": 1,
              "maxLength": 1000,
              "not": { "pattern": "(?:xpath|XPath|nth-child\\s*\\()" }
            }
          }
        }
      ]
    }
  }
}
