{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:apuos:spkg:manifest:1.0",
  "title": "ApuOS SPKG manifest v1.0",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "format_version",
    "pkg_id",
    "name",
    "version",
    "architecture",
    "author",
    "category",
    "description",
    "icon",
    "screenshots",
    "dependencies",
    "permissions",
    "runtime",
    "release_notes",
    "license"
  ],
  "properties": {
    "format_version": {
      "enum": [
        "1.0",
        "1.1"
      ]
    },
    "pkg_id": {
      "type": "string",
      "minLength": 5,
      "maxLength": 128,
      "pattern": "^[a-z][a-z0-9-]*(\\.[a-z][a-z0-9-]*){2,}$"
    },
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 100,
      "pattern": "\\S"
    },
    "version": {
      "$ref": "#/$defs/semver"
    },
    "architecture": {
      "type": "array",
      "items": {
        "enum": [
          "x86_64",
          "arm64"
        ]
      },
      "minItems": 1,
      "maxItems": 2,
      "uniqueItems": true
    },
    "author": {
      "type": "string",
      "minLength": 1,
      "maxLength": 100,
      "pattern": "\\S"
    },
    "category": {
      "enum": [
        "system",
        "storage",
        "network",
        "media",
        "development",
        "security",
        "business"
      ]
    },
    "description": {
      "type": "string",
      "minLength": 1,
      "maxLength": 4000,
      "pattern": "\\S"
    },
    "license": {
      "type": "string",
      "minLength": 1,
      "maxLength": 100,
      "pattern": "\\S"
    },
    "icon": {
      "$ref": "#/$defs/asset"
    },
    "screenshots": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/asset"
      },
      "maxItems": 8,
      "uniqueItems": true
    },
    "dependencies": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "system_packages",
        "spkg_packages"
      ],
      "properties": {
        "system_packages": {
          "type": "array",
          "maxItems": 64,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "maxLength": 160,
            "pattern": "^[a-z0-9][a-z0-9+.-]*( >= [0-9][A-Za-z0-9.+:~_-]*)?$"
          }
        },
        "spkg_packages": {
          "type": "array",
          "maxItems": 64,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "maxLength": 160,
            "pattern": "^[a-z][a-z0-9-]*(\\.[a-z][a-z0-9-]*){2,} >= (0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)$"
          }
        }
      }
    },
    "permissions": {
      "type": "array",
      "maxItems": 4,
      "uniqueItems": true,
      "items": {
        "enum": [
          "network:bind",
          "network:outbound",
          "filesystem:read",
          "filesystem:read_write"
        ]
      }
    },
    "default_port": {
      "type": "integer",
      "minimum": 1024,
      "maximum": 65535
    },
    "runtime": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "entrypoint",
        "arguments",
        "memory_mb",
        "cpu_quota_percent"
      ],
      "properties": {
        "entrypoint": {
          "type": "string",
          "maxLength": 100,
          "pattern": "^(bin/\\{architecture\\}/[a-zA-Z0-9][a-zA-Z0-9._/-]*|assets/entry\\.js)$"
        },
        "arguments": {
          "type": "array",
          "maxItems": 32,
          "items": {
            "type": "string",
            "maxLength": 1024
          }
        },
        "memory_mb": {
          "type": "integer",
          "minimum": 16,
          "maximum": 65536
        },
        "cpu_quota_percent": {
          "type": "integer",
          "minimum": 1,
          "maximum": 100
        },
        "minimum_apuos": {
          "type": "string",
          "pattern": "^[0-9]\\.[0-9]{2}\\.[0-9]{3}$"
        },
        "supported_os": {
          "type": "array",
          "minItems": 1,
          "maxItems": 16,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "maxLength": 60,
            "pattern": "^[a-z][a-z0-9-]*:[0-9][a-zA-Z0-9._-]*$"
          }
        },
        "kind": {
          "const": "platform-module"
        },
        "module": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9-]*$",
          "maxLength": 40
        },
        "host_version": {
          "type": "string",
          "pattern": "^[0-9]\\.[0-9]{2}\\.[0-9]{3}$"
        },
        "styles": {
          "type": "array",
          "maxItems": 100,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "pattern": "^assets/[a-zA-Z0-9._-]+\\.css$"
          }
        },
        "worker": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "contract",
            "entrypoint",
            "sha256"
          ],
          "properties": {
            "contract": {
              "type": "string",
              "maxLength": 64,
              "pattern": "^[a-z][a-z0-9.-]*\\.v[0-9]+$"
            },
            "entrypoint": {
              "type": "string",
              "maxLength": 100,
              "pattern": "^bin/arm64/[a-z][a-z0-9-]{0,63}$"
            },
            "sha256": {
              "type": "string",
              "pattern": "^[a-f0-9]{64}$"
            }
          }
        }
      }
    },
    "release_notes": {
      "type": "string",
      "maxLength": 10000
    },
    "cli": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schema_version",
        "version",
        "manifest",
        "sha256"
      ],
      "properties": {
        "schema_version": {
          "const": 1
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "manifest": {
          "const": "assets/cli.json"
        },
        "sha256": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        }
      }
    }
  },
  "$defs": {
    "semver": {
      "type": "string",
      "maxLength": 60,
      "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)$"
    },
    "asset": {
      "type": "string",
      "maxLength": 100,
      "pattern": "^assets/[a-zA-Z0-9][a-zA-Z0-9._/-]*\\.(png|jpg|jpeg|webp|svg)$"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "format_version": {
            "const": "1.1"
          }
        }
      },
      "then": {
        "properties": {
          "runtime": {
            "required": [
              "kind",
              "module",
              "host_version"
            ],
            "properties": {
              "entrypoint": {
                "const": "assets/entry.js"
              }
            }
          }
        }
      },
      "else": {
        "properties": {
          "runtime": {
            "properties": {
              "entrypoint": {
                "pattern": "^bin/\\{architecture\\}/[a-zA-Z0-9][a-zA-Z0-9._/-]*$"
              }
            },
            "not": {
              "anyOf": [
                {
                  "required": [
                    "kind"
                  ]
                },
                {
                  "required": [
                    "module"
                  ]
                },
                {
                  "required": [
                    "host_version"
                  ]
                },
                {
                  "required": [
                    "styles"
                  ]
                },
                {
                  "required": [
                    "worker"
                  ]
                }
              ]
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "runtime": {
            "required": [
              "worker"
            ]
          }
        }
      },
      "then": {
        "required": [
          "cli"
        ]
      }
    }
  ]
}
