{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.comparafactura.es/observatorio/datos.schema.json",
  "title": "Esquema de datos del Observatorio ComparaFactura",
  "description": "Estructura de la edición 1.1 del conjunto de indicadores públicos sobre electricidad doméstica en España.",
  "type": "object",
  "required": [
    "dataset_id",
    "name",
    "description",
    "publisher",
    "language",
    "geographic_coverage",
    "date_published",
    "date_modified",
    "version",
    "schema_url",
    "methodology_url",
    "license_note",
    "status_vocabulary",
    "citation_template",
    "sources",
    "observations",
    "regional_consumption_2025"
  ],
  "properties": {
    "dataset_id": {"type": "string"},
    "name": {"type": "string"},
    "description": {"type": "string"},
    "publisher": {
      "type": "object",
      "required": ["name", "url", "contact"],
      "properties": {
        "name": {"type": "string"},
        "url": {"type": "string", "format": "uri"},
        "contact": {"type": "string", "format": "email"}
      },
      "additionalProperties": false
    },
    "language": {"const": "es"},
    "geographic_coverage": {"const": "España"},
    "date_published": {"type": "string", "format": "date"},
    "date_modified": {"type": "string", "format": "date"},
    "version": {"type": "string"},
    "schema_url": {"type": "string", "format": "uri"},
    "methodology_url": {"type": "string", "format": "uri"},
    "license_note": {"type": "string"},
    "status_vocabulary": {
      "type": "object",
      "required": ["observado", "encuesta", "estimación publicada", "derivado"],
      "additionalProperties": {"type": "string"}
    },
    "citation_template": {"type": "string"},
    "sources": {
      "type": "array",
      "minItems": 1,
      "items": {"$ref": "#/$defs/source"}
    },
    "observations": {
      "type": "array",
      "minItems": 1,
      "items": {"$ref": "#/$defs/observation"}
    },
    "regional_consumption_2025": {
      "type": "array",
      "minItems": 1,
      "items": {"$ref": "#/$defs/regionalConsumption"}
    }
  },
  "additionalProperties": false,
  "$defs": {
    "source": {
      "type": "object",
      "required": ["id", "publisher", "title", "publication_date", "coverage", "source_type", "url"],
      "properties": {
        "id": {"type": "string"},
        "publisher": {"type": "string"},
        "title": {"type": "string"},
        "publication_date": {"type": "string", "format": "date"},
        "coverage": {"type": "string"},
        "source_type": {"type": "string"},
        "sample": {"type": "string"},
        "funding_disclosure": {"type": "string"},
        "url": {"type": "string", "format": "uri"},
        "document_url": {"type": "string", "format": "uri"}
      },
      "additionalProperties": false
    },
    "observation": {
      "type": "object",
      "required": ["id", "label", "value", "unit", "display_value", "period", "population", "status", "source_id", "source_location", "limitations", "topics"],
      "properties": {
        "id": {"type": "string", "pattern": "^CF-"},
        "label": {"type": "string"},
        "value": {"type": "number"},
        "unit": {"type": "string"},
        "display_value": {"type": "string"},
        "period": {"type": "string"},
        "population": {"type": "string"},
        "status": {"enum": ["observado", "encuesta", "estimación publicada", "derivado"]},
        "source_id": {"type": "string"},
        "source_location": {"type": "string"},
        "formula": {"type": "string"},
        "limitations": {"type": "string"},
        "topics": {"type": "array", "items": {"type": "string"}, "minItems": 1}
      },
      "additionalProperties": false
    },
    "regionalConsumption": {
      "type": "object",
      "required": ["region", "domestic_supply_points", "network_consumption_mwh", "derived_kwh_per_supply"],
      "properties": {
        "region": {"type": "string"},
        "domestic_supply_points": {"type": "integer", "minimum": 0},
        "network_consumption_mwh": {"type": "integer", "minimum": 0},
        "derived_kwh_per_supply": {"type": "integer", "minimum": 0}
      },
      "additionalProperties": false
    }
  }
}
