{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://akarya.id/schema/layout-v1.json",
  "title": "Doka Layout Document",
  "description": "A layout: a fixed-size canvas of stacked regions, each region playing a playlist of widgets. Version 1.",
  "type": "object",
  "required": ["schemaVersion", "id", "dimensions", "regions"],
  "additionalProperties": false,
  "properties": {
    "schemaVersion": { "const": 1 },
    "id": { "$ref": "#/$defs/id" },
    "name": { "type": "string", "maxLength": 255 },
    "tags": {
      "type": "array",
      "items": { "type": "string", "maxLength": 64 },
      "uniqueItems": true
    },
    "dimensions": {
      "type": "object",
      "required": ["width", "height"],
      "additionalProperties": false,
      "properties": {
        "width": { "type": "integer", "minimum": 1, "maximum": 16384 },
        "height": { "type": "integer", "minimum": 1, "maximum": 16384 },
        "orientation": {
          "description": "Advisory; derived from width/height if omitted.",
          "enum": ["landscape", "portrait"]
        }
      }
    },
    "useActualResolution": {
      "type": "boolean",
      "description": "Editor setting. When true, videos are published as uploaded; otherwise the editor compresses them to their layer size and 30 fps. Players ignore it."
    },
    "background": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "color": { "$ref": "#/$defs/color" },
        "mediaSha256": { "$ref": "#/$defs/sha256" },
        "mediaMime": { "type": "string" },
        "fit": { "enum": ["cover", "contain", "fill", "tile", "center"], "default": "cover" }
      }
    },
    "duration": {
      "description": "Seconds this layout plays when it is itself an item in a schedule loop. Omit for schedule-driven layouts that play until superseded.",
      "type": "number",
      "exclusiveMinimum": 0
    },
    "transition": {
      "description": "Default in/out transition for this layout when used as a loop item.",
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "in": { "$ref": "#/$defs/transition" },
        "out": { "$ref": "#/$defs/transition" }
      }
    },
    "regions": {
      "type": "array",
      "minItems": 1,
      "maxItems": 128,
      "items": { "$ref": "#/$defs/region" }
    }
  },

  "$defs": {
    "id": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_-]{1,64}$",
      "description": "Opaque stable identifier (ULID/UUID/slug)."
    },
    "sha256": {
      "type": "string",
      "pattern": "^[a-f0-9]{64}$",
      "description": "Lowercase hex SHA-256 of the asset bytes (ADR-0005)."
    },
    "color": {
      "type": "string",
      "pattern": "^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$",
      "description": "#rgb, #rrggbb, or #rrggbbaa."
    },
    "rect": {
      "type": "object",
      "required": ["x", "y", "width", "height"],
      "additionalProperties": false,
      "properties": {
        "unit": { "enum": ["px", "percent"], "default": "px" },
        "x": { "type": "number", "minimum": 0 },
        "y": { "type": "number", "minimum": 0 },
        "width": { "type": "number", "exclusiveMinimum": 0 },
        "height": { "type": "number", "exclusiveMinimum": 0 }
      },
      "description": "Position/size within the layout canvas. px is against dimensions; percent is 0..100 of the canvas."
    },
    "transition": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "type": { "enum": ["none", "fade", "slide", "wipe"], "default": "none" },
        "durationMs": { "type": "integer", "minimum": 0, "maximum": 10000, "default": 500 },
        "direction": {
          "enum": ["n", "s", "e", "w"],
          "description": "Required meaning only for slide/wipe."
        },
        "ease": { "enum": ["linear", "ease-in", "ease-out", "ease-in-out"], "default": "ease-in-out" }
      }
    },
    "region": {
      "type": "object",
      "required": ["id", "rect", "playlist"],
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "name": { "type": "string", "maxLength": 255 },
        "rect": { "$ref": "#/$defs/rect" },
        "zIndex": { "type": "integer", "default": 0 },
        "loop": {
          "type": "boolean",
          "default": true,
          "description": "When the playlist ends, start over. If false and the playlist is exhausted, the region holds the last frame."
        },
        "clip": { "type": "boolean", "default": true },
        "transition": { "$ref": "#/$defs/transition" },
        "playlist": { "$ref": "#/$defs/playlist" }
      }
    },
    "playlist": {
      "type": "object",
      "required": ["items"],
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "name": { "type": "string", "maxLength": 255 },
        "items": {
          "type": "array",
          "minItems": 0,
          "maxItems": 512,
          "items": { "$ref": "#/$defs/widget" }
        }
      }
    },
    "widget": {
      "type": "object",
      "required": ["id", "module"],
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "module": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9-]{1,38}$",
          "$comment": "Reserved native modules: image, video, audio, text, html, web, clock, pdf, embedded, playlist. Any other value names an installed marketplace module (ADR-0015) — including the first-party weather and rss data modules — validated per-tenant against the module's settingsSchema on publish."
        },
        "enabled": { "type": "boolean", "default": true },
        "duration": {
          "type": "number",
          "exclusiveMinimum": 0,
          "description": "Seconds to display. Required unless useMediaDuration is true for a timed medium."
        },
        "useMediaDuration": {
          "type": "boolean",
          "default": false,
          "description": "For video/audio: play the asset's natural length and ignore duration."
        },
        "validFrom": { "type": "string", "format": "date-time" },
        "validTo": { "type": "string", "format": "date-time" },
        "transitionIn": { "$ref": "#/$defs/transition" },
        "transitionOut": { "$ref": "#/$defs/transition" },
        "media": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["sha256"],
            "additionalProperties": false,
            "properties": {
              "sha256": { "$ref": "#/$defs/sha256" },
              "mime": { "type": "string" },
              "role": {
                "enum": ["primary", "poster", "font", "html-bundle", "attachment"],
                "default": "primary"
              }
            }
          }
        },
        "options": {
          "type": "object",
          "description": "Module-specific settings. Shape is validated per module by the runtime; kept open here to keep the schema stable as modules evolve.",
          "default": {}
        }
      },
      "allOf": [
        {
          "if": { "properties": { "module": { "enum": ["image", "video", "audio", "pdf"] } } },
          "then": { "required": ["media"] }
        },
        {
          "if": {
            "properties": { "module": { "enum": ["image", "text", "html", "web", "clock", "weather", "rss", "pdf", "embedded"] } }
          },
          "then": { "required": ["duration"] }
        },
        {
          "$comment": "A web widget's URL is loaded in an iframe on the player; anything but http(s) (javascript:, data:, file:, …) could run script with the player's origin.",
          "if": { "properties": { "module": { "const": "web" } } },
          "then": {
            "properties": {
              "options": {
                "type": "object",
                "properties": {
                  "url": { "type": "string", "pattern": "^[Hh][Tt][Tt][Pp][Ss]?://[^\\s]+$" }
                }
              }
            }
          }
        },
        {
          "$comment": "A text widget shows `html` (rich text from the editor's toolbar) when set, else `text`. Players render `html` only through the shared allowlist sanitizer (sp-client sanitizeRichText), so it may hold formatting tags and styles but never runs script; `text` is its plain-text form for players that predate it.",
          "if": { "properties": { "module": { "const": "text" } } },
          "then": {
            "properties": {
              "options": {
                "type": "object",
                "properties": {
                  "text": { "type": "string" },
                  "html": { "type": "string", "maxLength": 20000 },
                  "portableCms": {
                    "$comment": "Whole-widget text settings, the same as an Doka CMS (Android) text region's, under its field names and enum values (sp-client textWidgetStyle). Players clamp numbers to the Doka CMS (Android)'s ranges (size 12–120sp, speed 20–400, line spacing 80–200%); a missing key keeps the default look.",
                    "type": "object",
                    "properties": {
                      "running": { "type": "boolean", "default": false },
                      "textSizeSp": { "type": "number" },
                      "textBold": { "type": "boolean" },
                      "textItalic": { "type": "boolean", "default": false },
                      "textUnderline": { "type": "boolean", "default": false },
                      "textShadow": { "type": "boolean", "default": false },
                      "textFont": { "enum": ["SANS", "SERIF", "MONO", "HANDWRITING"], "default": "SANS" },
                      "textAlign": { "enum": ["START", "CENTER", "END"], "default": "CENTER" },
                      "textVerticalAlign": { "enum": ["TOP", "CENTER", "BOTTOM"], "default": "CENTER" },
                      "textLineSpacingPercent": { "type": "number", "default": 130 },
                      "textSpeedDpPerSecond": { "type": "number", "default": 80 },
                      "textReverse": { "type": "boolean", "default": false }
                    }
                  }
                }
              }
            }
          }
        },
        {
          "$comment": "A playlist widget (one layer that plays several items in turn, like a region's own playlist). Items are flat: any module except another playlist. Its length is the sum of its items, so duration is optional.",
          "if": { "properties": { "module": { "const": "playlist" } } },
          "then": {
            "required": ["options"],
            "properties": {
              "options": {
                "type": "object",
                "required": ["items"],
                "properties": {
                  "loop": { "type": "boolean", "default": true },
                  "items": {
                    "type": "array",
                    "minItems": 1,
                    "maxItems": 256,
                    "items": {
                      "allOf": [
                        { "$ref": "#/$defs/widget" },
                        { "type": "object", "not": { "properties": { "module": { "const": "playlist" } }, "required": ["module"] } }
                      ]
                    }
                  }
                }
              }
            }
          }
        },
        {
          "$comment": "How an image or video sits in its region: fit scales it (contain keeps the whole picture, cover fills the region and crops, fill stretches), align places it where it doesn't fill the region (3x3 grid). Absent keys mean contain + center. Players that can't honour them (host-drawn native video) keep contain + center.",
          "if": { "properties": { "module": { "enum": ["image", "video"] } } },
          "then": {
            "properties": {
              "options": {
                "type": "object",
                "properties": {
                  "fit": { "enum": ["contain", "cover", "fill"], "default": "contain" },
                  "align": {
                    "enum": ["top-left", "top", "top-right", "left", "center", "right", "bottom-left", "bottom", "bottom-right"],
                    "default": "center"
                  }
                }
              }
            }
          }
        },
        {
          "$comment": "A video plays with sound on players that can autoplay it (HostCapabilities.audioAutoplay); muted silences it everywhere.",
          "if": { "properties": { "module": { "const": "video" } } },
          "then": {
            "properties": {
              "options": {
                "type": "object",
                "properties": {
                  "muted": { "type": "boolean", "default": false },
                  "compress": {
                    "type": "boolean",
                    "default": false,
                    "description": "Editor setting: at publish, the console re-encodes the video to the size its region takes on screen and points media at the smaller copy. Players ignore it."
                  },
                  "capFps30": {
                    "type": "boolean",
                    "default": false,
                    "description": "Editor setting: at publish, the console re-encodes a video above 30 fps at 30 fps and points media at that copy. Players ignore it."
                  },
                  "sourceSha256": {
                    "$ref": "#/$defs/sha256",
                    "description": "Set with compress or capFps30: the original video that media was re-encoded from, so a later publish starts from it."
                  },
                  "compressedKey": {
                    "type": "string",
                    "description": "Set with compress or capFps30: what the current copy was made from (source, size, fps cap, encoder version); a different key means it is stale."
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}
