{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://soporte-dispositivos.local/schema/ranking.schema.json",
  "title": "Ranking contract v2 (the only input of the site; one file per category)",
  "type": "object",
  "required": [
    "contract_version",
    "meta",
    "scores",
    "bands",
    "metrics",
    "categories",
    "ranges",
    "ranking",
    "brands",
    "sources"
  ],
  "additionalProperties": false,
  "properties": {
    "contract_version": {
      "const": 2
    },
    "meta": {
      "type": "object",
      "required": [
        "category",
        "generated_at",
        "methodology_version",
        "is_example",
        "ranked_by",
        "min_coverage",
        "scoring_levels",
        "how_it_works"
      ],
      "additionalProperties": false,
      "properties": {
        "category": {
          "$ref": "#/$defs/slug"
        },
        "generated_at": {
          "type": "string",
          "format": "date-time"
        },
        "methodology_version": {
          "type": "string",
          "minLength": 1
        },
        "is_example": {
          "type": "boolean"
        },
        "ranked_by": {
          "type": "string",
          "description": "Score id that orders the rows and is the headline number"
        },
        "min_coverage": {
          "type": "number",
          "minimum": 0,
          "maximum": 1
        },
        "scoring_levels": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/level"
          },
          "minItems": 1
        },
        "how_it_works": {
          "$ref": "#/$defs/text"
        },
        "legal_reference": {
          "type": "object",
          "required": [
            "label",
            "text",
            "url"
          ],
          "additionalProperties": false,
          "properties": {
            "label": {
              "$ref": "#/$defs/text"
            },
            "text": {
              "$ref": "#/$defs/text"
            },
            "url": {
              "type": "string",
              "pattern": "^https://"
            }
          }
        },
        "sector_reliability": {
          "type": "number",
          "minimum": 0,
          "maximum": 1,
          "description": "Mean share of past promises delivered, over all brands: what a brand without history is assumed to be"
        },
        "updates_floor": {
          "type": "number",
          "minimum": 0,
          "maximum": 1,
          "description": "Share of the expected years that always stands; the updates move the rest (methodology.updates_floor)"
        },
        "expected_years": {
          "type": "object",
          "required": [
            "horizon",
            "above_factor",
            "above_cap",
            "reliability_models"
          ],
          "additionalProperties": false,
          "description": "Parameters of the expected years (methodology.expected_years)",
          "properties": {
            "horizon": {
              "type": "object",
              "propertyNames": {
                "$ref": "#/$defs/key"
              },
              "additionalProperties": {
                "type": "number",
                "minimum": 0
              }
            },
            "above_factor": {
              "type": "number",
              "minimum": 0,
              "maximum": 1
            },
            "above_cap": {
              "type": "number",
              "minimum": 0
            },
            "reliability_models": {
              "type": "number",
              "minimum": 0
            }
          }
        }
      }
    },
    "scores": {
      "description": "Score definitions in display order. The one in meta.ranked_by is the headline.",
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": [
          "id",
          "status",
          "label",
          "short"
        ],
        "additionalProperties": false,
        "properties": {
          "id": {
            "$ref": "#/$defs/key"
          },
          "status": {
            "enum": [
              "stable",
              "beta"
            ]
          },
          "label": {
            "$ref": "#/$defs/text"
          },
          "short": {
            "$ref": "#/$defs/text"
          },
          "description": {
            "$ref": "#/$defs/text"
          }
        }
      }
    },
    "bands": {
      "description": "Quality bands of the headline score, highest first; the last one has min 0.",
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": [
          "id",
          "min",
          "label"
        ],
        "additionalProperties": false,
        "properties": {
          "id": {
            "$ref": "#/$defs/key"
          },
          "min": {
            "type": "number",
            "minimum": 0,
            "maximum": 100
          },
          "label": {
            "$ref": "#/$defs/text"
          }
        }
      }
    },
    "metrics": {
      "description": "The columns of this category, in display order. Every row has one figure per metric.",
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": [
          "id",
          "score",
          "kind",
          "unit",
          "better",
          "scale_max",
          "label",
          "short"
        ],
        "additionalProperties": false,
        "properties": {
          "id": {
            "$ref": "#/$defs/key"
          },
          "score": {
            "$ref": "#/$defs/key",
            "description": "Score this metric feeds"
          },
          "kind": {
            "enum": [
              "years",
              "delay"
            ]
          },
          "unit": {
            "enum": [
              "years",
              "days"
            ]
          },
          "better": {
            "enum": [
              "higher",
              "lower"
            ]
          },
          "scale_max": {
            "type": "number",
            "exclusiveMinimum": 0,
            "description": "Value at which a bar is full (years) or empty (delay)"
          },
          "label": {
            "$ref": "#/$defs/text"
          },
          "short": {
            "$ref": "#/$defs/text"
          }
        }
      }
    },
    "categories": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": [
          "id",
          "name",
          "active"
        ],
        "additionalProperties": false,
        "properties": {
          "id": {
            "$ref": "#/$defs/slug"
          },
          "name": {
            "$ref": "#/$defs/text"
          },
          "active": {
            "type": "boolean",
            "description": "false = coming soon"
          }
        }
      }
    },
    "ranges": {
      "description": "Range filter, in display order. The first one is the default.",
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": [
          "id",
          "name"
        ],
        "additionalProperties": false,
        "properties": {
          "id": {
            "$ref": "#/$defs/slug"
          },
          "name": {
            "$ref": "#/$defs/text"
          }
        }
      }
    },
    "ranking": {
      "description": "Rows per range id, already sorted. Rows without a headline score go last with position null.",
      "type": "object",
      "propertyNames": {
        "$ref": "#/$defs/slug"
      },
      "additionalProperties": {
        "type": "array",
        "items": {
          "$ref": "#/$defs/row"
        }
      }
    },
    "brands": {
      "type": "object",
      "propertyNames": {
        "$ref": "#/$defs/slug"
      },
      "additionalProperties": {
        "$ref": "#/$defs/brand"
      }
    },
    "sources": {
      "type": "object",
      "propertyNames": {
        "pattern": "^src-[a-z0-9-]+$"
      },
      "additionalProperties": {
        "$ref": "#/$defs/source"
      }
    },
    "race": {
      "description": "Tennis-style second ranking (points per tournament, last 12 months). Shown apart; never part of the score.",
      "type": "object",
      "required": [
        "window",
        "rules",
        "min_monthly",
        "tournaments",
        "ranking"
      ],
      "additionalProperties": false,
      "properties": {
        "window": {
          "type": "object",
          "required": [
            "from",
            "to"
          ],
          "additionalProperties": false,
          "properties": {
            "from": {
              "type": "string",
              "format": "date"
            },
            "to": {
              "type": "string",
              "format": "date"
            }
          }
        },
        "rules": {
          "type": "object",
          "required": [
            "en"
          ],
          "properties": {
            "en": {
              "type": "string",
              "minLength": 1
            }
          },
          "additionalProperties": {
            "type": "string"
          }
        },
        "tournaments": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "id",
              "kind",
              "name",
              "date"
            ],
            "additionalProperties": false,
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^[ms]-[a-z0-9-]+$"
              },
              "kind": {
                "enum": [
                  "monthly",
                  "slam"
                ]
              },
              "name": {
                "type": "object",
                "required": [
                  "en"
                ],
                "properties": {
                  "en": {
                    "type": "string",
                    "minLength": 1
                  }
                },
                "additionalProperties": {
                  "type": "string"
                }
              },
              "date": {
                "type": "string",
                "format": "date"
              },
              "closes": {
                "type": "string",
                "format": "date"
              },
              "open": {
                "type": "boolean",
                "description": "Not scored yet: a monthly tournament closes 90 days after its bulletin"
              }
            }
          }
        },
        "min_monthly": {
          "type": "integer",
          "minimum": 0
        },
        "ranking": {
          "description": "One table per range id (overall plus each range)",
          "type": "object",
          "additionalProperties": {
            "type": "array",
            "items": {
              "type": "object",
              "required": [
                "position",
                "brand",
                "points",
                "played",
                "titles",
                "results",
                "average"
              ],
              "additionalProperties": false,
              "properties": {
                "position": {
                  "type": [
                    "integer",
                    "null"
                  ],
                  "minimum": 1
                },
                "brand": {
                  "$ref": "#/$defs/slug"
                },
                "points": {
                  "type": "integer",
                  "minimum": 0
                },
                "played": {
                  "type": "integer",
                  "minimum": 0
                },
                "titles": {
                  "type": "integer",
                  "minimum": 0
                },
                "results": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "required": [
                      "t",
                      "round",
                      "points",
                      "days"
                    ],
                    "additionalProperties": false,
                    "properties": {
                      "t": {
                        "type": "string"
                      },
                      "round": {
                        "enum": [
                          "W",
                          "F",
                          "SF",
                          "QF",
                          "R16",
                          "R32",
                          "R64",
                          "R128",
                          "R1"
                        ]
                      },
                      "points": {
                        "type": "integer",
                        "minimum": 0
                      },
                      "days": {
                        "description": "null: not delivered (a closed Grand Slam the brand documentedly missed; 0 points)",
                        "type": [
                          "integer",
                          "null"
                        ],
                        "minimum": 0
                      },
                      "missed": {
                        "type": "boolean"
                      }
                    }
                  }
                },
                "previous_position": {
                  "description": "Race place 7 days earlier (same data); null: not ranked then",
                  "type": [
                    "integer",
                    "null"
                  ],
                  "minimum": 1
                },
                "average": {
                  "type": "integer",
                  "minimum": 0,
                  "description": "Points per tournament played: the order of the Race"
                }
              }
            }
          }
        },
        "movement_since": {
          "description": "Date of the latest tournament: previous_position is the place the day before it",
          "type": "string",
          "format": "date"
        },
        "months": {
          "type": "integer",
          "minimum": 1
        }
      }
    }
  },
  "$defs": {
    "text": {
      "$ref": "localized.schema.json"
    },
    "slug": {
      "type": "string",
      "pattern": "^[a-z0-9-]+$"
    },
    "key": {
      "type": "string",
      "pattern": "^[a-z0-9_]+$"
    },
    "level": {
      "enum": [
        "official",
        "press",
        "unverified"
      ]
    },
    "datum": {
      "description": "A figure with its provenance. `level` is copied from the source. status: ok (value known), promised (no measurement, only a promised frequency in `proxy`), not_published (the brand commits to nothing), no_source (we do not know), conflicting (sources disagree; value is the conservative one). at_least: the value is a minimum.",
      "type": "object",
      "required": [
        "value",
        "status",
        "level",
        "source"
      ],
      "additionalProperties": false,
      "properties": {
        "value": {
          "type": [
            "number",
            "null"
          ],
          "minimum": 0
        },
        "status": {
          "enum": [
            "ok",
            "promised",
            "not_published",
            "no_source",
            "conflicting"
          ]
        },
        "level": {
          "oneOf": [
            {
              "$ref": "#/$defs/level"
            },
            {
              "type": "null"
            }
          ]
        },
        "source": {
          "type": [
            "string",
            "null"
          ],
          "pattern": "^src-[a-z0-9-]+$"
        },
        "proxy": {
          "enum": [
            "monthly",
            "bimonthly",
            "quarterly",
            "biannual",
            "irregular"
          ]
        },
        "at_least": {
          "type": "boolean"
        },
        "from_range": {
          "description": "Delay not measured in this range: taken from another range of the same brand (they usually update together)",
          "type": "string"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "status": {
                "enum": [
                  "ok",
                  "conflicting"
                ]
              }
            }
          },
          "then": {
            "properties": {
              "value": {
                "type": "number"
              },
              "level": {
                "$ref": "#/$defs/level"
              },
              "source": {
                "type": "string"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "status": {
                "const": "promised"
              }
            }
          },
          "then": {
            "required": [
              "proxy"
            ],
            "properties": {
              "value": {
                "type": "null"
              },
              "level": {
                "$ref": "#/$defs/level"
              },
              "source": {
                "type": "string"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "status": {
                "enum": [
                  "not_published",
                  "no_source"
                ]
              }
            }
          },
          "then": {
            "properties": {
              "value": {
                "type": "null"
              }
            }
          }
        }
      ]
    },
    "row": {
      "description": "One brand in one range. Self-contained: everything its card needs is here.",
      "type": "object",
      "required": [
        "position",
        "brand",
        "scores",
        "coverage",
        "trend",
        "provisional",
        "legal_minimum",
        "figures",
        "policy",
        "models"
      ],
      "additionalProperties": false,
      "properties": {
        "position": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 1
        },
        "brand": {
          "$ref": "#/$defs/slug"
        },
        "scores": {
          "description": "One entry per score id: 0-100, or null when coverage is below meta.min_coverage",
          "type": "object",
          "additionalProperties": {
            "type": [
              "number",
              "null"
            ],
            "minimum": 0,
            "maximum": 100
          }
        },
        "coverage": {
          "description": "One entry per score id: share (0-1) of that score's weight backed by scored figures",
          "type": "object",
          "additionalProperties": {
            "type": "number",
            "minimum": 0,
            "maximum": 1
          }
        },
        "trend": {
          "enum": [
            "up",
            "down",
            "same",
            null
          ]
        },
        "provisional": {
          "type": "boolean",
          "description": "The headline score rests on gaps, minimums or disputed figures"
        },
        "legal_minimum": {
          "enum": [
            "below",
            "meets",
            "unknown",
            null
          ],
          "description": "Published policy against meta.legal_reference"
        },
        "figures": {
          "type": "object",
          "propertyNames": {
            "$ref": "#/$defs/key"
          },
          "additionalProperties": {
            "$ref": "#/$defs/datum"
          }
        },
        "policy": {
          "oneOf": [
            {
              "$ref": "#/$defs/text"
            },
            {
              "type": "null"
            }
          ],
          "description": "The brand's policy for this range in one sentence"
        },
        "proven": {
          "description": "Years the brand has already delivered on older phones of this range, per years metric (0 when none). Present exactly when the range's figure is a promise",
          "type": "object",
          "propertyNames": {
            "$ref": "#/$defs/key"
          },
          "additionalProperties": {
            "type": "number",
            "minimum": 0
          }
        },
        "models": {
          "type": "array",
          "maxItems": 3,
          "items": {
            "type": "object",
            "required": [
              "name",
              "support_end",
              "ended"
            ],
            "additionalProperties": false,
            "properties": {
              "name": {
                "type": "string",
                "minLength": 1
              },
              "support_end": {
                "type": "string",
                "pattern": "^\\d{4}-(0[1-9]|1[0-2])$"
              },
              "ended": {
                "type": "boolean"
              }
            }
          }
        },
        "previous_position": {
          "description": "Position in this range in the contract of about a week before (null: not ranked then or no history)",
          "type": [
            "integer",
            "null"
          ],
          "minimum": 1
        },
        "expected_years": {
          "description": "Years a buyer can expect (methodology 4.0): the promise cut down by the brand's reliability and by what it has not yet proven; the delivered figure when there is no promise. null when the figure does not score",
          "type": "object",
          "required": [
            "security_years",
            "os_years"
          ],
          "additionalProperties": false,
          "properties": {
            "security_years": {
              "type": [
                "number",
                "null"
              ],
              "minimum": 0
            },
            "os_years": {
              "type": [
                "number",
                "null"
              ],
              "minimum": 0
            }
          }
        },
        "imputed": {
          "description": "Metric ids that carry no measurement of their own here and took the median of this range (the patch delay)",
          "type": "array",
          "items": {
            "$ref": "#/$defs/key"
          },
          "uniqueItems": true
        }
      }
    },
    "brand": {
      "type": "object",
      "required": [
        "name",
        "track_record"
      ],
      "additionalProperties": false,
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "group": {
          "$ref": "#/$defs/slug"
        },
        "track_record": {
          "description": "Promised against delivered on models whose promise has expired. no_history = nothing to check yet.",
          "type": "object",
          "required": [
            "status",
            "ratio",
            "checked_models"
          ],
          "additionalProperties": false,
          "properties": {
            "status": {
              "enum": [
                "kept",
                "short",
                "no_history"
              ]
            },
            "ratio": {
              "type": [
                "number",
                "null"
              ],
              "minimum": 0,
              "maximum": 1
            },
            "checked_models": {
              "type": "integer",
              "minimum": 0
            }
          }
        },
        "reliability": {
          "description": "Share of its past promises the brand delivered, pulled towards meta.sector_reliability with the weight of a few models: (sum of ratios + k * sector mean) / (n + k)",
          "type": "object",
          "required": [
            "value",
            "checked_models"
          ],
          "additionalProperties": false,
          "properties": {
            "value": {
              "type": "number",
              "minimum": 0,
              "maximum": 1
            },
            "checked_models": {
              "type": "integer",
              "minimum": 0
            }
          }
        }
      }
    },
    "source": {
      "type": "object",
      "required": [
        "url",
        "publisher",
        "level",
        "accessed_on"
      ],
      "additionalProperties": false,
      "properties": {
        "url": {
          "type": "string",
          "format": "uri",
          "pattern": "^https?://"
        },
        "publisher": {
          "enum": [
            "manufacturer",
            "regulator",
            "aggregator",
            "press",
            "community"
          ]
        },
        "level": {
          "$ref": "#/$defs/level"
        },
        "accessed_on": {
          "type": "string",
          "format": "date"
        }
      }
    }
  }
}
