{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://nicoseijas.github.io/cauce/data/schema/estado-v3.schema.json",
  "title": "Cauce — estado hídrico v3",
  "description": "Contrato público del estado consolidado. Las banderas QC determinan aptitud; la presencia de un valor no implica que sea utilizable.",
  "type": "object",
  "required": [
    "schema_version",
    "generado",
    "fuentes",
    "fuentes_detalle",
    "estaciones",
    "factores_curso",
    "activacion",
    "activacion_cobertura",
    "control_calidad"
  ],
  "properties": {
    "schema_version": {
      "const": 3
    },
    "generado": {
      "$ref": "#/$defs/fecha_hora_iso"
    },
    "fuentes": {
      "type": "object",
      "additionalProperties": {
        "enum": [
          "ok",
          "caida",
          "vencida",
          "sin_fecha",
          "no_implementada"
        ]
      }
    },
    "fuentes_detalle": {
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "required": [
          "estado",
          "ultima_observacion",
          "ultimo_exito"
        ],
        "properties": {
          "estado": {
            "enum": [
              "ok",
              "caida",
              "vencida",
              "sin_fecha",
              "no_implementada"
            ]
          },
          "ultima_observacion": {
            "type": [
              "string",
              "null"
            ]
          },
          "ultimo_exito": {
            "type": [
              "string",
              "null"
            ]
          }
        }
      }
    },
    "estaciones": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/estacion"
      }
    },
    "factores_curso": {
      "type": "object",
      "additionalProperties": {
        "$ref": "#/$defs/factor"
      }
    },
    "activacion": {
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "required": [
          "estacion",
          "nivel",
          "nivel_horas",
          "periodo_activo"
        ],
        "properties": {
          "estacion": {
            "type": "string"
          },
          "nivel": {
            "type": "number"
          },
          "nivel_horas": {
            "type": "number"
          },
          "periodo_activo": {
            "type": "integer"
          },
          "proximo": {
            "type": "object",
            "required": [
              "periodo",
              "faltan_m"
            ],
            "properties": {
              "periodo": {
                "type": "integer"
              },
              "faltan_m": {
                "type": "number"
              }
            }
          }
        }
      }
    },
    "activacion_cobertura": {
      "type": "object",
      "required": [
        "configuradas",
        "habilitadas",
        "evaluadas",
        "rechazadas_qc",
        "con_estacion_superficial",
        "con_curso_compatible",
        "tipos_inundacion",
        "bloqueos",
        "fuente_disponible"
      ],
      "properties": {
        "configuradas": {
          "type": "integer",
          "minimum": 0
        },
        "habilitadas": {
          "type": "integer",
          "minimum": 0
        },
        "evaluadas": {
          "type": "integer",
          "minimum": 0
        },
        "rechazadas_qc": {
          "type": "integer",
          "minimum": 0
        },
        "con_estacion_superficial": {
          "type": "integer",
          "minimum": 0
        },
        "con_curso_compatible": {
          "type": "integer",
          "minimum": 0
        },
        "tipos_inundacion": {
          "type": "object",
          "additionalProperties": {
            "type": "integer",
            "minimum": 0
          }
        },
        "bloqueos": {
          "type": "object",
          "additionalProperties": {
            "type": "integer",
            "minimum": 0
          }
        },
        "fuente_disponible": {
          "type": "boolean"
        }
      }
    },
    "control_calidad": {
      "type": "object",
      "required": [
        "version",
        "metodo",
        "resumen",
        "incidencias",
        "vigilancia_reforzada"
      ],
      "properties": {
        "version": {
          "type": "string"
        },
        "metodo": {
          "type": "object"
        },
        "resumen": {
          "type": "object"
        },
        "incidencias": {
          "type": "array"
        },
        "vigilancia_reforzada": {
          "type": "array"
        }
      }
    },
    "lluvia": {
      "oneOf": [
        {
          "type": "null"
        },
        {
          "type": "object",
          "required": [
            "hasta",
            "estaciones"
          ],
          "properties": {
            "hasta": {
              "type": "string"
            },
            "estaciones": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/estacion_lluvia"
              }
            }
          }
        }
      ]
    },
    "salto_grande": {
      "oneOf": [
        {
          "type": "null"
        },
        {
          "type": "object",
          "required": [
            "turbinado",
            "vertido",
            "total",
            "fecha",
            "horas"
          ],
          "properties": {
            "turbinado": {
              "type": "number",
              "minimum": 0
            },
            "vertido": {
              "type": "number",
              "minimum": 0
            },
            "total": {
              "type": "number",
              "minimum": 0
            },
            "fecha_local": {
              "type": "string"
            },
            "fecha": {
              "$ref": "#/$defs/fecha_hora_iso"
            },
            "horas": {
              "type": "number"
            }
          }
        }
      ]
    },
    "ina": {
      "$ref": "#/$defs/fuente_estaciones_nullable"
    },
    "ute_rio_negro": {
      "oneOf": [
        {
          "type": "null"
        },
        {
          "$ref": "#/$defs/pronostico_ute"
        }
      ]
    },
    "ana": {
      "$ref": "#/$defs/fuente_estaciones_nullable"
    },
    "sohma": {
      "$ref": "#/$defs/fuente_estaciones_nullable"
    }
  },
  "$defs": {
    "fecha_hora_iso": {
      "type": "string",
      "description": "ISO 8601 con zona explícita; admite precisión de minuto o segundo.",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}(?::\\d{2}(?:\\.\\d+)?)?(?:Z|[+-]\\d{2}:\\d{2})$"
    },
    "qc": {
      "type": "object",
      "required": [
        "estado",
        "codigos",
        "apto_informativo",
        "apto_derivados",
        "antiguedad_h",
        "controles",
        "referencia"
      ],
      "properties": {
        "estado": {
          "enum": [
            "ok",
            "vencido",
            "dudoso",
            "rechazado",
            "sin_dato"
          ]
        },
        "codigos": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "apto_informativo": {
          "type": "boolean"
        },
        "apto_derivados": {
          "type": "boolean"
        },
        "antiguedad_h": {
          "type": [
            "number",
            "null"
          ]
        },
        "controles": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "referencia": {
          "oneOf": [
            {
              "type": "null"
            },
            {
              "type": "object",
              "required": [
                "valor",
                "fecha"
              ],
              "properties": {
                "valor": {
                  "type": "number"
                },
                "fecha": {
                  "$ref": "#/$defs/fecha_hora_iso"
                }
              }
            }
          ]
        }
      }
    },
    "estacion": {
      "type": "object",
      "required": [
        "id",
        "estacion_id",
        "slug",
        "nombre",
        "clasificacion",
        "oficial",
        "fuente",
        "lat",
        "lon",
        "nivel",
        "nivel_fecha",
        "nivel_horas",
        "caudal",
        "caudal_fecha",
        "caudal_horas",
        "factor",
        "qc_nivel",
        "qc_caudal"
      ],
      "properties": {
        "id": {
          "type": [
            "integer",
            "string"
          ]
        },
        "estacion_id": {
          "type": "string",
          "pattern": "^[a-z0-9]+(-[a-z0-9]+)*$",
          "description": "Identificador canónico <organismo>-<id de origen>. Estable aunque el organismo renombre la estación."
        },
        "slug": {
          "type": "string",
          "pattern": "^[a-z0-9]+(-[a-z0-9]+)*$",
          "description": "Segmento de URL legible derivado del nombre publicado; lleva el organismo como sufijo cuando dos estaciones lo comparten."
        },
        "nombre": {
          "type": "string"
        },
        "curso": {
          "type": [
            "string",
            "null"
          ]
        },
        "clasificacion": {
          "enum": [
            "observado",
            "pronosticado",
            "estimado"
          ]
        },
        "oficial": {
          "type": "boolean"
        },
        "fuente": {
          "type": "string"
        },
        "lat": {
          "type": "number",
          "minimum": -90,
          "maximum": 90
        },
        "lon": {
          "type": "number",
          "minimum": -180,
          "maximum": 180
        },
        "nivel": {
          "type": [
            "number",
            "null"
          ]
        },
        "nivel_fecha": {
          "type": [
            "string",
            "null"
          ]
        },
        "nivel_horas": {
          "type": [
            "number",
            "null"
          ]
        },
        "caudal": {
          "type": [
            "number",
            "null"
          ]
        },
        "caudal_fecha": {
          "type": [
            "string",
            "null"
          ]
        },
        "caudal_horas": {
          "type": [
            "number",
            "null"
          ]
        },
        "factor": {
          "type": [
            "number",
            "null"
          ]
        },
        "qc_nivel": {
          "$ref": "#/$defs/qc"
        },
        "qc_caudal": {
          "$ref": "#/$defs/qc"
        }
      }
    },
    "estacion_externa": {
      "type": "object",
      "required": [
        "id",
        "estacion_id",
        "slug",
        "nombre",
        "clasificacion",
        "oficial",
        "fuente",
        "lat",
        "lon",
        "qc_nivel",
        "qc_caudal"
      ],
      "properties": {
        "id": {
          "type": [
            "integer",
            "string"
          ]
        },
        "estacion_id": {
          "type": "string",
          "pattern": "^[a-z0-9]+(-[a-z0-9]+)*$",
          "description": "Identificador canónico <organismo>-<id de origen>. Estable aunque el organismo renombre la estación."
        },
        "slug": {
          "type": "string",
          "pattern": "^[a-z0-9]+(-[a-z0-9]+)*$",
          "description": "Segmento de URL legible derivado del nombre publicado; lleva el organismo como sufijo cuando dos estaciones lo comparten."
        },
        "nombre": {
          "type": "string"
        },
        "curso": {
          "type": [
            "string",
            "null"
          ]
        },
        "clasificacion": {
          "enum": [
            "observado",
            "pronosticado",
            "estimado"
          ]
        },
        "oficial": {
          "type": "boolean"
        },
        "fuente": {
          "type": "string"
        },
        "lat": {
          "type": "number",
          "minimum": -90,
          "maximum": 90
        },
        "lon": {
          "type": "number",
          "minimum": -180,
          "maximum": 180
        },
        "nivel": {
          "type": [
            "number",
            "null"
          ]
        },
        "nivel_fecha": {
          "type": [
            "string",
            "null"
          ]
        },
        "nivel_horas": {
          "type": [
            "number",
            "null"
          ]
        },
        "caudal": {
          "type": [
            "number",
            "null"
          ]
        },
        "caudal_fecha": {
          "type": [
            "string",
            "null"
          ]
        },
        "caudal_horas": {
          "type": [
            "number",
            "null"
          ]
        },
        "fecha": {
          "type": [
            "string",
            "null"
          ]
        },
        "horas": {
          "type": [
            "number",
            "null"
          ]
        },
        "qc_nivel": {
          "$ref": "#/$defs/qc"
        },
        "qc_caudal": {
          "$ref": "#/$defs/qc"
        }
      }
    },
    "fuente_estaciones_nullable": {
      "oneOf": [
        {
          "type": "null"
        },
        {
          "type": "object",
          "required": [
            "estaciones"
          ],
          "properties": {
            "estaciones": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/estacion_externa"
              }
            }
          }
        }
      ]
    },
    "estacion_lluvia": {
      "type": "object",
      "required": [
        "nombre",
        "lat",
        "lon",
        "clasificacion",
        "oficial",
        "mm24",
        "mm72",
        "fuente"
      ],
      "properties": {
        "nombre": {
          "type": "string"
        },
        "lat": {
          "type": "number",
          "minimum": -90,
          "maximum": 90
        },
        "lon": {
          "type": "number",
          "minimum": -180,
          "maximum": 180
        },
        "clasificacion": {
          "const": "observado"
        },
        "oficial": {
          "type": "boolean"
        },
        "mm24": {
          "type": "number",
          "minimum": 0
        },
        "mm72": {
          "type": "number",
          "minimum": 0
        },
        "fecha": {
          "type": "string"
        },
        "fuente": {
          "type": "string"
        }
      }
    },
    "pronostico_ute": {
      "type": "object",
      "required": [
        "actualizado",
        "clasificacion",
        "oficial",
        "horizonte_dias",
        "probabilidad",
        "incertidumbre",
        "dias",
        "maximos"
      ],
      "properties": {
        "actualizado": {
          "$ref": "#/$defs/fecha_hora_iso"
        },
        "clasificacion": {
          "const": "pronosticado"
        },
        "oficial": {
          "type": "boolean"
        },
        "horizonte_dias": {
          "type": "integer",
          "minimum": 1
        },
        "probabilidad": {
          "type": [
            "number",
            "null"
          ]
        },
        "incertidumbre": {
          "type": "string"
        },
        "dias": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "fecha"
            ],
            "properties": {
              "fecha": {
                "type": "string",
                "format": "date"
              },
              "san_gregorio_local": {
                "type": "number"
              },
              "paso_toros_oficial": {
                "type": "number"
              },
              "mercedes_local": {
                "type": "number"
              },
              "erogado_bonete": {
                "type": "number",
                "minimum": 0
              },
              "erogado_palmar": {
                "type": "number",
                "minimum": 0
              }
            }
          }
        },
        "maximos": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "lugar",
              "nivel",
              "fecha"
            ],
            "properties": {
              "lugar": {
                "type": "string"
              },
              "nivel": {
                "type": "number"
              },
              "fecha": {
                "type": "string"
              }
            }
          }
        }
      }
    },
    "factor": {
      "type": "object",
      "required": [
        "factor",
        "estacion",
        "clasificacion",
        "insumo_clasificacion",
        "oficial",
        "fecha_insumo",
        "antiguedad_h",
        "alcance",
        "incertidumbre",
        "qc_version"
      ],
      "properties": {
        "factor": {
          "type": "number",
          "minimum": 0.05,
          "maximum": 20
        },
        "estacion": {
          "type": "string"
        },
        "clasificacion": {
          "const": "estimado"
        },
        "insumo_clasificacion": {
          "enum": [
            "observado",
            "pronosticado"
          ]
        },
        "oficial": {
          "type": "boolean"
        },
        "fecha_insumo": {
          "$ref": "#/$defs/fecha_hora_iso"
        },
        "antiguedad_h": {
          "type": "number",
          "minimum": -1,
          "maximum": 48
        },
        "alcance": {
          "type": "string"
        },
        "incertidumbre": {
          "type": "string"
        },
        "qc_version": {
          "type": "string"
        }
      }
    }
  }
}
