{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://kyle-pace.github.io/seattle-grocery-deals/schema/week.schema.json",
  "title": "Seattle Grocery Deals week file",
  "description": "One week of flyers and every readable sale item from them. File name is data/weeks/<week>.json, where week is that Friday's Pacific date. scripts/validate.mjs runs extra checks beyond this schema. Highlighting and ranking are computed by the build.",
  "type": "object",
  "additionalProperties": false,
  "required": ["week", "flyers", "items"],
  "properties": {
    "week": { "type": "string", "format": "date", "description": "The Friday this week is named for (Pacific date)." },
    "flyers": {
      "type": "array",
      "description": "One entry per store checked this week.",
      "items": { "$ref": "#/definitions/flyer" }
    },
    "items": {
      "type": "array",
      "description": "Every readable sale item from the flyers. Leave out anything whose price could not be read.",
      "items": { "$ref": "#/definitions/item" }
    }
  },
  "definitions": {
    "date": { "type": "string", "format": "date" },
    "money": { "type": "number", "exclusiveMinimum": 0, "multipleOf": 0.01 },
    "flyer": {
      "type": "object",
      "additionalProperties": false,
      "required": ["store", "status", "valid_from", "valid_through", "links"],
      "properties": {
        "store": { "type": "string", "pattern": "^[a-z0-9]+(-[a-z0-9]+)*$" },
        "status": {
          "enum": ["full", "partial", "stale", "unreadable"],
          "description": "full: read in full. partial: some pages or sections not read (say which in note). stale: printed dates are not this week. unreadable: no prices could be read."
        },
        "valid_from": { "oneOf": [{ "$ref": "#/definitions/date" }, { "type": "null" }] },
        "valid_through": { "oneOf": [{ "$ref": "#/definitions/date" }, { "type": "null" }] },
        "links": {
          "type": "array",
          "description": "Exact link(s) to this week's flyer. One per page or image when the store has several.",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["label", "url"],
            "properties": {
              "label": { "type": "string", "minLength": 1, "maxLength": 40 },
              "url": { "type": "string", "format": "uri", "pattern": "^https://" }
            }
          }
        },
        "note": { "type": "string", "maxLength": 300 }
      }
    },
    "item": {
      "type": "object",
      "additionalProperties": false,
      "required": ["store", "item", "item_key", "category", "price", "quantity", "unit", "unit_price", "regular_price", "percent_off", "member_price", "valid_through", "notes"],
      "properties": {
        "store": { "type": "string" },
        "item": { "type": "string", "minLength": 1, "maxLength": 120, "description": "Item name as printed on the flyer." },
        "item_key": {
          "oneOf": [{ "type": "string", "pattern": "^[a-z0-9]+(-[a-z0-9]+)*$" }, { "type": "null" }],
          "description": "Normalized key from config/items.json, used to compare across stores and weeks. Null when no key fits."
        },
        "category": { "enum": ["produce", "meat", "seafood", "dairy-eggs", "pantry", "frozen", "bakery", "prepared", "snacks-drinks", "other"] },
        "price": { "$ref": "#/definitions/money", "description": "Sale price as printed, for `quantity` units. For \"4 for $5\", price is 5 and quantity is 4." },
        "quantity": { "type": "integer", "minimum": 1 },
        "unit": { "enum": ["lb", "each", "pack"], "description": "lb: price per pound. each: per item. pack: per package, with pack size given." },
        "pack": {
          "type": "object",
          "additionalProperties": false,
          "required": ["size", "unit"],
          "properties": {
            "size": { "type": "number", "exclusiveMinimum": 0 },
            "unit": { "enum": ["lb", "oz", "kg", "g", "l", "ml", "fl-oz", "gal", "ct", "dozen"] }
          }
        },
        "unit_price": {
          "oneOf": [
            { "type": "null" },
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["value", "per"],
              "properties": {
                "value": { "type": "number", "exclusiveMinimum": 0 },
                "per": { "enum": ["lb", "each", "dozen", "l", "gal"] }
              }
            }
          ],
          "description": "Price per standard unit, rounded to cents. Null only when it cannot be computed."
        },
        "regular_price": {
          "oneOf": [{ "$ref": "#/definitions/money" }, { "type": "null" }],
          "description": "Only if the store printed it (or printed an exact savings amount). Same quantity and unit as price."
        },
        "percent_off": {
          "oneOf": [{ "type": "integer", "minimum": 1, "maximum": 99 }, { "type": "null" }],
          "description": "Whole percent. Computed from regular_price, or as printed when only a percent is printed."
        },
        "member_price": { "type": "boolean", "description": "True for member, loyalty-card or app-only prices." },
        "valid_through": {
          "oneOf": [{ "$ref": "#/definitions/date" }, { "type": "null" }],
          "description": "Last day of the sale price. Null to use the flyer's valid_through."
        },
        "notes": { "type": "string", "maxLength": 200, "description": "Limits, while supplies last, member details, location caveats, \"save up to\" text. Empty string if none." },
        "highlight": {
          "type": "object",
          "additionalProperties": false,
          "required": ["reason"],
          "properties": {
            "reason": { "type": "string", "minLength": 10, "maxLength": 140 }
          },
          "description": "Optional hand pick: marks the item as a standout and shows the one-line reason."
        }
      },
      "if": { "properties": { "unit": { "const": "pack" } } },
      "then": { "required": ["pack"] },
      "else": { "not": { "required": ["pack"] } }
    }
  }
}
