{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://colliehq.github.io/collie/extension-manifest-v1.schema.json",
  "title": "Collie extension manifest v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version", "id", "name", "version", "publisher", "description", "license",
    "collie", "platforms", "files", "components", "permissions", "data", "verification"
  ],
  "properties": {
    "schema_version": { "const": 1 },
    "id": {
      "type": "string",
      "pattern": "^[a-z0-9](?:[a-z0-9._-]{0,126}[a-z0-9])?$"
    },
    "name": { "type": "string", "minLength": 1, "maxLength": 160 },
    "version": { "$ref": "#/$defs/semver" },
    "publisher": { "type": "string", "minLength": 1, "maxLength": 160 },
    "description": { "type": "string", "minLength": 1, "maxLength": 1000 },
    "license": { "type": "string", "minLength": 1, "maxLength": 160 },
    "collie": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "min_version": { "$ref": "#/$defs/semver" },
        "max_version": {
          "anyOf": [{ "const": "" }, { "$ref": "#/$defs/semver" }]
        }
      }
    },
    "platforms": {
      "type": "array",
      "minItems": 1,
      "uniqueItems": true,
      "items": { "enum": ["windows", "macos", "linux"] }
    },
    "files": {
      "type": "array",
      "maxItems": 511,
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/path" }
    },
    "components": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "skills": { "$ref": "#/$defs/paths" },
        "hooks": { "$ref": "#/$defs/paths" },
        "templates": { "$ref": "#/$defs/paths" },
        "assets": { "$ref": "#/$defs/paths" },
        "connections": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["name", "url"],
            "properties": {
              "name": {
                "type": "string",
                "pattern": "^[a-z0-9](?:[a-z0-9._-]{0,126}[a-z0-9])?$"
              },
              "url": { "type": "string", "pattern": "^https://" },
              "description": { "type": "string", "maxLength": 500 },
              "auth": { "enum": ["none", "oauth", "api_key_env"] },
              "secret_ref": { "type": "string", "pattern": "^[A-Z][A-Z0-9_]{1,127}$" }
            },
            "allOf": [
              {
                "if": {
                  "properties": { "auth": { "const": "api_key_env" } },
                  "required": ["auth"]
                },
                "then": { "required": ["secret_ref"] }
              }
            ]
          }
        }
      }
    },
    "permissions": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "filesystem": { "$ref": "#/$defs/strings" },
        "network": { "$ref": "#/$defs/strings" },
        "credentials": {
          "type": "array",
          "uniqueItems": true,
          "items": { "type": "string", "pattern": "^[A-Z][A-Z0-9_]{1,127}$" }
        },
        "browser": { "$ref": "#/$defs/strings" },
        "desktop": { "$ref": "#/$defs/strings" },
        "external_actions": { "$ref": "#/$defs/strings" },
        "host_hooks": { "type": "boolean" }
      }
    },
    "data": {
      "type": "object",
      "additionalProperties": false,
      "required": ["retention", "export", "uninstall"],
      "properties": {
        "retention": { "enum": ["none", "package", "user_state"] },
        "export": { "type": "boolean" },
        "uninstall": { "enum": ["remove", "retain_user_state"] }
      }
    },
    "verification": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["kind"],
        "properties": {
          "kind": { "enum": ["command", "pytest", "artifact", "manual", "url"] },
          "description": { "type": "string", "minLength": 1, "maxLength": 4096 },
          "command": { "type": "string", "minLength": 1, "maxLength": 4096 },
          "path": { "$ref": "#/$defs/path" }
        },
        "anyOf": [
          { "required": ["description"] },
          { "required": ["command"] },
          { "required": ["path"] }
        ]
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "components": {
            "properties": { "hooks": { "minItems": 1 } },
            "required": ["hooks"]
          }
        }
      },
      "then": {
        "properties": {
          "permissions": {
            "properties": { "host_hooks": { "const": true } },
            "required": ["host_hooks"]
          }
        }
      }
    }
  ],
  "$defs": {
    "semver": {
      "type": "string",
      "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(?:-(?:0|[1-9][0-9]*|[0-9A-Za-z-]*[A-Za-z-][0-9A-Za-z-]*)(?:\\.(?:0|[1-9][0-9]*|[0-9A-Za-z-]*[A-Za-z-][0-9A-Za-z-]*))*)?(?:\\+[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)?$"
    },
    "path": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2048,
      "pattern": "^(?!/)(?!.*(?:^|/)\\.\\.(?:/|$))(?!.*[:\\\\]).+$"
    },
    "paths": {
      "type": "array",
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/path" }
    },
    "strings": {
      "type": "array",
      "uniqueItems": true,
      "items": { "type": "string", "minLength": 1, "maxLength": 2048 }
    }
  }
}
