{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kaladan.dev/contracts/theme/v1/theme.schema.json",
  "title": "theme.yaml",
  "description": "A theme's declaration of itself: what it is, how to run it, what it may reach, and what the visual builder may edit. Validated at publish; a theme that fails this never reaches a merchant.",
  "type": "object",

  "properties": {
    "contract": {
      "const": "theme/v1",
      "description": "Which version of the theme contract this is written against. A breaking change to the contract becomes theme/v2, and both run side by side for a published window."
    },
    "id": {
      "$ref": "common.schema.json#/$defs/Handle",
      "description": "Stable and permanent. It keys the merchant's settings document, so changing it orphans every customization on every shop using the theme."
    },
    "version": {
      "type": "string",
      "description": "Semver of this build. A shop pins an exact version and upgrades explicitly; Kaladan never upgrades a merchant's theme for them.",
      "pattern": "^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(-[0-9A-Za-z.-]+)?$"
    },
    "publisher": { "$ref": "common.schema.json#/$defs/Handle" },
    "name": { "$ref": "common.schema.json#/$defs/LocalizedText" },
    "description": { "$ref": "common.schema.json#/$defs/LocalizedText" },

    "surface": {
      "enum": ["storefront", "hub"],
      "default": "storefront",
      "description": "Where the theme is drawn. A storefront theme is a server the platform runs for a shop's store, and pins a Storefront API version. A hub theme is drawn by kaladan.shop itself, on a shop's page there: it pins a Hub API version and has no runtime, network or chrome, because it never runs as a server and the page's frame is the platform's. A shop has one published theme per surface, and the hub's is the platform's own."
    },

    "hubApiVersion": {
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}$",
      "description": "A hub theme's dated Hub API version: the one kaladan.shop reads the page with. Only a hub theme has one."
    },

    "storefrontApiVersion": {
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}$",
      "description": "The dated Storefront API version this build queries, pinned for the life of the version. When it retires, the theme stops publishing until rebuilt; shops already on it keep running until the retirement date."
    },

    "capabilities": {
      "type": "array",
      "description": "What the theme supports, so a merchant sees the truth in the picker rather than discovering it after installing. Claiming one and not delivering it is a review failure.",
      "items": {
        "enum": ["RTL", "LTR", "MULTI_LOCALE", "APP_BLOCKS", "PRODUCT_REVIEWS", "PREDICTIVE_SEARCH", "FACETED_FILTERS", "PWA", "PICKUP_IN_STORE", "OFFICIAL_INVOICE"]
      },
      "uniqueItems": true
    },

    "runtime": {
      "type": "object",
      "description": "How the container is run. Every value is capped by the merchant's plan; a theme asking for more than the plan allows is clamped, not rejected.",
      "properties": {
        "drainSeconds": {
          "type": "integer", "minimum": 1, "maximum": 60, "default": 20,
          "description": "How long after SIGTERM the process has to finish in-flight requests before SIGKILL."
        },
        "timeoutSeconds": {
          "type": "integer", "minimum": 1, "maximum": 30, "default": 10,
          "description": "How long the edge waits for a response before cutting it and serving its own error page."
        },
        "concurrency": {
          "type": "integer", "minimum": 1, "maximum": 500, "default": 40,
          "description": "In-flight requests per replica. Scaling is measured against this, across every shop using the theme."
        },
        "minReplicas": {
          "type": "integer", "minimum": 0, "maximum": 10, "default": 0,
          "description": "0 permits scale-to-zero — right for a theme on a few low-traffic shops, wrong for one carrying a crawl-sensitive catalogue, because a cold start lands on the Torob crawler."
        },
        "resources": {
          "type": "object",
          "properties": {
            "memory": { "type": "string", "pattern": "^[0-9]+(Mi|Gi)$", "default": "512Mi" },
            "cpu": { "type": "string", "pattern": "^[0-9]+m?$", "default": "500m" }
          },
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    },

    "network": {
      "type": "object",
      "description": "Egress is default-deny. Kaladan's own endpoints and the shop's Storefront API are always reachable; everything else must be declared here and survive review.",
      "properties": {
        "egress": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "host": { "type": "string", "format": "hostname" },
              "reason": {
                "type": "string", "minLength": 10,
                "description": "Why the theme needs it, in a sentence a reviewer can judge. This is the control; the allowlist is only as good as what is written here."
              }
            },
            "required": ["host", "reason"],
            "additionalProperties": false
          }
        }
      },
      "additionalProperties": false
    },

    "assets": {
      "type": "object",
      "description": "Static files. Uploading them is preferred for anything large: a CDN hit never wakes a scaled-to-zero container.",
      "properties": {
        "upload": { "type": "boolean", "default": false },
        "path": { "type": "string", "default": "assets" }
      },
      "additionalProperties": false
    },

    "templates": {
      "type": "object",
      "description": "Which routes the theme renders, and which sections the builder offers on each. Keys are route types in lower case.",
      "propertyNames": {
        "enum": ["index", "product", "collection", "category", "page", "blog", "article", "search", "cart", "order", "account", "policy", "not_found"]
      },
      "additionalProperties": {
        "type": "object",
        "properties": {
          "name": { "$ref": "common.schema.json#/$defs/LocalizedText" },
          "sections": {
            "type": "array",
            "items": { "$ref": "common.schema.json#/$defs/Handle" },
            "description": "Section ids, each declared by a file in sections/."
          },
          "required": {
            "type": "array",
            "items": { "$ref": "common.schema.json#/$defs/Handle" },
            "description": "Sections the merchant cannot remove. A product template without its product section is a broken shop, and the builder should not let a merchant make one by accident."
          },
          "default": {
            "type": "array",
            "items": { "$ref": "common.schema.json#/$defs/SectionRef" },
            "description": "What this page starts as, in order, before anybody opens the builder. `sections` is what the builder offers and `required` is what it refuses to remove; neither says what is on the page on day one, and a merchant who has never opened the builder still has to get a working shop. Omit it and the page starts empty, which is a choice a theme may make for `cart` and should not make for `index`."
          }
        },
        "required": ["name"],
        "additionalProperties": false
      },
      "minProperties": 1
    },

    "chrome": {
      "type": "object",
      "description": "The header and the footer: the two surfaces that are on every page and belong to no route. Without this a theme's chrome is whatever its author hard-coded, and the one thing every merchant asks to change first — the logo, the menu, the announcement bar — is the one thing they cannot. Optional, and a theme that omits it keeps rendering its own.",
      "properties": {
        "header": { "$ref": "#/$defs/ChromeGroup" },
        "footer": { "$ref": "#/$defs/ChromeGroup" }
      },
      "additionalProperties": false
    },

    "styles": {
      "type": "array",
      "minItems": 1,
      "description": "Named starting looks — the same theme wearing different clothes. The first is what a shop gets before anybody chooses.\n\nThis exists because a theme is a container and a container costs something to run, so variety has to come from inside a theme rather than from more of them. It is also the narrowest surface a generator can safely write to: a style is settings and section order, both validated against this manifest, and a bad one is a merchant clicking undo rather than a container to review.",
      "items": {
        "type": "object",
        "properties": {
          "handle": { "$ref": "common.schema.json#/$defs/Handle" },
          "name": { "$ref": "common.schema.json#/$defs/LocalizedText" },
          "description": { "$ref": "common.schema.json#/$defs/LocalizedText" },
          "preview": {
            "type": "string",
            "description": "A path under the theme's assets, for the picker. The store's own screenshots are rendered from the running image rather than taken from here — a screenshot a publisher uploads is one that can disagree with what the theme draws."
          },
          "settings": {
            "type": "object",
            "description": "Values for theme-wide settings, keyed by setting id. Consulted after the merchant's document and before each setting's declared `default`.",
            "additionalProperties": true
          },
          "templates": {
            "type": "object",
            "description": "Starting layouts this style overrides, keyed by template. A template absent here falls back to that template's own `default`.",
            "propertyNames": {
              "enum": ["index", "product", "collection", "category", "page", "blog", "article", "search", "cart", "order", "account", "policy", "not_found"]
            },
            "additionalProperties": {
              "type": "array",
              "items": { "$ref": "common.schema.json#/$defs/SectionRef" }
            }
          },
          "chrome": {
            "type": "object",
            "description": "Starting header and footer this style overrides, on the same terms as `templates`.",
            "properties": {
              "header": { "type": "array", "items": { "$ref": "common.schema.json#/$defs/SectionRef" } },
              "footer": { "type": "array", "items": { "$ref": "common.schema.json#/$defs/SectionRef" } }
            },
            "additionalProperties": false
          }
        },
        "required": ["handle", "name"],
        "additionalProperties": false
      }
    },

    "settings": {
      "type": "array",
      "description": "Theme-wide settings, grouped. Same setting types as a section's.",
      "items": { "$ref": "common.schema.json#/$defs/SettingGroup" }
    },

    "migrations": {
      "type": "array",
      "description": "How a merchant's settings document is carried across a major version. Applied once, on upgrade across the boundary, with the pre-migration document kept so a rollback restores it.",
      "items": {
        "type": "object",
        "properties": {
          "fromVersion": { "type": "string", "description": "The major version being migrated from." },
          "rename": {
            "type": "object",
            "additionalProperties": { "type": "string" },
            "description": "Old dotted path to new: {\"hero.heading\": \"hero.title\"}."
          },
          "drop": { "type": "array", "items": { "type": "string" } },
          "setDefault": { "type": "object" }
        },
        "required": ["fromVersion"],
        "additionalProperties": false
      }
    }
  },

  "required": ["contract", "id", "version", "publisher", "name", "templates"],
  "additionalProperties": false,
  "allOf": [
    {
      "description": "A hub theme pins the Hub API and offers only the page's own template; it has no server to run, nothing to reach and no chrome. Every other theme is a storefront theme and pins the Storefront API.",
      "if": { "properties": { "surface": { "const": "hub" } }, "required": ["surface"] },
      "then": {
        "required": ["hubApiVersion"],
        "properties": {
          "storefrontApiVersion": false,
          "runtime": false,
          "network": false,
          "chrome": false,
          "templates": { "type": "object", "propertyNames": { "const": "index" } }
        }
      },
      "else": {
        "required": ["storefrontApiVersion"],
        "properties": { "hubApiVersion": false }
      }
    }
  ],

  "$defs": {
    "ChromeGroup": {
      "type": "object",
      "description": "One chrome surface. The same three keys a template has, and deliberately so: the builder edits a header with the code it already has for a page.",
      "properties": {
        "sections": {
          "type": "array",
          "items": { "$ref": "common.schema.json#/$defs/Handle" },
          "description": "Section ids the builder offers here. A section may appear both in chrome and on a template; nothing stops a newsletter block living in the footer and on the home page."
        },
        "required": {
          "type": "array",
          "items": { "$ref": "common.schema.json#/$defs/Handle" },
          "description": "What the merchant cannot remove. A header with no navigation is a shop with no navigation."
        },
        "default": {
          "type": "array",
          "items": { "$ref": "common.schema.json#/$defs/SectionRef" },
          "description": "What this surface starts as. Omit it and the theme renders its own chrome, which is the pre-existing behaviour and stays valid."
        }
      },
      "additionalProperties": false
    }
  }
}
