{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://proyectolibero.org/schemas/edition.schema.json",
  "title": "Edición (cualquier arquetipo)",
  "type": "object",
  "required": [
    "schemaVersion", "archetype", "sport", "competition", "edition",
    "status", "teamMode", "results", "coverage", "sources", "asOf"
  ],
  "properties": {
    "schemaVersion": { "const": 2 },
    "archetype": { "enum": ["encounter", "ranking", "timed", "scored"] },
    "sport": { "type": "string" },
    "competition": { "type": "string" },
    "edition": { "type": ["integer", "string"] },
    "status": { "const": "closed" },
    "teamMode": { "enum": ["country", "club", "individual"] },
    "hosts": { "type": "array", "items": { "type": "string" } },
    "teamsCount": { "type": "integer", "minimum": 0 },
    "results": {
      "type": "object",
      "required": ["champion", "runnerUp"],
      "properties": {
        "champion": { "type": ["string", "object", "null"] },
        "runnerUp": { "type": ["string", "object", "null"] },
        "third": { "type": ["string", "object", "null"] },
        "finalMatchId": { "type": ["string", "null"] },
        "decidedBy": { "type": "string", "enum": ["final", "standings"] },
        "coChampions": { "type": "array", "items": { "type": "string" }, "minItems": 2 }
      }
    },
    "coverage": {
      "type": "object",
      "required": ["matches", "scope", "complete"],
      "additionalProperties": true,
      "properties": {
        "matches": { "type": "integer", "minimum": 0 },
        "scorers": { "type": "integer", "minimum": 0 },
        "goals": { "type": "integer", "minimum": 0 },
        "scope": { "enum": ["all", "final"] },
        "complete": { "type": "boolean" },
        "missingPhases": {
          "type": "array",
          "items": { "enum": ["qualifying", "group", "knockout", "ranking", "heat"] }
        },
        "partialPhases": {
          "type": "array",
          "items": { "enum": ["qualifying", "group", "knockout", "ranking", "heat"] }
        },
        "scorersComplete": { "type": "boolean" },
        "matchsheets": {
          "type": "object",
          "required": ["complete"],
          "additionalProperties": false,
          "properties": {
            "complete": { "type": "boolean" }
          }
        },
        "note": { "type": "string" }
      }
    },
    "sources": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": ["id", "name", "url", "retrieved", "license", "redistribute"],
        "additionalProperties": false,
        "properties": {
          "id": { "type": "string" },
          "name": { "type": "string" },
          "url": { "type": "string", "format": "uri" },
          "retrieved": { "type": "string", "format": "date" },
          "author": { "type": "string" },
          "publisher": { "type": "string" },
          "license": { "type": "string" },
          "redistribute": { "type": "boolean" },
          "note": { "type": "string" }
        }
      }
    },
    "asOf": { "type": "string", "format": "date" },
    "matches": { "type": "array", "items": { "$ref": "#/$defs/encounterResult" } },
    "events": { "type": "array", "items": { "$ref": "#/$defs/rankingResult" } },
    "scorers": { "type": "array", "items": { "$ref": "#/$defs/scorer" } },
    "groups": { "type": "array", "items": { "$ref": "#/$defs/groupTable" } }
  },
  "allOf": [
    {
      "if": { "properties": { "archetype": { "const": "encounter" } } },
      "then": { "required": ["matches"] }
    },
    {
      "if": { "properties": { "archetype": { "enum": ["ranking", "timed", "scored"] } } },
      "then": { "required": ["events"] }
    }
  ],
  "$defs": {
    "provenance": {
      "type": "object",
      "required": ["primary"],
      "additionalProperties": false,
      "properties": {
        "primary": { "type": "string" },
        "rejected": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["source", "field", "reason"],
            "additionalProperties": false,
            "properties": {
              "source": { "type": "string" },
              "field": { "type": "string" },
              "value": {},
              "reason": { "type": "string", "minLength": 3 }
            }
          }
        }
      }
    },
    "encounterResult": {
      "type": "object",
      "required": ["id", "stage", "home", "away"],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string" },
        "stage": { "type": "string" },
        "group": { "type": ["string", "null"] },
        "matchday": { "type": ["integer", "string", "null"] },
        "date": { "type": ["string", "null"], "format": "date" },
        "city": { "type": ["string", "null"] },
        "home": { "type": "string" },
        "away": { "type": "string" },
        "score": {
          "anyOf": [
            { "type": "array", "items": { "type": "integer" }, "minItems": 2, "maxItems": 2 },
            { "type": "null" }
          ]
        },
        "status": { "enum": ["played", "notplayed", "awarded", "walkover"] },
        "notes": {
          "type": "array",
          "items": { "enum": ["aet", "pen", "replay", "awarded", "walkover", "notplayed", "withdrawn", "singleMatch"] }
        },
        "pens": {
          "anyOf": [
            { "type": "array", "items": { "type": "integer" }, "minItems": 2, "maxItems": 2 },
            { "type": "null" }
          ]
        },
        "extra": {
          "type": "object",
          "additionalProperties": true,
          "properties": {
            "referee": {
              "type": "object",
              "required": ["name"],
              "properties": {
                "name": { "type": "string" },
                "country": { "type": "string" },
                "raw": { "type": "string" }
              }
            },
            "attendance": { "type": ["integer", "null"] },
            "events": {
              "type": "array",
              "items": {
                "type": "object",
                "required": ["minute", "player", "team", "score"],
                "properties": {
                  "minute": { "type": ["integer", "string"] },
                  "player": { "type": "string" },
                  "team": { "type": "string" },
                  "score": { "type": "array", "items": { "type": "integer" }, "minItems": 2, "maxItems": 2 }
                }
              }
            }
          }
        },
        "sheet": {
          "type": "object",
          "required": ["source", "text"],
          "additionalProperties": false,
          "properties": {
            "source": { "type": "string" },
            "text": { "type": "string" },
            "lang": { "type": "string" }
          }
        },
        "provenance": { "$ref": "#/$defs/provenance" },
        "raw": { "type": ["string", "null"] }
      }
    },
    "rankingResult": {
      "type": "object",
      "required": ["id", "stage", "label", "results"],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string" },
        "stage": { "type": "string" },
        "date": { "type": ["string", "null"], "format": "date" },
        "city": { "type": ["string", "null"] },
        "label": { "type": "string" },
        "discipline": { "type": ["string", "null"] },
        "gender": { "enum": ["men", "women", "mixed"] },
        "provenance": { "$ref": "#/$defs/provenance" },
        "raw": { "type": ["string", "null"] },
        "results": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "required": ["place", "name", "country"],
            "properties": {
              "place": { "type": "integer", "minimum": 1 },
              "medal": { "type": ["string", "null"], "enum": ["gold", "silver", "bronze", null] },
              "entity": { "enum": ["athlete", "team"] },
              "name": { "type": "string" },
              "country": { "type": "string" },
              "athleteKey": { "type": ["string", "null"] },
              "teamKey": { "type": ["string", "null"] },
              "members": { "type": "array", "items": { "type": "string" } },
              "time": {
                "type": "object",
                "required": ["raw"],
                "properties": {
                  "raw": { "type": "string" },
                  "ms": { "type": ["integer", "null"] }
                }
              },
              "score": { "type": ["string", "number", "null"] },
              "points": { "type": ["number", "null"] },
              "raw": { "type": ["string", "null"] }
            }
          }
        }
      }
    },
    "groupTable": {
      "type": "object",
      "required": ["stage", "letter", "standings"],
      "additionalProperties": false,
      "properties": {
        "stage": { "type": "string" },
        "letter": { "type": "string" },
        "provenance": { "$ref": "#/$defs/provenance" },
        "raw": { "type": ["string", "null"] },
        "standings": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "required": ["team", "p", "w", "d", "l", "gf", "ga", "pts"],
            "additionalProperties": false,
            "properties": {
              "team": { "type": "string" },
              "p": { "type": "integer", "minimum": 0 },
              "w": { "type": "integer", "minimum": 0 },
              "d": { "type": "integer", "minimum": 0 },
              "l": { "type": "integer", "minimum": 0 },
              "gf": { "type": "integer", "minimum": 0 },
              "ga": { "type": "integer", "minimum": 0 },
              "pts": { "type": "integer", "minimum": 0 },
              "deduction": {
                "type": "integer",
                "minimum": 0,
                "description": "Puntos restados por sanción (D39/ADR-028). El validador exige pts = (2|3)·w + d − deduction y un motivo si deduction > 0."
              },
              "deductionReason": { "type": "string", "minLength": 1 },
              "notes": { "type": "string", "minLength": 1 }
            }
          }
        }
      }
    },
    "scorer": {
      "type": "object",
      "required": ["player", "country", "goals", "scope"],
      "additionalProperties": false,
      "properties": {
        "player": { "type": "string" },
        "country": { "type": "string" },
        "goals": { "type": "integer", "minimum": 1 },
        "scope": { "enum": ["all", "final"] },
        "ownGoal": { "type": "boolean" },
        "annulled": { "type": "boolean" },
        "raw": { "type": "string" }
      }
    }
  }
}
