{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kaladan.dev/contracts/theme/v1/render-context.schema.json",
  "title": "Render context",
  "description": "What GET {KALADAN_INTERNAL}/theme/v1/context/{contextId} returns: one shop, one route, one settings document. A contextId resolves to exactly one shop and expires with the JWT that fetched it.",
  "type": "object",

  "properties": {
    "shop": {
      "type": "object",
      "description": "Enough to render a page without a Storefront API round trip for the obvious things. Anything richer is queried.",
      "properties": {
        "id": { "type": "string" },
        "handle": { "type": "string" },
        "name": { "type": "string" },
        "domain": { "type": "string", "description": "The primary domain. Build canonical URLs on it and nothing else." },
        "locale": { "type": "string" },
        "direction": { "enum": ["rtl", "ltr"], "description": "Given rather than inferred from the locale, so no theme ships its own list of RTL languages with one entry wrong." },
        "currency": { "const": "IRR" },
        "calendar": { "enum": ["JALALI", "GREGORIAN", "HIJRI"], "description": "A presentation instruction. Every timestamp elsewhere is Gregorian UTC." },
        "moneyPresentation": {
          "type": "object",
          "description": "Mirrors Shop.moneyPresentation in the Storefront API. Divide by `divisor`; do not hard-code ten.",
          "properties": {
            "unit": { "enum": ["RIAL", "TOMAN"] },
            "divisor": { "type": "integer", "minimum": 1 },
            "fractionDigits": { "type": "integer", "minimum": 0 },
            "numeralSystem": { "enum": ["PERSIAN", "LATIN"] }
          },
          "required": ["unit", "divisor", "fractionDigits", "numeralSystem"],
          "additionalProperties": false
        },
        "brandColors": {
          "type": "object",
          "description": "The merchant's four role-named colours. The one thing Kaladan asks a theme to honour: map them onto whatever palette the theme already has.",
          "properties": {
            "primary": { "type": ["string", "null"] },
            "accent": { "type": ["string", "null"] },
            "background": { "type": ["string", "null"] },
            "foreground": { "type": ["string", "null"] }
          },
          "additionalProperties": false
        }
      },
      "required": ["id", "handle", "name", "domain", "locale", "direction", "currency", "calendar", "moneyPresentation"],
      "additionalProperties": false
    },

    "route": {
      "type": "object",
      "description": "What the edge resolved this path to. The URL structure is the platform's, so a theme SHOULD use this rather than re-parsing the path — see README decision 6.",
      "properties": {
        "type": {
          "enum": ["INDEX", "PRODUCT", "COLLECTION", "CATEGORY", "PAGE", "BLOG", "ARTICLE", "SEARCH", "CART", "ORDER", "ACCOUNT", "POLICY", "NOT_FOUND"]
        },
        "path": { "type": "string" },
        "handle": { "type": ["string", "null"], "description": "The resource's handle, for querying the Storefront API. Null for routes that address no resource." },
        "id": { "type": ["string", "null"], "description": "The resource's opaque id. Stable across handle changes; prefer it as a cache key." },
        "query": { "type": ["string", "null"], "description": "The buyer's search terms, on SEARCH. Send them to the Storefront API unaltered — Persian normalization is server-side." },
        "orderToken": { "type": ["string", "null"], "description": "The order's bearer token, on ORDER — what hosted checkout's return leg appended, and what the Storefront API's order(token:) query takes. A credential for one order, not an id: the rendered page MUST NOT be cached, and the token MUST NOT appear in logs or analytics." }
      },
      "required": ["type", "path", "handle", "id"],
      "additionalProperties": false
    },

    "settings": { "$ref": "settings-document.schema.json" },

    "preview": {
      "type": "boolean",
      "description": "True when the visual builder is rendering an unpublished draft. A theme MUST render a preview exactly as it would render production, and MUST NOT emit analytics, fire pixels, or write anything."
    },

    "builderOrigin": {
      "type": ["string", "null"],
      "description": "The origin the visual builder is loaded from, and the only one a theme may postMessage to or accept a message from. Present when `preview` is true; null otherwise.\n\nIt is given rather than assumed because the theme is reverse-proxied and cannot see the builder's origin from inside the iframe, and because it differs between production, staging and a partner's own environment. A theme MUST compare `event.origin` against this exact string and MUST NOT post to `\"*\"` — a preview iframe is embedded by whoever holds the preview link, and `\"*\"` hands the settings document to that page."
    },

    "assetBase": {
      "type": ["string", "null"],
      "description": "Where uploaded assets are served from, when assets.upload is set. Null when the theme serves its own from /_theme/."
    },

    "storefrontApi": {
      "type": "object",
      "description": "Where to query, and with what. The token is short-lived and shop-scoped; it is the public storefront token and carries no more authority than one embedded in a page.",
      "properties": {
        "endpoint": { "type": "string" },
        "token": { "type": "string" },
        "version": { "type": "string", "pattern": "^\\d{4}-\\d{2}$" },
        "expiresAt": { "type": "string", "format": "date-time" }
      },
      "required": ["endpoint", "token", "version", "expiresAt"],
      "additionalProperties": false
    }
  },

  "required": ["shop", "route", "settings", "preview", "storefrontApi"],
  "additionalProperties": false
}
