{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://ocets.5ya.de/schema/plant-registry-v1.1.json",
  "title": "OCETS Plant Registry",
  "type": "object",
  "required": [
    "version", "registry_id", "revision", "organizations", "portfolios",
    "plants", "assets", "devices", "measurement_points", "topologies", "relationships"
  ],
  "additionalProperties": false,
  "properties": {
    "version": { "const": "1.1" },
    "registry_id": { "$ref": "#/$defs/id" },
    "revision": { "type": "string", "format": "date-time" },
    "organizations": { "type": "array", "items": { "$ref": "#/$defs/organization" } },
    "portfolios": { "type": "array", "items": { "$ref": "#/$defs/portfolio" } },
    "plants": { "type": "array", "items": { "$ref": "#/$defs/plant" } },
    "assets": { "type": "array", "items": { "$ref": "#/$defs/asset" } },
    "devices": { "type": "array", "items": { "$ref": "#/$defs/device" } },
    "measurement_points": { "type": "array", "items": { "$ref": "#/$defs/measurement_point" } },
    "topologies": { "type": "array", "items": { "$ref": "#/$defs/topology" } },
    "relationships": { "type": "array", "items": { "$ref": "#/$defs/relationship" } }
  },
  "$defs": {
    "id": { "type": "string", "minLength": 1, "pattern": "^[a-z0-9][a-z0-9._-]*$" },
    "status": { "enum": ["planned", "active", "maintenance", "disabled", "retired"] },
    "role": {
      "enum": ["grid", "load", "pv", "battery", "inverter", "meter", "transformer", "busbar", "collector", "environment"]
    },
    "energy_form": { "enum": ["electricity_ac", "electricity_dc", "thermal", "environmental", "operational"] },
    "phase": { "enum": ["l1", "l2", "l3", "n", "total"] },
    "direction": { "enum": ["import", "export", "charge", "discharge", "generation", "consumption", "bidirectional"] },
    "organization": {
      "type": "object",
      "required": ["id", "name", "status"],
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "name": { "type": "string", "minLength": 1 },
        "status": { "$ref": "#/$defs/status" },
        "data_residency_region": { "type": "string", "minLength": 1 },
        "tags": { "type": "array", "items": { "type": "string", "minLength": 1 }, "uniqueItems": true }
      }
    },
    "portfolio": {
      "type": "object",
      "required": ["id", "organization_id", "name", "status"],
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "organization_id": { "$ref": "#/$defs/id" },
        "name": { "type": "string", "minLength": 1 },
        "status": { "$ref": "#/$defs/status" },
        "tags": { "type": "array", "items": { "type": "string", "minLength": 1 }, "uniqueItems": true }
      }
    },
    "plant": {
      "type": "object",
      "required": ["id", "organization_id", "name", "timezone", "status"],
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "organization_id": { "$ref": "#/$defs/id" },
        "portfolio_ids": { "type": "array", "items": { "$ref": "#/$defs/id" }, "uniqueItems": true },
        "name": { "type": "string", "minLength": 1 },
        "timezone": { "type": "string", "minLength": 1 },
        "status": { "$ref": "#/$defs/status" },
        "country": { "type": "string", "pattern": "^[A-Z]{2}$" },
        "operator": { "type": "string", "minLength": 1 },
        "commissioned_at": { "type": "string", "format": "date-time" },
        "data_residency_region": { "type": "string", "minLength": 1 },
        "tags": { "type": "array", "items": { "type": "string", "minLength": 1 }, "uniqueItems": true }
      }
    },
    "asset": {
      "type": "object",
      "required": ["id", "plant_id", "type", "name", "status"],
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "plant_id": { "$ref": "#/$defs/id" },
        "type": { "type": "string", "minLength": 1 },
        "name": { "type": "string", "minLength": 1 },
        "status": { "$ref": "#/$defs/status" },
        "vendor": { "type": "string" },
        "model": { "type": "string" },
        "serial_number": { "type": "string" },
        "location": { "type": "string" },
        "commissioned_at": { "type": "string", "format": "date-time" }
      }
    },
    "device": {
      "type": "object",
      "required": ["id", "plant_id", "asset_id", "role", "energy_form", "status"],
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "plant_id": { "$ref": "#/$defs/id" },
        "asset_id": { "$ref": "#/$defs/id" },
        "role": { "$ref": "#/$defs/role" },
        "energy_form": { "$ref": "#/$defs/energy_form" },
        "status": { "$ref": "#/$defs/status" },
        "vendor": { "type": "string" },
        "model": { "type": "string" },
        "protocols": { "type": "array", "items": { "type": "string", "minLength": 1 }, "uniqueItems": true },
        "communication": { "type": "object" },
        "capabilities": { "type": "array", "items": { "type": "string", "minLength": 1 }, "uniqueItems": true },
        "profiles": {
          "type": "array",
          "items": { "enum": ["edge_collector_observability"] },
          "uniqueItems": true
        }
      }
    },
    "measurement_point": {
      "type": "object",
      "required": ["id", "plant_id", "device_id", "type", "name"],
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "plant_id": { "$ref": "#/$defs/id" },
        "device_id": { "$ref": "#/$defs/id" },
        "type": { "type": "string", "minLength": 1 },
        "name": { "type": "string", "minLength": 1 },
        "role": { "$ref": "#/$defs/role" },
        "energy_form": { "$ref": "#/$defs/energy_form" },
        "phase": { "$ref": "#/$defs/phase" },
        "direction": { "$ref": "#/$defs/direction" },
        "string": { "type": "string" },
        "mppt": { "type": "string" },
        "nominal_voltage": { "type": "number" },
        "parent_measurement_point_id": { "$ref": "#/$defs/id" }
      }
    },
    "topology": {
      "type": "object",
      "required": ["id", "plant_id", "kind", "version", "name", "status", "valid_from"],
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "plant_id": { "$ref": "#/$defs/id" },
        "kind": { "enum": ["electrical", "physical", "telemetry"] },
        "version": { "type": "integer", "minimum": 1 },
        "name": { "type": "string", "minLength": 1 },
        "status": { "$ref": "#/$defs/status" },
        "valid_from": { "type": "string", "format": "date-time" },
        "valid_to": { "type": "string", "format": "date-time" }
      }
    },
    "endpoint": {
      "type": "object",
      "required": ["kind", "id"],
      "additionalProperties": false,
      "properties": {
        "kind": { "enum": ["plant", "asset", "device", "measurement_point"] },
        "id": { "$ref": "#/$defs/id" }
      }
    },
    "relationship": {
      "type": "object",
      "required": ["id", "plant_id", "topology_id", "topology_version", "type", "from", "to", "status", "valid_from"],
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "plant_id": { "$ref": "#/$defs/id" },
        "topology_id": { "$ref": "#/$defs/id" },
        "topology_version": { "type": "integer", "minimum": 1 },
        "type": { "enum": ["contains", "feeds", "connected_to", "measures", "controls", "powered_by"] },
        "from": { "$ref": "#/$defs/endpoint" },
        "to": { "$ref": "#/$defs/endpoint" },
        "status": { "$ref": "#/$defs/status" },
        "valid_from": { "type": "string", "format": "date-time" },
        "valid_to": { "type": "string", "format": "date-time" }
      }
    }
  }
}
