{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://akarya.id/schema/layout-v1.json",
  "title": "Akarya 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"]
        }
      }
    },
    "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" }
      }
    },
    "interactive": {
      "description": "Defaults for touch actions on this layout's widgets (ADR-0020).",
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "timeoutS": {
          "description": "Seconds of inactivity before a navigateLayout action's target returns to the schedule-driven layout, when the action itself omits returnAfterS.",
          "type": "integer",
          "minimum": 1,
          "maximum": 3600
        }
      }
    },
    "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. 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": {}
        },
        "actions": {
          "description": "Touch actions on this widget (ADR-0020). At most one per trigger is meaningful; the runtime honours the first match.",
          "type": "array",
          "maxItems": 4,
          "items": { "$ref": "#/$defs/action" }
        }
      },
      "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 (asp-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 a portable CMS text region's, under its field names and enum values (asp-client textWidgetStyle). Players clamp numbers to the portable CMS'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 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 }
                }
              }
            }
          }
        }
      ]
    },
    "action": {
      "description": "A touch action on a widget (ADR-0020). `navigateLayout` jumps to another published layout in the same tenant; `previousLayout` returns to whatever was on screen before the interactive session began (or falls back to the schedule if nothing to return to).",
      "type": "object",
      "required": ["trigger", "type"],
      "additionalProperties": false,
      "properties": {
        "trigger": { "enum": ["tap"], "default": "tap" },
        "type": { "enum": ["navigateLayout", "previousLayout"] },
        "layoutId": { "$ref": "#/$defs/id" },
        "returnAfterS": {
          "description": "Seconds of inactivity before this navigation reverts. Defaults to the target layout's `interactive.timeoutS`, or a runtime default.",
          "type": "integer",
          "minimum": 1,
          "maximum": 3600
        }
      },
      "allOf": [
        {
          "if": { "properties": { "type": { "const": "navigateLayout" } } },
          "then": { "required": ["layoutId"] }
        }
      ]
    }
  }
}
