{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "emergence/aggregate.schema.json",
  "title": "Day aggregates (fractal topics, GIS, day stats)",
  "type": "object",
  "defs": {
    "essence_ref": {
      "type": "object",
      "required": ["id", "essence", "source_id", "url"],
      "properties": {
        "id": { "type": "string" },
        "essence": { "type": "string" },
        "source_id": { "type": "string" },
        "url": { "type": "string" },
        "title": { "type": "string" }
      }
    }
  },
  "properties": {
    "events": {
      "type": "object",
      "description": "the event layer output (events.json): every ok essence belongs to exactly one event",
      "required": ["date", "generated_at", "clustering", "events"],
      "properties": {
        "date": { "type": "string" },
        "generated_at": { "type": "string" },
        "model": { "type": "string" },
        "clustering": {
          "type": "object",
          "required": ["candidates", "ai_calls", "fallbacks"],
          "properties": {
            "candidates": { "type": "integer" },
            "ai_calls": { "type": "integer" },
            "fallbacks": { "type": "integer" }
          }
        },
        "events": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["id", "members", "sources", "topic", "subtopic", "facets", "reports", "weight"],
            "properties": {
              "id": { "type": "string", "pattern": "^evt_[a-f0-9]{12}$" },
              "seed": { "type": "string" },
              "members": { "type": "array", "minItems": 1, "items": { "type": "string" } },
              "sources": { "type": "array", "items": { "type": "string" } },
              "topic": { "type": "string" },
              "subtopic": { "type": "string" },
              "facets": {
                "type": "array",
                "items": {
                  "type": "object",
                  "required": ["id", "label", "members"],
                  "properties": {
                    "id": { "type": "string", "pattern": "^fct_[a-f0-9]{12}$" },
                    "label": { "type": "string" },
                    "members": { "type": "array", "items": { "type": "string" } }
                  }
                }
              },
              "locations": {
                "type": "array",
                "items": {
                  "type": "object",
                  "required": ["country_code", "name"],
                  "properties": { "country_code": { "type": "string" }, "name": { "type": "string" } }
                }
              },
              "reports": { "type": "integer", "minimum": 1 },
              "weight": { "type": "number", "description": "corroboration x complexity; feeds the day-portrait ranking" }
            }
          }
        }
      }
    },
    "day": {
      "type": "object",
      "required": ["date", "generated_at", "counts", "sources", "spend"],
      "properties": {
        "date": { "type": "string", "pattern": "^\\d{4}-\\d{2}-\\d{2}$" },
        "generated_at": { "type": "string" },
        "counts": {
          "type": "object",
          "required": ["articles", "essences", "failures"],
          "properties": {
            "articles": { "type": "integer" },
            "essences": { "type": "integer" },
            "failures": { "type": "integer" },
            "events": { "type": ["integer", "null"] }
          }
        },
        "sources": {
          "type": "object",
          "additionalProperties": {
            "type": "object",
            "required": ["status"],
            "properties": {
              "status": { "enum": ["ok", "failed"] },
              "http": { "type": ["integer", "null"] },
              "fetched": { "type": "integer" },
              "new": { "type": "integer" }
            }
          }
        },
        "spend": {
          "type": "object",
          "required": ["tokens_in", "tokens_out", "usd"],
          "properties": {
            "tokens_in": { "type": "integer" },
            "tokens_out": { "type": "integer" },
            "usd": { "type": "number" }
          }
        }
      }
    },
    "topics": {
      "type": "object",
      "required": ["date", "tree"],
      "properties": {
        "date": { "type": "string" },
        "tree": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["topic", "name", "count", "subtopics"],
            "properties": {
              "topic": { "type": "string" },
              "name": { "type": "string" },
              "count": { "type": "integer" },
              "subtopics": {
                "type": "array",
                "items": {
                  "type": "object",
                  "required": ["subtopic", "name", "count", "essences"],
                  "properties": {
                    "subtopic": { "type": "string" },
                    "name": { "type": "string" },
                    "count": { "type": "integer" },
                    "essences": { "type": "array", "items": { "$ref": "#/defs/essence_ref" } }
                  }
                }
              }
            }
          }
        }
      }
    },
    "gis": {
      "type": "object",
      "required": ["date", "countries"],
      "properties": {
        "date": { "type": "string" },
        "resolution": { "const": "country" },
        "countries": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["country_code", "name", "lat", "lon", "count", "essences"],
            "properties": {
              "country_code": { "type": "string", "pattern": "^[A-Z]{2}$" },
              "name": { "type": "string" },
              "lat": { "type": "number" },
              "lon": { "type": "number" },
              "count": { "type": "integer" },
              "essences": { "type": "array", "items": { "$ref": "#/defs/essence_ref" } }
            }
          }
        }
      }
    }
  }
}
