{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://owogg.com/schemas/manifest/v1.json",
  "title": "OWOGG Game Manifest v1",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "game", "progression", "result"],
  "properties": {
    "$schema": { "type": "string", "const": "https://owogg.com/schemas/manifest/v1.json" },
    "schemaVersion": { "type": "integer", "const": 1 },
    "game": {
      "type": "object",
      "additionalProperties": false,
      "required": ["slug", "title", "genre", "mode", "playModes"],
      "properties": {
        "slug": { "type": "string", "pattern": "^[a-z0-9-]+$", "minLength": 3, "maxLength": 64 },
        "title": {
          "type": "string",
          "minLength": 1,
          "maxLength": 60,
          "description": "Required English/default title. Use localizations for ko, ja, and zh."
        },
        "genre": { "type": "string", "minLength": 1 },
        "mode": { "type": "string", "enum": ["single", "multi"] },
        "playModes": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": { "type": "string", "enum": ["single", "local-multi", "online-multi"] }
        },
        "shortDescription": {
          "type": "string",
          "maxLength": 200,
          "description": "English/default summary."
        },
        "localizations": {
          "type": "object",
          "additionalProperties": false,
          "minProperties": 1,
          "description": "Optional translated titles and summaries. English stays in the top-level fields.",
          "properties": {
            "ko": { "$ref": "#/$defs/gameLocalization" },
            "ja": { "$ref": "#/$defs/gameLocalization" },
            "zh": { "$ref": "#/$defs/gameLocalization" }
          }
        },
        "description": {
          "oneOf": [
            { "type": "string", "maxLength": 4000, "deprecated": true },
            {
              "type": "array",
              "minItems": 1,
              "uniqueItems": true,
              "contains": { "const": "description.md" },
              "items": {
                "type": "string",
                "enum": [
                  "description.md",
                  "description_kr.md",
                  "description_ja.md",
                  "description_zh.md"
                ]
              }
            }
          ]
        },
        "description_images": {
          "type": "array",
          "maxItems": 5,
          "uniqueItems": true,
          "items": { "type": "string", "minLength": 1 }
        },
        "tags": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 },
          "uniqueItems": true
        }
      },
      "allOf": [
        {
          "if": { "properties": { "mode": { "const": "single" } }, "required": ["mode"] },
          "then": { "properties": { "playModes": { "const": ["single"] } } }
        },
        {
          "if": { "properties": { "mode": { "const": "multi" } }, "required": ["mode"] },
          "then": {
            "properties": {
              "playModes": {
                "contains": { "enum": ["local-multi", "online-multi"] },
                "minContains": 1
              }
            }
          }
        }
      ]
    },
    "input": {
      "type": "array",
      "items": { "type": "string", "enum": ["keyboard", "mouse", "touch", "gamepad"] },
      "uniqueItems": true
    },
    "presentation": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "orientation": { "type": "string", "enum": ["any", "landscape", "portrait"] },
        "aspectRatio": { "type": "string", "pattern": "^[1-9][0-9]*:[1-9][0-9]*$" },
        "defaultMode": { "type": "string", "enum": ["default", "theater"] }
      }
    },
    "difficulties": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["id", "title"],
        "properties": {
          "id": { "type": "string", "minLength": 1, "maxLength": 100 },
          "title": { "type": "string", "minLength": 1, "maxLength": 60 },
          "default": { "type": "boolean" }
        }
      }
    },
    "progression": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type"],
      "properties": {
        "type": {
          "type": "string",
          "enum": ["none", "endless", "stage", "level", "round", "wave", "chapter", "lap", "custom"]
        },
        "range": { "$ref": "#/$defs/range" }
      }
    },
    "result": {
      "type": "object",
      "additionalProperties": false,
      "required": ["score"],
      "properties": {
        "outcome": {
          "anyOf": [
            { "type": "null" },
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["values"],
              "properties": {
                "values": {
                  "type": "array",
                  "minItems": 1,
                  "uniqueItems": true,
                  "items": {
                    "type": "string",
                    "enum": ["neutral", "success", "failure", "win", "loss", "draw"]
                  }
                }
              }
            }
          ]
        },
        "score": { "anyOf": [{ "type": "null" }, { "$ref": "#/$defs/score" }] },
        "metrics": {
          "type": "object",
          "propertyNames": { "pattern": "^[A-Za-z][A-Za-z0-9_-]{0,63}$" },
          "additionalProperties": { "$ref": "#/$defs/metric" }
        }
      }
    },
    "leaderboard": {
      "type": "object",
      "additionalProperties": false,
      "required": ["enabled"],
      "properties": { "enabled": { "type": "boolean" } }
    },
    "events": {
      "type": "object",
      "propertyNames": { "pattern": "^[A-Za-z][A-Za-z0-9_-]{0,63}$" },
      "additionalProperties": {
        "type": "object",
        "additionalProperties": false,
        "properties": { "maxPerAttempt": { "type": "integer", "minimum": 1 } }
      }
    },
    "achievements": { "type": "array", "items": { "$ref": "#/$defs/achievement" } },
    "multiplayer": { "$ref": "#/$defs/multiplayerRuntimeRequest" },
    "playConfig": { "$ref": "#/$defs/playConfig" }
  },
  "$defs": {
    "gameLocalization": {
      "type": "object",
      "additionalProperties": false,
      "minProperties": 1,
      "properties": {
        "title": { "type": "string", "minLength": 1, "maxLength": 60 },
        "shortDescription": { "type": "string", "maxLength": 200 }
      }
    },
    "range": {
      "type": "object",
      "additionalProperties": false,
      "required": ["min", "max"],
      "properties": {
        "min": { "type": "number" },
        "max": { "type": "number" },
        "outOfRange": { "type": "string", "enum": ["clamp", "reject"], "default": "clamp" }
      }
    },
    "score": {
      "type": "object",
      "additionalProperties": false,
      "required": ["unit", "direction", "range"],
      "properties": {
        "unit": { "type": "string", "minLength": 1 },
        "direction": { "type": "string", "enum": ["asc", "desc"] },
        "precision": { "type": "integer", "minimum": 0, "maximum": 6 },
        "range": { "$ref": "#/$defs/range" }
      }
    },
    "metric": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type"],
      "properties": {
        "type": { "type": "string", "enum": ["integer", "number"] },
        "range": { "$ref": "#/$defs/range" }
      }
    },
    "condition": {
      "type": "object",
      "additionalProperties": false,
      "required": ["source", "operator", "value"],
      "properties": {
        "source": {
          "type": "string",
          "enum": ["score", "outcome", "progression", "metric", "event"]
        },
        "key": { "type": "string", "minLength": 1 },
        "aggregate": { "type": "string", "enum": ["max", "min", "sum", "count"] },
        "operator": { "type": "string", "enum": ["==", "!=", ">", ">=", "<", "<="] },
        "value": { "type": ["number", "string", "boolean"] }
      }
    },
    "achievement": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "title", "condition"],
      "properties": {
        "id": { "type": "string", "pattern": "^[a-z0-9-]+$", "minLength": 1, "maxLength": 64 },
        "title": { "type": "string", "minLength": 1, "maxLength": 80 },
        "description": { "type": "string", "maxLength": 300 },
        "scope": { "type": "string", "enum": ["session", "lifetime"] },
        "condition": { "$ref": "#/$defs/condition" }
      }
    },
    "playConfigVariant": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "title"],
      "properties": {
        "id": { "type": "string", "minLength": 1, "maxLength": 100 },
        "title": { "type": "string", "minLength": 1, "maxLength": 60 },
        "default": { "type": "boolean" }
      }
    },
    "playConfigAllowedConfig": {
      "type": "object",
      "additionalProperties": false,
      "required": ["difficultyId", "variantId", "rewardFactor"],
      "properties": {
        "difficultyId": { "type": "string", "minLength": 1, "maxLength": 100 },
        "variantId": { "type": "string", "minLength": 1, "maxLength": 100 },
        "rewardFactor": { "type": "number", "exclusiveMinimum": 0 }
      }
    },
    "playConfig": {
      "type": "object",
      "additionalProperties": false,
      "required": ["version", "rulesetRevision", "verifierId", "variants", "allowedConfigs"],
      "properties": {
        "version": { "type": "integer", "const": 1 },
        "rulesetRevision": { "type": "integer", "minimum": 1 },
        "verifierId": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9._:/-]{0,95}$",
          "maxLength": 96
        },
        "variants": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/playConfigVariant" },
          "contains": {
            "type": "object",
            "properties": { "default": { "const": true } },
            "required": ["default"]
          },
          "minContains": 0,
          "maxContains": 1
        },
        "allowedConfigs": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/playConfigAllowedConfig" }
        }
      }
    },
    "multiplayerPlayers": {
      "type": "object",
      "additionalProperties": false,
      "required": ["min", "max"],
      "properties": {
        "min": { "type": "integer", "minimum": 2, "maximum": 8 },
        "max": { "type": "integer", "minimum": 2, "maximum": 8 }
      }
    },
    "multiplayerFeatures": {
      "type": "object",
      "additionalProperties": false,
      "required": ["reconnect", "directMessages", "hostSnapshot", "joinInProgress", "spectators"],
      "properties": {
        "reconnect": { "type": "string", "enum": ["none", "resume"] },
        "directMessages": { "type": "boolean" },
        "hostSnapshot": { "type": "boolean" },
        "joinInProgress": { "type": "boolean" },
        "spectators": { "type": "boolean" }
      }
    },
    "multiplayerTransport": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind", "protocolVersion"],
      "properties": {
        "kind": { "type": "string", "const": "websocket" },
        "protocolVersion": { "type": "integer", "const": 1 }
      }
    },
    "multiplayerRuntime": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind"],
      "properties": {
        "kind": { "type": "string", "enum": ["relay", "worker", "container"] }
      }
    },
    "multiplayerRuntimeRequest": {
      "type": "object",
      "additionalProperties": false,
      "required": ["version", "transport", "runtime", "players", "features"],
      "properties": {
        "version": { "type": "integer", "const": 1 },
        "transport": { "$ref": "#/$defs/multiplayerTransport" },
        "runtime": { "$ref": "#/$defs/multiplayerRuntime" },
        "players": { "$ref": "#/$defs/multiplayerPlayers" },
        "features": { "$ref": "#/$defs/multiplayerFeatures" }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "leaderboard": { "properties": { "enabled": { "const": true } }, "required": ["enabled"] }
        },
        "required": ["leaderboard"]
      },
      "then": {
        "properties": { "result": { "properties": { "score": { "not": { "type": "null" } } } } }
      }
    },
    {
      "if": { "required": ["multiplayer"] },
      "then": {
        "properties": {
          "game": {
            "properties": {
              "mode": { "const": "multi" },
              "playModes": { "contains": { "const": "online-multi" }, "minContains": 1 }
            }
          }
        },
        "allOf": [
          {
            "if": { "not": { "required": ["playConfig"] } },
            "then": {
              "properties": {
                "leaderboard": { "properties": { "enabled": { "const": false } } }
              }
            }
          }
        ]
      }
    },
    {
      "if": { "required": ["playConfig"] },
      "then": {
        "required": ["leaderboard"],
        "properties": {
          "game": {
            "properties": {
              "playModes": {
                "contains": { "enum": ["single", "local-multi"] },
                "minContains": 1
              }
            }
          },
          "result": { "properties": { "score": { "not": { "type": "null" } } } },
          "leaderboard": {
            "properties": { "enabled": { "const": true } },
            "required": ["enabled"]
          }
        }
      }
    }
  ]
}
