{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://trove.local/schemas/manifest.schema.json",
  "title": "Trove Curio Manifest v1",
  "type": "object",
  "required": [
    "schemaVersion",
    "id",
    "name",
    "version",
    "engine"
  ],
  "properties": {
    "schemaVersion": {
      "const": 1
    },
    "id": {
      "type": "string",
      "minLength": 3,
      "maxLength": 160,
      "pattern": "^[a-z0-9][a-z0-9.-]*[a-z0-9]$"
    },
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 100
    },
    "description": {
      "type": "string",
      "maxLength": 1000
    },
    "version": {
      "$ref": "#/$defs/semver"
    },
    "engine": {
      "type": "object",
      "required": [
        "trove"
      ],
      "properties": {
        "trove": {
          "type": "string",
          "minLength": 1,
          "maxLength": 100
        }
      },
      "additionalProperties": false
    },
    "icon": {
      "$ref": "#/$defs/relativePath"
    },
    "ui": {
      "type": "object",
      "required": [
        "entry"
      ],
      "properties": {
        "entry": {
          "$ref": "#/$defs/relativePath"
        },
        "window": {
          "type": "object",
          "properties": {
            "title": {
              "type": "string",
              "maxLength": 200
            },
            "width": {
              "type": "integer",
              "minimum": 240,
              "maximum": 10000
            },
            "height": {
              "type": "integer",
              "minimum": 160,
              "maximum": 10000
            },
            "minWidth": {
              "type": "integer",
              "minimum": 120,
              "maximum": 10000
            },
            "minHeight": {
              "type": "integer",
              "minimum": 80,
              "maximum": 10000
            },
            "resizable": {
              "type": "boolean"
            },
            "transparent": {
              "type": "boolean"
            },
            "alwaysOnTop": {
              "type": "boolean"
            }
          },
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    },
    "backend": {
      "type": "object",
      "required": [
        "entry",
        "lifecycle",
        "transport"
      ],
      "properties": {
        "entry": {
          "$ref": "#/$defs/relativePath"
        },
        "args": {
          "type": "array",
          "items": {
            "type": "string",
            "maxLength": 4096
          },
          "maxItems": 256,
          "default": []
        },
        "env": {
          "type": "object",
          "propertyNames": {
            "pattern": "^[A-Z_][A-Z0-9_]*$"
          },
          "additionalProperties": {
            "type": "string",
            "maxLength": 16384
          }
        },
        "lifecycle": {
          "type": "string",
          "enum": [
            "on-demand",
            "app-start"
          ]
        },
        "transport": {
          "const": "unix"
        },
        "startWithUi": {
          "type": "boolean",
          "default": false
        },
        "startupTimeoutMs": {
          "type": "integer",
          "minimum": 100,
          "maximum": 300000,
          "default": 10000
        },
        "shutdownGraceMs": {
          "type": "integer",
          "minimum": 0,
          "maximum": 60000,
          "default": 2000
        },
        "idleTimeoutMs": {
          "type": "integer",
          "minimum": 0,
          "maximum": 86400000,
          "default": 0
        },
        "contract": {
          "$ref": "#/$defs/relativePath"
        }
      },
      "additionalProperties": false
    },
    "services": {
      "type": "object",
      "properties": {
        "provides": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/providedService"
          },
          "maxItems": 128,
          "default": []
        },
        "requires": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/requiredService"
          },
          "maxItems": 256,
          "default": []
        }
      },
      "additionalProperties": false
    },
    "development": {
      "type": "object",
      "properties": {
        "ui": {
          "type": "object",
          "required": [
            "url"
          ],
          "properties": {
            "command": {
              "$ref": "#/$defs/command"
            },
            "url": {
              "type": "string",
              "format": "uri",
              "pattern": "^https?://(127\\.0\\.0\\.1|localhost)(:[0-9]+)?(?:/.*)?$"
            },
            "readyTimeoutMs": {
              "type": "integer",
              "minimum": 100,
              "maximum": 300000,
              "default": 20000
            }
          },
          "additionalProperties": false
        },
        "backend": {
          "type": "object",
          "required": [
            "mode"
          ],
          "properties": {
            "mode": {
              "type": "string",
              "enum": [
                "managed",
                "external"
              ]
            },
            "command": {
              "$ref": "#/$defs/command"
            }
          },
          "allOf": [
            {
              "if": {
                "properties": {
                  "mode": {
                    "const": "managed"
                  }
                },
                "required": [
                  "mode"
                ]
              },
              "then": {
                "required": [
                  "command"
                ]
              }
            }
          ],
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    },
    "build": {
      "type": "object",
      "required": [
        "command"
      ],
      "properties": {
        "command": {
          "$ref": "#/$defs/command"
        }
      },
      "additionalProperties": false
    },
    "package": {
      "type": "object",
      "required": [
        "files"
      ],
      "properties": {
        "files": {
          "type": "array",
          "minItems": 1,
          "maxItems": 256,
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/relativePath"
          }
        }
      },
      "additionalProperties": false
    },
    "bindings": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "backend": {
          "$ref": "#/$defs/relativePath"
        },
        "generate": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "services": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "contract",
              "output"
            ],
            "additionalProperties": false,
            "properties": {
              "contract": {
                "$ref": "#/$defs/relativePath"
              },
              "output": {
                "$ref": "#/$defs/relativePath"
              }
            }
          }
        }
      }
    }
  },
  "anyOf": [
    {
      "required": [
        "ui"
      ]
    },
    {
      "required": [
        "backend"
      ]
    }
  ],
  "allOf": [
    {
      "if": {
        "properties": {
          "services": {
            "properties": {
              "provides": {
                "minItems": 1
              }
            },
            "required": [
              "provides"
            ]
          }
        },
        "required": [
          "services"
        ]
      },
      "then": {
        "required": [
          "backend"
        ]
      }
    }
  ],
  "additionalProperties": false,
  "$defs": {
    "semver": {
      "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.-]+)?$"
    },
    "relativePath": {
      "type": "string",
      "minLength": 1,
      "maxLength": 1024,
      "pattern": "^(?!/)(?!.*(?:^|/)\\.\\.(?:/|$)).+"
    },
    "serviceId": {
      "type": "string",
      "minLength": 3,
      "maxLength": 240,
      "pattern": "^[a-z0-9][a-z0-9.-]*[a-z0-9]$"
    },
    "providedService": {
      "type": "object",
      "required": [
        "id",
        "version",
        "contract"
      ],
      "properties": {
        "id": {
          "$ref": "#/$defs/serviceId"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "contract": {
          "$ref": "#/$defs/relativePath"
        }
      },
      "additionalProperties": false
    },
    "requiredService": {
      "type": "object",
      "required": [
        "id",
        "version"
      ],
      "properties": {
        "id": {
          "$ref": "#/$defs/serviceId"
        },
        "version": {
          "type": "string",
          "minLength": 1,
          "maxLength": 100
        },
        "optional": {
          "type": "boolean",
          "default": false
        }
      },
      "additionalProperties": false
    },
    "command": {
      "type": "array",
      "items": {
        "type": "string",
        "maxLength": 4096
      },
      "minItems": 1,
      "maxItems": 256
    }
  }
}
