{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://opengastronomy.org/schema/v0.2/ingredient.schema.json",
  "title": "OGS Ingredient",
  "description": "An ingredient entity with sensory profile, category, and provenance metadata.",
  "type": "object",
  "required": ["id", "ogs_version", "name", "category"],
  "properties": {
    "id": {
      "type": "string",
      "pattern": "^ogs:[a-z0-9-]+:ingredient:[a-z0-9-]+$",
      "description": "OGS identifier in the format ogs:<namespace>:ingredient:<id>"
    },
    "ogs_version": {
      "type": "string",
      "pattern": "^0\\.(1|2)\\.\\d+$",
      "description": "OGS specification version (0.1.x or 0.2.x)"
    },
    "primary_language": {
      "$ref": "#/$defs/bcp47_tag",
      "description": "Authoritative / endonym language of this entity's human-readable fields."
    },
    "available_languages": {
      "type": "array",
      "items": { "$ref": "#/$defs/bcp47_tag" },
      "description": "Advisory list of languages covered by this entity's LString fields."
    },
    "name": { "$ref": "#/$defs/lstring" },
    "description": { "$ref": "#/$defs/lstring" },
    "category": {
      "type": "string",
      "enum": [
        "protein", "vegetable", "fruit", "grain", "dairy", "fat",
        "herb", "spice", "fungus", "legume", "nut", "seed",
        "sweetener", "acid", "salt", "other"
      ]
    },
    "subcategory": { "type": "string" },
    "sensory_profile": { "$ref": "#/$defs/sensory_profile" },
    "origin": {
      "type": "object",
      "properties": {
        "region": { "$ref": "#/$defs/lstring" },
        "season": { "$ref": "#/$defs/lstring" },
        "terroir_notes": { "$ref": "#/$defs/lstring" }
      },
      "additionalProperties": true
    },
    "notes": { "$ref": "#/$defs/lstring" },
    "metadata": { "$ref": "#/$defs/metadata" }
  },
  "additionalProperties": true,
  "$defs": {
    "bcp47_tag": {
      "type": "string",
      "pattern": "^[A-Za-z]{2,3}(-[A-Za-z0-9]{2,8})*$"
    },
    "lstring": {
      "oneOf": [
        { "type": "string", "minLength": 1 },
        {
          "type": "object",
          "minProperties": 1,
          "patternProperties": {
            "^[A-Za-z]{2,3}(-[A-Za-z0-9]{2,8})*$": { "type": "string", "minLength": 1 }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": ["labels"],
          "properties": {
            "primary_language": { "$ref": "#/$defs/bcp47_tag" },
            "labels": {
              "type": "array",
              "minItems": 1,
              "items": {
                "type": "object",
                "required": ["value", "language"],
                "properties": {
                  "value": { "type": "string", "minLength": 1 },
                  "language": { "$ref": "#/$defs/bcp47_tag" },
                  "script": { "type": "string", "pattern": "^[A-Z][a-z]{3}$" },
                  "role": {
                    "type": "string",
                    "enum": ["preferred", "alternate", "transliteration", "descriptive", "loan", "historical", "deprecated", "ipa"]
                  },
                  "transliteration_scheme": { "type": "string" },
                  "translation": {
                    "type": "object",
                    "additionalProperties": true
                  }
                },
                "additionalProperties": false
              }
            }
          },
          "additionalProperties": false
        }
      ]
    },
    "sensory_profile": {
      "type": "object",
      "properties": {
        "basic_tastes": { "$ref": "#/$defs/basic_tastes" },
        "structural": { "$ref": "#/$defs/structural" },
        "aromatics": {
          "type": "array",
          "items": { "$ref": "#/$defs/aromatic_entry" }
        },
        "texture": {
          "type": "array",
          "items": { "type": "string" }
        },
        "finish_length": { "type": "number", "minimum": 0, "maximum": 10 },
        "complexity": { "type": "number", "minimum": 0, "maximum": 10 }
      },
      "additionalProperties": true
    },
    "basic_tastes": {
      "type": "object",
      "properties": {
        "sweet": { "$ref": "#/$defs/intensity_value" },
        "sour": { "$ref": "#/$defs/intensity_value" },
        "salty": { "$ref": "#/$defs/intensity_value" },
        "bitter": { "$ref": "#/$defs/intensity_value" },
        "umami": { "$ref": "#/$defs/intensity_value" }
      },
      "additionalProperties": false
    },
    "structural": {
      "type": "object",
      "properties": {
        "body": { "$ref": "#/$defs/intensity_value" },
        "acidity_perception": { "$ref": "#/$defs/intensity_value" },
        "tannin": { "$ref": "#/$defs/intensity_value" },
        "astringency": { "$ref": "#/$defs/intensity_value" },
        "alcohol_heat": { "$ref": "#/$defs/intensity_value" },
        "effervescence": { "$ref": "#/$defs/intensity_value" }
      },
      "additionalProperties": false
    },
    "intensity_value": { "type": "number", "minimum": 0, "maximum": 10 },
    "aromatic_entry": {
      "type": "object",
      "required": ["family", "group", "item", "intensity"],
      "properties": {
        "family": {
          "type": "string",
          "enum": [
            "fruit", "floral", "herbal", "spice", "earth", "wood",
            "dairy", "savory", "confection", "vegetal", "marine"
          ]
        },
        "group": { "type": "string" },
        "item": { "type": "string" },
        "qualifier": {
          "type": "string",
          "enum": ["fresh", "dried", "cooked", "candied", "fermented", "zest", "overripe"]
        },
        "intensity": { "$ref": "#/$defs/intensity_value" }
      },
      "additionalProperties": false
    },
    "metadata": {
      "type": "object",
      "properties": {
        "created": { "type": "string", "format": "date-time" },
        "updated": { "type": "string", "format": "date-time" },
        "source": { "type": "string" },
        "license": { "type": "string" }
      },
      "additionalProperties": true
    }
  }
}
