{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.fanettiai.com/software/score-keeper/backup-schema.json",
  "title": "Score Keeper backup",
  "description": "The file Score Keeper writes when you choose Download backup. Every value stays on your device; this schema documents the exact shape so the data stays readable without the app. Scores are whole numbers.",
  "type": "object",
  "additionalProperties": false,
  "required": ["app", "schemaVersion", "players", "games", "live", "sessions", "settings", "meta", "exportedAt", "appVersion"],
  "properties": {
    "app": { "const": "fanetti-score-keeper" },
    "schemaVersion": { "type": "integer", "minimum": 1, "maximum": 1 },
    "players": { "type": "array", "maxItems": 60, "items": { "$ref": "#/$defs/player" } },
    "games": { "type": "array", "maxItems": 100, "items": { "$ref": "#/$defs/game" } },
    "live": { "oneOf": [ { "type": "null" }, { "$ref": "#/$defs/played" } ] },
    "sessions": { "type": "array", "maxItems": 5000, "items": { "$ref": "#/$defs/session" } },
    "settings": { "$ref": "#/$defs/settings" },
    "meta": { "$ref": "#/$defs/meta" },
    "exportedAt": { "type": "string", "format": "date-time" },
    "appVersion": { "type": "string", "minLength": 1 }
  },
  "$defs": {
    "player": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "name", "color"],
      "properties": {
        "id": { "type": "string", "pattern": "^[A-Za-z0-9_-]{1,64}$" },
        "name": { "type": "string", "minLength": 1, "maxLength": 30 },
        "color": { "enum": ["red", "orange", "gold", "green", "teal", "blue", "purple", "pink"] }
      }
    },
    "game": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "name", "scoring", "target", "rounds", "rules"],
      "properties": {
        "id": { "type": "string", "pattern": "^[A-Za-z0-9_-]{1,64}$" },
        "name": { "type": "string", "minLength": 1, "maxLength": 30 },
        "scoring": { "enum": ["high", "low"] },
        "target": { "type": ["integer", "null"], "minimum": 1, "maximum": 1000000 },
        "rounds": { "type": ["integer", "null"], "minimum": 1, "maximum": 100 },
        "rules": { "type": "string", "maxLength": 300 }
      }
    },
    "played": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "gameId", "name", "scoring", "target", "rounds", "players", "scores", "started", "round", "turn", "past", "future"],
      "properties": {
        "id": { "type": "string", "pattern": "^[A-Za-z0-9_-]{1,64}$" },
        "gameId": { "type": "string", "minLength": 1 },
        "name": { "type": "string", "minLength": 1, "maxLength": 30 },
        "scoring": { "enum": ["high", "low"] },
        "target": { "type": ["integer", "null"], "minimum": 1, "maximum": 1000000 },
        "rounds": { "type": ["integer", "null"], "minimum": 1, "maximum": 100 },
        "players": {
          "type": "array",
          "minItems": 1,
          "maxItems": 12,
          "items": { "$ref": "#/$defs/playedPlayer" }
        },
        "scores": {
          "type": "array",
          "minItems": 1,
          "maxItems": 200,
          "items": {
            "type": "array",
            "items": { "type": "integer", "minimum": -100000, "maximum": 100000 }
          }
        },
        "started": { "type": "integer", "minimum": 946684800000, "maximum": 4102444800000 },
        "round": { "type": "integer", "minimum": 0 },
        "turn": { "type": "integer", "minimum": 0 },
        "past": { "type": "array", "items": { "type": "object" } },
        "future": { "type": "array", "items": { "type": "object" } }
      }
    },
    "playedPlayer": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "name", "color"],
      "properties": {
        "id": { "type": "string", "minLength": 1, "maxLength": 64 },
        "name": { "type": "string", "minLength": 1, "maxLength": 30 },
        "color": { "enum": ["red", "orange", "gold", "green", "teal", "blue", "purple", "pink"] }
      }
    },
    "session": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "gameId", "name", "scoring", "target", "rounds", "players", "scores", "totals", "ranks", "winners", "started", "ended"],
      "properties": {
        "id": { "type": "string", "pattern": "^[A-Za-z0-9_-]{1,64}$" },
        "gameId": { "type": "string", "minLength": 1 },
        "name": { "type": "string", "minLength": 1, "maxLength": 30 },
        "scoring": { "enum": ["high", "low"] },
        "target": { "type": ["integer", "null"], "minimum": 1, "maximum": 1000000 },
        "rounds": { "type": ["integer", "null"], "minimum": 1, "maximum": 100 },
        "players": {
          "type": "array",
          "minItems": 1,
          "maxItems": 12,
          "items": { "$ref": "#/$defs/playedPlayer" }
        },
        "scores": {
          "type": "array",
          "minItems": 1,
          "maxItems": 200,
          "items": {
            "type": "array",
            "items": { "type": "integer", "minimum": -100000, "maximum": 100000 }
          }
        },
        "totals": { "type": "array", "items": { "type": "integer" } },
        "ranks": { "type": "array", "items": { "type": "integer", "minimum": 1 } },
        "winners": { "type": "array", "items": { "type": "string" } },
        "started": { "type": "integer", "minimum": 946684800000, "maximum": 4102444800000 },
        "ended": { "type": "integer", "minimum": 946684800000, "maximum": 4102444800000 }
      }
    },
    "settings": {
      "type": "object",
      "additionalProperties": false,
      "required": ["theme", "notifications", "lastGame", "lastPlayers"],
      "properties": {
        "theme": { "enum": ["system", "light", "dark"] },
        "notifications": { "type": "boolean" },
        "lastGame": { "type": "string" },
        "lastPlayers": { "type": "array", "items": { "type": "string" } }
      }
    },
    "meta": {
      "type": "object",
      "additionalProperties": false,
      "required": ["lastExport", "firstUse"],
      "properties": {
        "lastExport": { "type": ["string", "null"], "format": "date-time" },
        "firstUse": { "type": ["string", "null"], "format": "date-time" }
      }
    }
  }
}
