{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:next-platform:manifest:1.0",
  "title": "Next Platform App Manifest 1.0 (routing extension)",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "manifestVersion",
    "id",
    "name",
    "version",
    "compatibility",
    "runtime",
    "artifact",
    "entrypoints",
    "capabilities",
    "permissions",
    "configuration",
    "deployment",
    "lifecycle"
  ],
  "properties": {
    "manifestVersion": {
      "const": "1.0"
    },
    "id": {
      "type": "string",
      "maxLength": 100,
      "pattern": "^[a-z][a-z0-9-]*(\\.[a-z][a-z0-9-]*){2,}$"
    },
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 80
    },
    "version": {
      "type": "string",
      "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-[0-9A-Za-z.-]+)?(\\+[0-9A-Za-z.-]+)?$"
    },
    "description": {
      "type": "string",
      "maxLength": 500
    },
    "author": {
      "type": "string",
      "maxLength": 100
    },
    "icon": {
      "const": "assets/icon.png"
    },
    "compatibility": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "platform",
        "sdk"
      ],
      "properties": {
        "platform": {
          "type": "string",
          "minLength": 1
        },
        "sdk": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "runtime": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "framework",
        "port",
        "healthCheck",
        "stateless"
      ],
      "properties": {
        "type": {
          "const": "container"
        },
        "framework": {
          "const": "nextjs"
        },
        "port": {
          "type": "integer",
          "minimum": 1,
          "maximum": 65535
        },
        "healthCheck": {
          "$ref": "#/$defs/route"
        },
        "stateless": {
          "const": true
        }
      }
    },
    "artifact": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "image"
      ],
      "properties": {
        "image": {
          "type": "string",
          "maxLength": 512,
          "pattern": "^[a-z0-9][a-z0-9.:-]*/[a-z0-9][a-z0-9._/-]*@sha256:[a-f0-9]{64}$"
        }
      }
    },
    "entrypoints": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "frontend"
      ],
      "properties": {
        "frontend": {
          "$ref": "#/$defs/route"
        },
        "admin": {
          "$ref": "#/$defs/route"
        }
      }
    },
    "capabilities": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "auth",
        "database",
        "storage",
        "ai"
      ],
      "properties": {
        "auth": {
          "$ref": "#/$defs/capability"
        },
        "database": {
          "$ref": "#/$defs/capability"
        },
        "storage": {
          "$ref": "#/$defs/capability"
        },
        "ai": {
          "const": "none"
        }
      }
    },
    "database": {
      "$ref": "#/$defs/database"
    },
    "permissions": {
      "type": "array",
      "maxItems": 100,
      "uniqueItems": true,
      "items": {
        "type": "string",
        "pattern": "^[a-z][a-z0-9_-]*(\\.[a-z][a-z0-9_-]*)+$"
      }
    },
    "configuration": {
      "type": "object",
      "maxProperties": 100,
      "propertyNames": {
        "pattern": "^[a-zA-Z][a-zA-Z0-9_]{0,63}$"
      },
      "additionalProperties": {
        "$ref": "#/$defs/config"
      }
    },
    "deployment": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "provider",
        "resources",
        "scaling"
      ],
      "properties": {
        "provider": {
          "enum": [
            "cloudbase",
            "docker"
          ]
        },
        "resources": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "cpu",
            "memoryMiB"
          ],
          "properties": {
            "cpu": {
              "type": "number",
              "exclusiveMinimum": 0
            },
            "memoryMiB": {
              "type": "integer",
              "minimum": 1
            }
          }
        },
        "scaling": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "minInstances",
            "maxInstances"
          ],
          "properties": {
            "minInstances": {
              "type": "integer",
              "minimum": 0
            },
            "maxInstances": {
              "type": "integer",
              "minimum": 1
            }
          }
        }
      }
    },
    "lifecycle": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "preserveDataOnUninstall",
        "migrationCompatibility"
      ],
      "properties": {
        "preserveDataOnUninstall": {
          "const": true
        },
        "migrationCompatibility": {
          "enum": [
            "compatible",
            "breaking",
            "none"
          ]
        },
        "migrationPath": {
          "const": "migrations/"
        }
      }
    },
    "routing": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "mode",
        "slug",
        "basePath",
        "pathTransmission"
      ],
      "properties": {
        "mode": {
          "const": "path"
        },
        "slug": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9-]{1,39}$"
        },
        "basePath": {
          "type": "string",
          "pattern": "^/apps/[a-z][a-z0-9-]{1,39}$"
        },
        "pathTransmission": {
          "const": true
        }
      }
    }
  },
  "$defs": {
    "route": {
      "type": "string",
      "maxLength": 200,
      "pattern": "^/(?:[a-zA-Z0-9_-]+/)*[a-zA-Z0-9_-]*$"
    },
    "capability": {
      "enum": [
        "required",
        "optional",
        "none"
      ]
    },
    "database": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "provider",
        "schemaVersion"
      ],
      "properties": {
        "provider": {
          "enum": [
            "cloudbase-nosql",
            "postgresql"
          ]
        },
        "schemaVersion": {
          "type": "string",
          "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)$"
        },
        "collections": {
          "type": "array",
          "maxItems": 50,
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/collection"
          }
        },
        "isolation": {
          "const": "database"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "provider": {
                "const": "cloudbase-nosql"
              }
            }
          },
          "then": {
            "required": [
              "collections"
            ],
            "not": {
              "required": [
                "isolation"
              ]
            }
          }
        },
        {
          "if": {
            "properties": {
              "provider": {
                "const": "postgresql"
              }
            }
          },
          "then": {
            "required": [
              "isolation"
            ],
            "not": {
              "required": [
                "collections"
              ]
            }
          }
        }
      ]
    },
    "collection": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "name",
        "indexes"
      ],
      "properties": {
        "name": {
          "type": "string",
          "pattern": "^np_[a-z][a-z0-9_]{1,62}$"
        },
        "description": {
          "type": "string",
          "maxLength": 300
        },
        "indexes": {
          "type": "array",
          "maxItems": 20,
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/index"
          }
        }
      }
    },
    "index": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "name",
        "fields"
      ],
      "properties": {
        "name": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{1,62}$"
        },
        "unique": {
          "type": "boolean",
          "default": false
        },
        "fields": {
          "type": "array",
          "minItems": 1,
          "maxItems": 32,
          "uniqueItems": true,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "name",
              "direction"
            ],
            "properties": {
              "name": {
                "type": "string",
                "pattern": "^[a-zA-Z][a-zA-Z0-9_.]{0,63}$"
              },
              "direction": {
                "enum": [
                  1,
                  -1
                ]
              }
            }
          }
        }
      }
    },
    "config": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "secret"
      ],
      "properties": {
        "type": {
          "enum": [
            "string",
            "number",
            "integer",
            "boolean"
          ]
        },
        "secret": {
          "type": "boolean"
        },
        "default": {
          "type": [
            "string",
            "number",
            "boolean"
          ]
        },
        "description": {
          "type": "string",
          "maxLength": 300
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "secret": {
                "const": true
              }
            }
          },
          "then": {
            "not": {
              "required": [
                "default"
              ]
            },
            "properties": {
              "type": {
                "const": "string"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "string"
              }
            }
          },
          "then": {
            "properties": {
              "default": {
                "type": "string"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "number"
              }
            }
          },
          "then": {
            "properties": {
              "default": {
                "type": "number"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "integer"
              }
            }
          },
          "then": {
            "properties": {
              "default": {
                "type": "integer"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "boolean"
              }
            }
          },
          "then": {
            "properties": {
              "default": {
                "type": "boolean"
              }
            }
          }
        }
      ]
    }
  }
}
