{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kaladan.dev/contracts/theme/v1/settings-document.schema.json",
  "title": "Settings document",
  "description": "The merchant's choices: what the visual builder writes and the platform injects. A theme never ships one — it ships defaults. This separation is the only reason a theme update does not destroy customization.",
  "type": "object",

  "properties": {
    "version": {
      "type": "string",
      "description": "Opaque, and equal to `settingsVersion` in the context JWT. It is half of the render-context cache key; the other half is the shop id. Keying on less serves one merchant's shop inside another's."
    },
    "themeId": { "type": "string" },
    "themeVersion": { "type": "string" },

    "global": {
      "type": "object",
      "description": "Values for the theme-wide settings declared in theme.yaml. Absent keys fall back to the declared default.",
      "additionalProperties": true
    },

    "style": {
      "type": "string",
      "description": "The handle of the style the merchant chose, from theme.yaml's `styles`. Absent means they never chose, and the first style applies.\n\nIt is recorded rather than flattened into `global` on purpose: a builder that copied every value in would freeze the shop at the style as it was the day it was picked, and a later release that improves that style would reach nobody. Only what the merchant actually changed lands in `global`; the rest still resolves through the style."
    },

    "chrome": {
      "type": "object",
      "description": "The header and footer the merchant composed, when the theme declares a `chrome` surface. Absent when it does not, and the theme renders its own.",
      "properties": {
        "header": { "type": "array", "items": { "$ref": "#/$defs/PlacedSection" } },
        "footer": { "type": "array", "items": { "$ref": "#/$defs/PlacedSection" } }
      },
      "additionalProperties": false
    },

    "template": {
      "type": "object",
      "description": "Only the template being rendered. A theme rendering a product page is not handed the home page's layout.",
      "properties": {
        "type": {
          "enum": ["index", "product", "collection", "category", "page", "blog", "article", "search", "cart", "order", "account", "policy", "not_found"]
        },
        "sections": {
          "type": "array",
          "description": "In the merchant's order, which a theme MUST preserve — reordering sections is the single most-used control in the builder.\n\n**Absent and empty mean different things.** Absent is a merchant who has never arranged this page, and the theme starts it from the template's `default`. `[]` is a merchant who removed everything, and the theme renders nothing. Collapsing the two costs one of them their choice, and it is always the same one: the merchant who deliberately emptied a page and keeps being given it back.",
          "items": { "$ref": "#/$defs/PlacedSection" }
        }
      },
      "required": ["type"],
      "additionalProperties": false
    }
  },

  "required": ["version", "themeId", "themeVersion", "template"],
  "additionalProperties": false,

  "$defs": {
    "PlacedSection": {
      "type": "object",
      "description": "One section the merchant put somewhere — on a template, or in the header or footer. The same shape either way, because a section does not know which surface it landed on.",
      "properties": {
        "id": { "type": "string", "description": "Unique within the template. Stable across edits, so it is safe as a DOM id or a React key." },
        "type": {
          "type": "string",
          "description": "A section id from the theme's own sections/, or the literal \"app\" for a third-party block. A theme MUST skip a type it does not recognise without failing the page."
        },
        "settings": { "type": "object", "additionalProperties": true },
        "blocks": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": { "type": "string" },
              "type": { "type": "string" },
              "settings": { "type": "object", "additionalProperties": true },
              "app": { "type": "string", "description": "The app's handle, on a block the merchant took from an installed app." },
              "html": {
                "type": "string",
                "description": "Pre-rendered by the platform: it called the app, cached the result, enforced the timeout, and sanitized the markup. A theme MUST emit this for a block whose type it does not recognise, and skip the block when it is absent."
              }
            },
            "required": ["id", "type"],
            "additionalProperties": false
          }
        },
        "app": { "type": "string" },
        "handle": { "type": "string" },
        "html": {
          "type": "string",
          "description": "As above, for a whole section supplied by an app rather than a block within one."
        }
      },
      "required": ["id", "type"],
      "additionalProperties": false
    }
  }
}
