{
  "type": "object",
  "properties": {
    "schema_version": {
      "const": "0.2"
    },
    "id": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_-]*$"
    },
    "title": {
      "type": "string",
      "minLength": 1
    },
    "devices": {
      "type": "object",
      "minProperties": 1,
      "propertyNames": {
        "type": "string",
        "pattern": "^[a-z][a-z0-9_-]*$"
      },
      "additionalProperties": {
        "type": "object",
        "properties": {
          "width": {
            "type": "integer",
            "minimum": 1
          },
          "height": {
            "type": "integer",
            "minimum": 1
          },
          "asset_root": {
            "type": "string",
            "pattern": "^devices/[a-z][a-z0-9_-]*$"
          },
          "description": {
            "type": "string",
            "minLength": 1
          },
          "assets": {
            "type": "object",
            "minProperties": 1,
            "propertyNames": {
              "type": "string",
              "pattern": "^[a-z][a-z0-9_-]*$"
            },
            "additionalProperties": {
              "type": "object",
              "properties": {
                "file": {
                  "type": "string",
                  "pattern": "^[A-Za-z0-9][A-Za-z0-9_./-]*\\.(png|jpg|jpeg)$"
                },
                "role": {
                  "enum": [
                    "background",
                    "overlay"
                  ]
                },
                "description": {
                  "type": "string",
                  "minLength": 1
                }
              },
              "required": [
                "file",
                "role",
                "description"
              ],
              "additionalProperties": true
            }
          },
          "rules": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "id": {
                  "type": "string",
                  "pattern": "^[a-z][a-z0-9_-]*$"
                },
                "description": {
                  "type": "string",
                  "minLength": 1
                },
                "when": {
                  "$ref": "#/$defs/condition"
                },
                "layers": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "id": {
                        "type": "string",
                        "pattern": "^[a-z][a-z0-9_-]*$"
                      },
                      "asset": {
                        "type": "string",
                        "pattern": "^[a-z][a-z0-9_-]*$"
                      },
                      "order": {
                        "type": "integer",
                        "minimum": 0
                      },
                      "opacity": {
                        "type": "number",
                        "minimum": 0,
                        "maximum": 1
                      }
                    },
                    "required": [
                      "id",
                      "asset",
                      "order"
                    ],
                    "additionalProperties": true
                  }
                },
                "phase": {
                  "enum": [
                    "base",
                    "life"
                  ],
                  "default": "base"
                }
              },
              "required": [
                "id",
                "description",
                "when",
                "layers"
              ],
              "additionalProperties": true
            }
          },
          "render": {
            "description": "Optional render declaration; validated as an independent mode. Unknown/incomplete modes preserve the layer recipe.",
            "properties": {
              "calendar": {
                "type": "object",
                "required": [
                  "file",
                  "timezone",
                  "event_ids"
                ],
                "properties": {
                  "file": {
                    "type": "string",
                    "minLength": 1
                  },
                  "timezone": {
                    "type": "string",
                    "minLength": 1
                  },
                  "event_ids": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "pattern": "^[a-z][a-z0-9_]*$"
                    },
                    "uniqueItems": true
                  }
                },
                "additionalProperties": true
              },
              "output_format": {
                "type": "string",
                "enum": [
                  "jpg",
                  "png"
                ]
              },
              "activity_clock": {
                "type": "object",
                "required": [
                  "mode",
                  "timezone"
                ],
                "properties": {
                  "mode": {
                    "const": "timezone"
                  },
                  "timezone": {
                    "type": "string"
                  }
                },
                "additionalProperties": true
              }
            }
          }
        },
        "required": [
          "width",
          "height",
          "asset_root",
          "description",
          "assets",
          "rules"
        ],
        "additionalProperties": true
      }
    },
    "environment": {
      "type": "string",
      "minLength": 1
    }
  },
  "required": [
    "schema_version",
    "id",
    "title",
    "devices"
  ],
  "additionalProperties": true,
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:imperial-school:dynamic-wallpaper-theme:0.2",
  "$defs": {
    "condition": {
      "oneOf": [
        {
          "type": "boolean"
        },
        {
          "type": "object",
          "required": [
            "code"
          ],
          "properties": {
            "code": {
              "type": "string",
              "pattern": "^[a-z][a-z0-9_-]*(?:\\.[a-z0-9_-]+)+$"
            }
          },
          "additionalProperties": true
        },
        {
          "type": "object",
          "required": [
            "field",
            "op",
            "value"
          ],
          "properties": {
            "field": {
              "type": "string",
              "pattern": "^[a-z][a-z0-9_-]*\\.[a-z][a-z0-9_-]*$"
            },
            "op": {
              "type": "string",
              "minLength": 1
            },
            "value": {
              "type": "number"
            }
          },
          "additionalProperties": true
        },
        {
          "type": "object",
          "required": [
            "all"
          ],
          "properties": {
            "all": {
              "type": "array",
              "minItems": 1,
              "items": {
                "$ref": "#/$defs/condition"
              }
            }
          },
          "additionalProperties": true
        },
        {
          "type": "object",
          "required": [
            "any"
          ],
          "properties": {
            "any": {
              "type": "array",
              "minItems": 1,
              "items": {
                "$ref": "#/$defs/condition"
              }
            }
          },
          "additionalProperties": true
        },
        {
          "type": "object",
          "minProperties": 1,
          "not": {
            "anyOf": [
              {
                "required": [
                  "code"
                ]
              },
              {
                "required": [
                  "field"
                ]
              },
              {
                "required": [
                  "all"
                ]
              },
              {
                "required": [
                  "any"
                ]
              }
            ]
          }
        }
      ]
    }
  }
}
