{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://radio2.org/schema/v0.2/event.schema.json",
  "title": "Radio 2.0 live event, version 0.2",
  "description": "One event of a station's live event stream, and the document of its poll address. Select the definition by the event's type; each definition in $defs uses only basic keywords so simple validators can check it on its own.",
  "oneOf": [
    { "$ref": "#/$defs/now_playing" },
    { "$ref": "#/$defs/show_change" },
    { "$ref": "#/$defs/live_start" },
    { "$ref": "#/$defs/live_end" },
    { "$ref": "#/$defs/schedule_change" },
    { "$ref": "#/$defs/value_change" },
    { "$ref": "#/$defs/heartbeat" }
  ],
  "$defs": {
    "eventId": { "type": "string", "pattern": "^[\\x21-\\x7E]{1,128}$" },
    "time": { "type": "string", "format": "date-time" },
    "now_playing": {
      "type": "object",
      "required": ["type", "id", "sentAt", "item", "live", "show"],
      "additionalProperties": false,
      "properties": {
        "type": { "const": "now_playing" },
        "id": { "$ref": "#/$defs/eventId" },
        "sentAt": { "$ref": "#/$defs/time" },
        "item": { "$ref": "#/$defs/item" },
        "live": { "type": "boolean" },
        "onAir": { "type": "boolean", "default": true },
        "show": { "$ref": "#/$defs/showOrNull" },
        "listeners": { "type": "integer", "minimum": 0 },
        "value": { "$ref": "#/$defs/value" }
      }
    },
    "show_change": {
      "type": "object",
      "required": ["type", "id", "sentAt", "show"],
      "additionalProperties": false,
      "properties": {
        "type": { "const": "show_change" },
        "id": { "$ref": "#/$defs/eventId" },
        "sentAt": { "$ref": "#/$defs/time" },
        "show": { "$ref": "#/$defs/showOrNull" }
      }
    },
    "live_start": {
      "type": "object",
      "required": ["type", "id", "sentAt", "show"],
      "additionalProperties": false,
      "properties": {
        "type": { "const": "live_start" },
        "id": { "$ref": "#/$defs/eventId" },
        "sentAt": { "$ref": "#/$defs/time" },
        "show": { "$ref": "#/$defs/showOrNull" }
      }
    },
    "live_end": {
      "type": "object",
      "required": ["type", "id", "sentAt"],
      "additionalProperties": false,
      "properties": {
        "type": { "const": "live_end" },
        "id": { "$ref": "#/$defs/eventId" },
        "sentAt": { "$ref": "#/$defs/time" }
      }
    },
    "schedule_change": {
      "type": "object",
      "required": ["type", "id", "sentAt"],
      "additionalProperties": false,
      "properties": {
        "type": { "const": "schedule_change" },
        "id": { "$ref": "#/$defs/eventId" },
        "sentAt": { "$ref": "#/$defs/time" }
      }
    },
    "value_change": {
      "type": "object",
      "required": ["type", "id", "sentAt", "value"],
      "additionalProperties": false,
      "properties": {
        "type": { "const": "value_change" },
        "id": { "$ref": "#/$defs/eventId" },
        "sentAt": { "$ref": "#/$defs/time" },
        "value": { "$ref": "#/$defs/valueOrNull" }
      }
    },
    "heartbeat": {
      "type": "object",
      "required": ["type", "sentAt"],
      "additionalProperties": false,
      "properties": {
        "type": { "const": "heartbeat" },
        "sentAt": { "$ref": "#/$defs/time" }
      }
    },
    "item": {
      "type": "object",
      "required": ["kind", "title", "startedAt"],
      "additionalProperties": false,
      "properties": {
        "kind": { "type": "string", "enum": ["song", "live", "break", "station", "other"] },
        "title": { "type": "string", "minLength": 1, "maxLength": 300 },
        "artists": {
          "type": "array",
          "maxItems": 20,
          "items": { "type": "string", "minLength": 1, "maxLength": 200 }
        },
        "album": { "type": "string", "maxLength": 300 },
        "year": { "type": "integer", "minimum": 1000, "maximum": 9999 },
        "artwork": { "type": "string", "format": "uri-reference", "maxLength": 2000 },
        "isrc": { "type": "string", "pattern": "^[A-Z]{2}[A-Z0-9]{3}[0-9]{7}$" },
        "url": { "type": "string", "format": "uri-reference", "maxLength": 2000 },
        "startedAt": { "$ref": "#/$defs/time" },
        "durationMs": { "type": ["integer", "null"], "minimum": 0 }
      }
    },
    "showOrNull": {
      "type": ["object", "null"],
      "required": ["title"],
      "additionalProperties": false,
      "properties": {
        "title": { "type": "string", "minLength": 1, "maxLength": 300 },
        "presenters": {
          "type": "array",
          "maxItems": 20,
          "items": { "type": "string", "minLength": 1, "maxLength": 200 }
        },
        "startsAt": { "$ref": "#/$defs/time" },
        "endsAt": { "$ref": "#/$defs/time" },
        "url": { "type": "string", "format": "uri-reference", "maxLength": 2000 }
      }
    },
    "value": {
      "type": "object",
      "required": ["type", "method", "recipients"],
      "additionalProperties": false,
      "properties": {
        "type": { "type": "string", "enum": ["lightning"] },
        "method": { "type": "string", "enum": ["keysend", "lnaddress"] },
        "suggested": { "type": "string", "pattern": "^[0-9]+(\\.[0-9]+)?$" },
        "recipients": {
          "type": "array",
          "minItems": 1,
          "maxItems": 50,
          "items": { "$ref": "#/$defs/recipient" }
        }
      }
    },
    "valueOrNull": {
      "type": ["object", "null"],
      "required": ["type", "method", "recipients"],
      "additionalProperties": false,
      "properties": {
        "type": { "type": "string", "enum": ["lightning"] },
        "method": { "type": "string", "enum": ["keysend", "lnaddress"] },
        "suggested": { "type": "string", "pattern": "^[0-9]+(\\.[0-9]+)?$" },
        "recipients": {
          "type": "array",
          "minItems": 1,
          "maxItems": 50,
          "items": { "$ref": "#/$defs/recipient" }
        }
      }
    },
    "recipient": {
      "type": "object",
      "required": ["type", "address", "split"],
      "additionalProperties": false,
      "properties": {
        "name": { "type": "string", "maxLength": 120 },
        "type": { "type": "string", "enum": ["node", "lnaddress"] },
        "address": { "type": "string", "minLength": 3, "maxLength": 320 },
        "split": { "type": "integer", "minimum": 0, "maximum": 1000000 },
        "fee": { "type": "boolean", "default": false },
        "customKey": { "type": "string", "maxLength": 64 },
        "customValue": { "type": "string", "maxLength": 256 },
        "role": { "type": "string", "enum": ["station", "host", "artist", "producer", "app", "other"] }
      }
    }
  }
}
