{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://opengastronomy.org/schema/v0.2/pairing.schema.json",
  "title": "OGS Pairing",
  "description": "A scored, explained pairing between a dish and a beverage.",
  "type": "object",
  "required": ["id", "ogs_version", "name", "dish_ref", "beverage_ref", "match_types", "overall_score", "grade", "explanation_codes", "confidence"],
  "properties": {
    "id": {
      "type": "string",
      "pattern": "^ogs:[a-z0-9-]+:pairing:[a-z0-9-]+$"
    },
    "ogs_version": {
      "type": "string",
      "pattern": "^0\\.(1|2)\\.\\d+$"
    },
    "primary_language": { "$ref": "#/$defs/bcp47_tag" },
    "available_languages": {
      "type": "array",
      "items": { "$ref": "#/$defs/bcp47_tag" }
    },
    "name": { "$ref": "#/$defs/lstring" },
    "description": { "$ref": "#/$defs/lstring" },
    "dish_ref": {
      "type": "string",
      "pattern": "^ogs:[a-z0-9-]+:dish:[a-z0-9-]+$"
    },
    "beverage_ref": {
      "type": "string",
      "pattern": "^ogs:[a-z0-9-]+:beverage:[a-z0-9-]+$"
    },
    "match_types": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/match_type_entry" }
    },
    "overall_score": { "type": "integer", "minimum": 0, "maximum": 100 },
    "grade": {
      "type": "string",
      "enum": ["A+", "A", "B+", "B", "C+", "C", "D", "F"]
    },
    "explanation_codes": {
      "type": "array",
      "items": { "$ref": "#/$defs/explanation_code_entry" }
    },
    "risks": {
      "type": "array",
      "items": { "$ref": "#/$defs/risk_entry" }
    },
    "confidence": { "type": "number", "minimum": 0, "maximum": 1 },
    "evidence": { "$ref": "#/$defs/evidence" },
    "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
        }
      ]
    },
    "match_type_entry": {
      "type": "object",
      "required": ["type", "score"],
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "harmony", "contrast", "bridge",
            "intensity_alignment", "structural_balance", "refresh"
          ]
        },
        "score": { "type": "integer", "minimum": 0, "maximum": 100 },
        "description": { "$ref": "#/$defs/lstring" },
        "primary": { "type": "boolean" }
      },
      "additionalProperties": false
    },
    "explanation_code_entry": {
      "type": "object",
      "required": ["code"],
      "properties": {
        "code": { "type": "string" },
        "weight": { "type": "number", "minimum": 0, "maximum": 1 },
        "detail": { "$ref": "#/$defs/lstring" }
      },
      "additionalProperties": false
    },
    "risk_entry": {
      "type": "object",
      "required": ["code", "severity"],
      "properties": {
        "code": { "type": "string" },
        "severity": {
          "type": "string",
          "enum": ["low", "medium", "high"]
        },
        "description": { "$ref": "#/$defs/lstring" },
        "mitigation": { "$ref": "#/$defs/lstring" }
      },
      "additionalProperties": false
    },
    "evidence": {
      "type": "object",
      "properties": {
        "method": {
          "type": "string",
          "enum": ["expert_panel", "instrumental", "computed", "estimated"]
        },
        "sources": {
          "type": "array",
          "items": { "type": "string" }
        },
        "notes": { "$ref": "#/$defs/lstring" }
      },
      "additionalProperties": true
    },
    "metadata": {
      "type": "object",
      "properties": {
        "created": { "type": "string", "format": "date-time" },
        "updated": { "type": "string", "format": "date-time" },
        "source": { "type": "string" },
        "license": { "type": "string" }
      },
      "additionalProperties": true
    }
  }
}
