{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://turg.fitness.ee/schemas/partner-feed-v1.json",
  "title": "Partner product feed",
  "description": "v1 contract for vendor product feeds ingested by turg.fitness.ee. See partner-feed-spec.md.",
  "type": "object",
  "required": ["schema_version", "generated_at", "vendor_id", "currency", "products"],
  "additionalProperties": false,
  "properties": {
    "schema_version": { "const": "1.0" },
    "generated_at":   { "type": "string", "format": "date-time" },
    "vendor_id":      { "type": "string", "pattern": "^[a-z0-9-]+$" },
    "currency":       { "type": "string", "enum": ["EUR"] },
    "products": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/product" }
    }
  },
  "$defs": {
    "product": {
      "type": "object",
      "required": [
        "id", "sku", "parent_id", "type", "permalink", "updated_at",
        "locales", "price", "regular_price", "sale_price",
        "stock_status", "stock_quantity", "manage_stock",
        "brand", "attributes", "images"
      ],
      "additionalProperties": false,
      "properties": {
        "id":         { "type": "string", "minLength": 1 },
        "sku":        { "type": "string", "minLength": 1 },
        "parent_id":  { "type": ["string", "null"] },
        "type":       { "type": "string", "enum": ["simple", "variation", "variable", "grouped", "bundle"] },
        "permalink":  { "type": "string", "format": "uri" },
        "updated_at": { "type": "string", "format": "date-time" },

        "locales": {
          "type": "object",
          "minProperties": 1,
          "propertyNames": { "enum": ["et", "en", "ru"] },
          "additionalProperties": { "$ref": "#/$defs/locale" }
        },

        "price":         { "$ref": "#/$defs/decimalString" },
        "regular_price": { "$ref": "#/$defs/decimalString" },
        "sale_price":    { "oneOf": [{ "$ref": "#/$defs/decimalString" }, { "type": "null" }] },

        "stock_status":   { "type": "string", "enum": ["instock", "outofstock", "onbackorder"] },
        "stock_quantity": { "type": ["integer", "null"], "minimum": 0 },
        "manage_stock":   { "type": "boolean" },

        "brand": {
          "type": "object",
          "required": ["slug", "name"],
          "additionalProperties": false,
          "properties": {
            "slug": { "type": "string", "pattern": "^[a-z0-9-]+$" },
            "name": { "type": "string", "minLength": 1 }
          }
        },

        "attributes": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["slug", "name", "value"],
            "additionalProperties": false,
            "properties": {
              "slug":  { "type": "string", "minLength": 1 },
              "name":  { "type": "string", "minLength": 1 },
              "value": { "type": "string" }
            }
          }
        },

        "tags": {
          "type": "array",
          "items": { "type": "string", "pattern": "^[a-z0-9-]+$" }
        },

        "images": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "string", "format": "uri" }
        }
      }
    },

    "locale": {
      "type": "object",
      "required": ["name", "slug", "categories"],
      "additionalProperties": false,
      "properties": {
        "name":                   { "type": "string", "minLength": 1 },
        "slug":                   { "type": "string", "minLength": 1 },
        "short_description_html": { "type": "string" },
        "description_html":       { "type": "string" },
        "categories": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "required": ["id", "slug", "name"],
            "additionalProperties": false,
            "properties": {
              "id":   { "type": "string", "minLength": 1 },
              "slug": { "type": "string", "minLength": 1 },
              "name": { "type": "string", "minLength": 1 }
            }
          }
        }
      }
    },

    "decimalString": {
      "type": "string",
      "pattern": "^[0-9]+(\\.[0-9]{1,4})?$"
    }
  }
}
