{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "id": {
      "type": "string",
      "minLength": 1,
      "maxLength": 100,
      "pattern": "^[a-zA-Z0-9_-]+$"
    },
    "leaderboard": {
      "type": "string",
      "minLength": 1,
      "maxLength": 100,
      "pattern": "^[a-zA-Z0-9_-]+$"
    },
    "version": {
      "type": "string",
      "minLength": 1,
      "maxLength": 100,
      "pattern": "^[a-zA-Z0-9_-]+$"
    },
    "title": {
      "type": "string"
    },
    "period": {
      "type": "object",
      "properties": {
        "start": {
          "type": "integer",
          "minimum": 0,
          "maximum": 8640000000000000
        },
        "end": {
          "type": "integer",
          "minimum": 0,
          "maximum": 8640000000000000
        }
      },
      "required": ["start", "end"]
    },
    "ties": {
      "type": "string",
      "enum": ["shared", "earliest"]
    },
    "status": {
      "type": "string",
      "enum": ["pending", "approved", "rejected"]
    },
    "total": {
      "type": "integer",
      "minimum": 0,
      "maximum": 9007199254740991
    },
    "winners": {
      "type": "integer",
      "minimum": 0,
      "maximum": 9007199254740991
    },
    "createdAt": {
      "type": "integer",
      "minimum": 0,
      "maximum": 8640000000000000
    },
    "approvedAt": {
      "anyOf": [
        {
          "type": "integer",
          "minimum": 0,
          "maximum": 8640000000000000
        },
        {
          "type": "null"
        }
      ]
    },
    "approvedBy": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
    },
    "approvalReason": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
    },
    "prizes": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "ranks": {
            "type": "array",
            "prefixItems": [
              {
                "type": "integer",
                "exclusiveMinimum": 0,
                "maximum": 9007199254740991
              },
              {
                "type": "integer",
                "exclusiveMinimum": 0,
                "maximum": 9007199254740991
              }
            ]
          },
          "rewards": {
            "minItems": 1,
            "maxItems": 20,
            "type": "array",
            "items": {
              "anyOf": [
                {
                  "type": "object",
                  "properties": {
                    "kind": {
                      "default": "points",
                      "type": "string",
                      "const": "points"
                    },
                    "balance": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 100,
                      "pattern": "^[a-zA-Z0-9_-]+$"
                    },
                    "amount": {
                      "type": "integer",
                      "minimum": 0,
                      "maximum": 2147483647
                    }
                  },
                  "required": ["kind", "balance", "amount"]
                },
                {
                  "type": "object",
                  "properties": {
                    "kind": {
                      "type": "string",
                      "const": "tier"
                    },
                    "reward": {
                      "type": "object",
                      "properties": {
                        "badge": {
                          "type": "string",
                          "minLength": 1,
                          "maxLength": 100,
                          "pattern": "^[a-zA-Z0-9_-]+$"
                        },
                        "group": {
                          "type": "string",
                          "minLength": 1,
                          "maxLength": 100,
                          "pattern": "^[a-zA-Z0-9_-]+$"
                        },
                        "id": {
                          "type": "string",
                          "minLength": 1,
                          "maxLength": 100,
                          "pattern": "^[a-zA-Z0-9_-]+$"
                        },
                        "title": {
                          "type": "string",
                          "minLength": 1,
                          "maxLength": 120
                        },
                        "tiers": {
                          "minItems": 1,
                          "maxItems": 20,
                          "type": "array",
                          "items": {
                            "type": "object",
                            "properties": {
                              "id": {
                                "type": "string",
                                "minLength": 1,
                                "maxLength": 100,
                                "pattern": "^[a-zA-Z0-9_-]+$"
                              },
                              "title": {
                                "type": "string",
                                "minLength": 1,
                                "maxLength": 120
                              }
                            },
                            "required": ["id", "title"]
                          }
                        },
                        "inventory": {
                          "type": "object",
                          "properties": {
                            "kind": {
                              "type": "string",
                              "const": "untracked"
                            }
                          },
                          "required": ["kind"]
                        },
                        "fulfillment": {
                          "oneOf": [
                            {
                              "type": "object",
                              "properties": {
                                "kind": {
                                  "type": "string",
                                  "const": "staff-handover"
                                }
                              },
                              "required": ["kind"],
                              "additionalProperties": false
                            },
                            {
                              "type": "object",
                              "properties": {
                                "kind": {
                                  "type": "string",
                                  "const": "automatic"
                                }
                              },
                              "required": ["kind"],
                              "additionalProperties": false
                            }
                          ]
                        }
                      },
                      "required": [
                        "id",
                        "title",
                        "tiers",
                        "inventory",
                        "fulfillment"
                      ]
                    },
                    "tier": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 100,
                      "pattern": "^[a-zA-Z0-9_-]+$"
                    }
                  },
                  "required": ["kind", "reward", "tier"]
                }
              ]
            }
          }
        },
        "required": ["ranks", "rewards"]
      }
    },
    "excluded": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 100,
        "pattern": "^[a-zA-Z0-9_-]+$"
      }
    },
    "unmeasured": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "content": {
            "type": "string"
          },
          "member": {
            "type": "string",
            "minLength": 1,
            "maxLength": 100,
            "pattern": "^[a-zA-Z0-9_-]+$"
          },
          "observedAt": {
            "anyOf": [
              {
                "type": "integer",
                "minimum": 0,
                "maximum": 8640000000000000
              },
              {
                "type": "null"
              }
            ]
          }
        },
        "required": ["content", "member", "observedAt"]
      }
    },
    "pool": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "amount": {
              "type": "string",
              "pattern": "^(0|[1-9]\\d{0,77})$"
            },
            "unit": {
              "anyOf": [
                {
                  "type": "string",
                  "const": "points"
                },
                {
                  "type": "object",
                  "properties": {
                    "code": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 32
                    },
                    "decimals": {
                      "type": "integer",
                      "minimum": 0,
                      "maximum": 36
                    }
                  },
                  "required": ["code", "decimals"]
                }
              ]
            },
            "ledger": {
              "type": "string",
              "minLength": 1,
              "maxLength": 100,
              "pattern": "^[a-zA-Z0-9_-]+$"
            },
            "minScore": {
              "type": "integer",
              "minimum": -9007199254740991,
              "maximum": 9007199254740991
            },
            "allocated": {
              "type": "string",
              "pattern": "^(0|[1-9]\\d{0,77})$"
            },
            "unallocated": {
              "type": "string",
              "pattern": "^(0|[1-9]\\d{0,77})$"
            }
          },
          "required": ["amount", "unit", "allocated", "unallocated"]
        },
        {
          "type": "null"
        }
      ]
    },
    "rows": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "member": {
            "type": "string",
            "minLength": 1,
            "maxLength": 100,
            "pattern": "^[a-zA-Z0-9_-]+$"
          },
          "score": {
            "type": "integer",
            "minimum": -9007199254740991,
            "maximum": 9007199254740991
          },
          "rank": {
            "type": "integer",
            "exclusiveMinimum": 0,
            "maximum": 9007199254740991
          },
          "position": {
            "type": "integer",
            "exclusiveMinimum": 0,
            "maximum": 9007199254740991
          },
          "scoredAt": {
            "type": "integer",
            "minimum": 0,
            "maximum": 8640000000000000
          },
          "prizes": {
            "type": "array",
            "items": {
              "type": "integer",
              "minimum": 0,
              "maximum": 9007199254740991
            }
          },
          "completion": {
            "anyOf": [
              {
                "type": "string",
                "minLength": 1,
                "maxLength": 100,
                "pattern": "^[a-zA-Z0-9_-]+$"
              },
              {
                "type": "null"
              }
            ]
          },
          "revokedAt": {
            "anyOf": [
              {
                "type": "integer",
                "minimum": 0,
                "maximum": 8640000000000000
              },
              {
                "type": "null"
              }
            ]
          },
          "share": {
            "type": "string",
            "pattern": "^(0|[1-9]\\d{0,77})$"
          }
        },
        "required": [
          "member",
          "score",
          "rank",
          "position",
          "scoredAt",
          "prizes",
          "completion",
          "revokedAt"
        ]
      }
    },
    "closing": {
      "type": "object",
      "properties": {
        "startedAt": {
          "type": "integer",
          "minimum": 0,
          "maximum": 9007199254740991
        },
        "deadline": {
          "type": "integer",
          "minimum": 0,
          "maximum": 9007199254740991
        }
      },
      "required": ["startedAt", "deadline"]
    },
    "rejectedAt": {
      "anyOf": [
        {
          "type": "integer",
          "minimum": 0,
          "maximum": 9007199254740991
        },
        {
          "type": "null"
        }
      ]
    },
    "rejectedBy": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
    },
    "rejectionReason": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
    }
  },
  "required": [
    "id",
    "leaderboard",
    "version",
    "title",
    "period",
    "ties",
    "status",
    "total",
    "winners",
    "createdAt",
    "approvedAt",
    "approvedBy",
    "approvalReason",
    "prizes",
    "excluded",
    "rows",
    "rejectedAt",
    "rejectedBy",
    "rejectionReason"
  ]
}
