{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://blueprints.servus.run/schema/servus-blueprint-v1.schema.json",
  "title": "Servus Blueprint v1",
  "description": "Schema for Servus 1-click deployable application blueprints.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "id",
    "name",
    "tagline",
    "category",
    "version",
    "description",
    "setup",
    "defaultProfile",
    "profiles",
    "services"
  ],
  "properties": {
    "$schema": {
      "type": "string",
      "maxLength": 300
    },
    "schemaVersion": {
      "const": "servus-blueprint/v1"
    },
    "id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9-]{1,40}$"
    },
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 80
    },
    "tagline": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "description": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2000
    },
    "category": {
      "type": "string",
      "enum": [
        "cms",
        "automation",
        "monitoring",
        "storage",
        "analytics",
        "backend",
        "security",
        "developer",
        "project-management",
        "collaboration",
        "database",
        "communication",
        "search",
        "web"
      ]
    },
    "tags": {
      "type": "array",
      "maxItems": 20,
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 40
      }
    },
    "version": {
      "type": "string",
      "minLength": 1,
      "maxLength": 40
    },
    "publisher": {
      "type": "string",
      "minLength": 1,
      "maxLength": 120
    },
    "icon": {
      "type": "string",
      "minLength": 1,
      "maxLength": 80
    },
    "homepage": {
      "type": "string",
      "format": "uri",
      "maxLength": 300
    },
    "docsUrl": {
      "type": "string",
      "format": "uri",
      "maxLength": 300
    },
    "sourceUrl": {
      "type": "string",
      "format": "uri",
      "maxLength": 300
    },
    "license": {
      "type": "string",
      "minLength": 1,
      "maxLength": 80
    },
    "minPanelVersion": {
      "type": "string",
      "minLength": 1,
      "maxLength": 40
    },
    "setup": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2000
    },
    "defaultProfile": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9-]{0,30}$"
    },
    "inputs": {
      "type": "array",
      "maxItems": 30,
      "items": {
        "$ref": "#/$defs/inputField"
      }
    },
    "secrets": {
      "type": "array",
      "maxItems": 20,
      "items": {
        "$ref": "#/$defs/secretSpec"
      }
    },
    "domains": {
      "type": "array",
      "maxItems": 10,
      "items": {
        "$ref": "#/$defs/domainSlot"
      }
    },
    "profiles": {
      "type": "array",
      "minItems": 1,
      "maxItems": 6,
      "items": {
        "$ref": "#/$defs/resourceProfile"
      }
    },
    "files": {
      "type": "array",
      "maxItems": 20,
      "items": {
        "$ref": "#/$defs/configFile"
      }
    },
    "volumes": {
      "type": "array",
      "maxItems": 20,
      "items": {
        "$ref": "#/$defs/volumeSpec"
      }
    },
    "networks": {
      "type": "array",
      "maxItems": 10,
      "items": {
        "$ref": "#/$defs/networkSpec"
      }
    },
    "services": {
      "type": "array",
      "minItems": 1,
      "maxItems": 20,
      "items": {
        "$ref": "#/$defs/serviceSpec"
      }
    }
  },
  "$defs": {
    "composeName": {
      "type": "string",
      "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]{0,62}$"
    },
    "slotKey": {
      "type": "string",
      "pattern": "^[a-zA-Z][a-zA-Z0-9_]{0,39}$"
    },
    "safeTarget": {
      "type": "string",
      "minLength": 2,
      "maxLength": 240,
      "pattern": "^/[^\\s:]*$"
    },
    "condition": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["input", "equals"],
          "properties": {
            "input": { "$ref": "#/$defs/slotKey" },
            "equals": { "type": ["string", "number", "boolean"] }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["input", "notEquals"],
          "properties": {
            "input": { "$ref": "#/$defs/slotKey" },
            "notEquals": { "type": ["string", "number", "boolean"] }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["inputTruthy"],
          "properties": {
            "inputTruthy": { "$ref": "#/$defs/slotKey" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["inputFalsy"],
          "properties": {
            "inputFalsy": { "$ref": "#/$defs/slotKey" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["domainAssigned"],
          "properties": {
            "domainAssigned": { "$ref": "#/$defs/slotKey" }
          }
        }
      ]
    },
    "inputField": {
      "type": "object",
      "additionalProperties": false,
      "required": ["key", "label", "type"],
      "properties": {
        "key": { "$ref": "#/$defs/slotKey" },
        "label": { "type": "string", "minLength": 1, "maxLength": 80 },
        "description": { "type": "string", "maxLength": 300 },
        "type": { "type": "string", "enum": ["text", "email", "password", "number", "boolean", "select"] },
        "required": { "type": "boolean" },
        "default": { "type": ["string", "number", "boolean"] },
        "placeholder": { "type": "string", "maxLength": 160 },
        "min": { "type": "number" },
        "max": { "type": "number" },
        "pattern": { "type": "string", "maxLength": 160 },
        "secret": { "type": "boolean" },
        "group": { "type": "string", "enum": ["general", "advanced"] },
        "options": {
          "type": "array",
          "minItems": 1,
          "maxItems": 30,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["value", "label"],
            "properties": {
              "value": { "type": "string", "minLength": 1, "maxLength": 120 },
              "label": { "type": "string", "minLength": 1, "maxLength": 120 }
            }
          }
        }
      }
    },
    "secretSpec": {
      "type": "object",
      "additionalProperties": false,
      "required": ["key", "format", "bytes"],
      "properties": {
        "key": { "$ref": "#/$defs/slotKey" },
        "format": { "type": "string", "enum": ["base64url", "hex", "alphanumeric", "uuid"] },
        "bytes": { "type": "integer", "minimum": 16, "maximum": 64 },
        "prefix": { "type": "string", "maxLength": 24 }
      }
    },
    "domainSlot": {
      "type": "object",
      "additionalProperties": false,
      "required": ["key", "label", "service", "port"],
      "properties": {
        "key": { "$ref": "#/$defs/slotKey" },
        "label": { "type": "string", "minLength": 1, "maxLength": 80 },
        "description": { "type": "string", "maxLength": 300 },
        "service": { "$ref": "#/$defs/composeName" },
        "port": { "type": "integer", "minimum": 1, "maximum": 65535 },
        "primary": { "type": "boolean" },
        "required": { "type": "boolean" },
        "subdomainPrefix": { "type": "string", "pattern": "^[a-z0-9][a-z0-9-]{0,30}$" },
        "httpsDefault": { "type": "boolean" }
      }
    },
    "serviceResources": {
      "type": "object",
      "additionalProperties": false,
      "required": ["cpus", "memoryMb"],
      "properties": {
        "cpus": { "type": "number", "exclusiveMinimum": 0, "maximum": 64 },
        "memoryMb": { "type": "integer", "minimum": 64, "maximum": 131072 }
      }
    },
    "resourceProfile": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "label", "services"],
      "properties": {
        "id": { "type": "string", "pattern": "^[a-z0-9][a-z0-9-]{0,30}$" },
        "label": { "type": "string", "minLength": 1, "maxLength": 60 },
        "description": { "type": "string", "maxLength": 200 },
        "services": {
          "type": "object",
          "additionalProperties": { "$ref": "#/$defs/serviceResources" }
        }
      }
    },
    "configFile": {
      "type": "object",
      "additionalProperties": false,
      "required": ["key", "service", "target", "content"],
      "properties": {
        "key": { "$ref": "#/$defs/composeName" },
        "service": { "$ref": "#/$defs/composeName" },
        "target": { "$ref": "#/$defs/safeTarget" },
        "mode": { "type": "integer", "minimum": 256, "maximum": 511 },
        "content": { "type": "string", "maxLength": 65536 },
        "when": { "$ref": "#/$defs/condition" }
      }
    },
    "envCase": {
      "type": "object",
      "additionalProperties": false,
      "required": ["value"],
      "properties": {
        "value": { "type": "string", "maxLength": 16384 },
        "when": { "$ref": "#/$defs/condition" }
      }
    },
    "envValue": {
      "oneOf": [
        { "type": "string", "maxLength": 16384 },
        { "$ref": "#/$defs/envCase" },
        {
          "type": "array",
          "minItems": 1,
          "maxItems": 10,
          "items": { "$ref": "#/$defs/envCase" }
        }
      ]
    },
    "healthcheck": {
      "type": "object",
      "additionalProperties": false,
      "required": ["test"],
      "properties": {
        "test": {
          "oneOf": [
            { "type": "string", "minLength": 1, "maxLength": 4096 },
            {
              "type": "array",
              "minItems": 1,
              "maxItems": 30,
              "items": { "type": "string", "minLength": 1, "maxLength": 4096 }
            }
          ]
        },
        "interval": { "type": "string", "pattern": "^\\d+(ms|s|m|h)$" },
        "timeout": { "type": "string", "pattern": "^\\d+(ms|s|m|h)$" },
        "startPeriod": { "type": "string", "pattern": "^\\d+(ms|s|m|h)$" },
        "retries": { "type": "integer", "minimum": 1, "maximum": 100 }
      }
    },
    "serviceVolume": {
      "type": "object",
      "additionalProperties": false,
      "required": ["volume", "target"],
      "properties": {
        "volume": { "$ref": "#/$defs/composeName" },
        "target": { "$ref": "#/$defs/safeTarget" },
        "readOnly": { "type": "boolean" }
      }
    },
    "volumeSpec": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name"],
      "properties": {
        "name": { "$ref": "#/$defs/composeName" },
        "when": { "$ref": "#/$defs/condition" }
      }
    },
    "networkSpec": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name"],
      "properties": {
        "name": { "$ref": "#/$defs/composeName" },
        "internal": { "type": "boolean" },
        "when": { "$ref": "#/$defs/condition" }
      }
    },
    "serviceSpec": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "image", "resources"],
      "properties": {
        "name": { "$ref": "#/$defs/composeName" },
        "image": {
          "type": "string",
          "minLength": 3,
          "maxLength": 240,
          "not": { "pattern": ":latest$" }
        },
        "command": {
          "oneOf": [
            { "type": "string", "maxLength": 8192 },
            { "type": "array", "maxItems": 100, "items": { "type": "string", "maxLength": 8192 } }
          ]
        },
        "entrypoint": {
          "oneOf": [
            { "type": "string", "maxLength": 8192 },
            { "type": "array", "maxItems": 100, "items": { "type": "string", "maxLength": 8192 } }
          ]
        },
        "environment": {
          "type": "object",
          "propertyNames": { "pattern": "^[A-Za-z_][A-Za-z0-9_]{0,127}$" },
          "additionalProperties": { "$ref": "#/$defs/envValue" }
        },
        "volumes": {
          "type": "array",
          "maxItems": 20,
          "items": { "$ref": "#/$defs/serviceVolume" }
        },
        "networks": {
          "type": "array",
          "maxItems": 20,
          "items": { "$ref": "#/$defs/composeName" }
        },
        "dependsOn": {
          "type": "array",
          "maxItems": 20,
          "items": { "$ref": "#/$defs/composeName" }
        },
        "expose": {
          "type": "array",
          "maxItems": 20,
          "items": { "type": "integer", "minimum": 1, "maximum": 65535 }
        },
        "healthcheck": { "$ref": "#/$defs/healthcheck" },
        "user": { "type": "string", "maxLength": 128, "pattern": "^[A-Za-z0-9_.:-]+$" },
        "workingDir": { "$ref": "#/$defs/safeTarget" },
        "init": { "type": "boolean" },
        "readOnly": { "type": "boolean" },
        "restart": { "type": "string", "enum": ["no", "always", "on-failure", "unless-stopped"] },
        "resources": { "$ref": "#/$defs/serviceResources" },
        "when": { "$ref": "#/$defs/condition" }
      }
    }
  }
}
