{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://opengastronomy.org/schema/v0.5/pairing.schema.json",
  "title": "OGS Pairing",
  "description": "A scored, explained match between one dish and one beverage. See spec/ogs-match.md.",
  "type": "object",
  "required": [
    "id",
    "ogs_version",
    "name",
    "beverage_ref",
    "match_types",
    "overall_score",
    "grade",
    "explanation_codes",
    "confidence"
  ],
  "properties": {
    "id": {
      "type": "string",
      "pattern": "^ogs:[a-z0-9-]+:pairing:[a-z0-9-]+$",
      "description": "OGS identifier: ogs:<namespace>:pairing:<id>."
    },
    "ogs_version": {
      "$ref": "common.schema.json#/$defs/ogs_version"
    },
    "primary_language": {
      "$ref": "common.schema.json#/$defs/primary_language"
    },
    "available_languages": {
      "$ref": "common.schema.json#/$defs/available_languages"
    },
    "name": {
      "$ref": "common.schema.json#/$defs/lstring",
      "description": "Human-readable pairing name."
    },
    "description": {
      "$ref": "common.schema.json#/$defs/lstring",
      "description": "Longer description of the pairing."
    },
    "dish_ref": {
      "type": "string",
      "pattern": "^ogs:[a-z0-9-]+:dish:[a-z0-9-]+$",
      "description": "Reference to the OGS dish being paired. A pairing has either dish_ref or ingredient_ref."
    },
    "ingredient_ref": {
      "type": "string",
      "pattern": "^ogs:[a-z0-9-]+:ingredient:[a-z0-9-]+$",
      "description": "Reference to an OGS ingredient paired on its own, such as a cheese (new in v0.5)."
    },
    "beverage_ref": {
      "type": "string",
      "pattern": "^ogs:[a-z0-9-]+:beverage:[a-z0-9-]+$",
      "description": "Reference to an OGS beverage."
    },
    "focus": {
      "type": "object",
      "description": "The part of the dish the pairing is judged against, for example the sauce (new in v0.5). See spec/ogs-match.md §2.1.",
      "properties": {
        "component_role": {
          "type": "string",
          "enum": [
            "main_protein",
            "main_vegetable",
            "starch",
            "sauce",
            "fat_component",
            "acid_component",
            "aromatic_garnish",
            "seasoning",
            "textural_element"
          ]
        },
        "ingredient_ref": {
          "type": "string",
          "pattern": "^ogs:[a-z0-9-]+:ingredient:[a-z0-9-]+$"
        },
        "preparation_ref": {
          "type": "string",
          "pattern": "^ogs:[a-z0-9-]+:dish:[a-z0-9-]+$"
        },
        "note": {
          "$ref": "common.schema.json#/$defs/lstring"
        }
      },
      "minProperties": 1,
      "patternProperties": {
        "^x-": true
      },
      "additionalProperties": false
    },
    "match_types": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/match_type_entry"
      },
      "description": "Sub-scores per match dimension. See spec/ogs-match.md §3."
    },
    "overall_score": {
      "type": "integer",
      "minimum": 0,
      "maximum": 100,
      "description": "Composite pairing score, 0-100. See spec/ogs-match.md §4."
    },
    "grade": {
      "type": "string",
      "enum": [
        "A+",
        "A",
        "B+",
        "B",
        "C+",
        "C",
        "D",
        "F"
      ],
      "description": "Letter grade. MUST match overall_score per spec/ogs-match.md §5."
    },
    "explanation_codes": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/explanation_code_entry"
      },
      "description": "Why the pairing works or fails. See spec/ogs-match.md §6."
    },
    "risks": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/risk_entry"
      },
      "description": "Potential negative interactions. See spec/ogs-match.md §7."
    },
    "confidence": {
      "$ref": "common.schema.json#/$defs/confidence",
      "description": "Overall confidence in the pairing score."
    },
    "scoring": {
      "$ref": "#/$defs/scoring"
    },
    "evidence": {
      "$ref": "#/$defs/evidence"
    },
    "notes": {
      "$ref": "common.schema.json#/$defs/lstring"
    },
    "metadata": {
      "$ref": "common.schema.json#/$defs/metadata"
    }
  },
  "additionalProperties": true,
  "allOf": [
    {
      "if": {
        "properties": {
          "grade": {
            "const": "A+"
          }
        },
        "required": [
          "grade"
        ]
      },
      "then": {
        "properties": {
          "overall_score": {
            "minimum": 95
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "grade": {
            "const": "A"
          }
        },
        "required": [
          "grade"
        ]
      },
      "then": {
        "properties": {
          "overall_score": {
            "minimum": 90,
            "maximum": 94
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "grade": {
            "const": "B+"
          }
        },
        "required": [
          "grade"
        ]
      },
      "then": {
        "properties": {
          "overall_score": {
            "minimum": 85,
            "maximum": 89
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "grade": {
            "const": "B"
          }
        },
        "required": [
          "grade"
        ]
      },
      "then": {
        "properties": {
          "overall_score": {
            "minimum": 80,
            "maximum": 84
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "grade": {
            "const": "C+"
          }
        },
        "required": [
          "grade"
        ]
      },
      "then": {
        "properties": {
          "overall_score": {
            "minimum": 75,
            "maximum": 79
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "grade": {
            "const": "C"
          }
        },
        "required": [
          "grade"
        ]
      },
      "then": {
        "properties": {
          "overall_score": {
            "minimum": 70,
            "maximum": 74
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "grade": {
            "const": "D"
          }
        },
        "required": [
          "grade"
        ]
      },
      "then": {
        "properties": {
          "overall_score": {
            "minimum": 60,
            "maximum": 69
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "grade": {
            "const": "F"
          }
        },
        "required": [
          "grade"
        ]
      },
      "then": {
        "properties": {
          "overall_score": {
            "maximum": 59
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "risks": {
            "contains": {
              "properties": {
                "severity": {
                  "const": "exclude"
                }
              },
              "required": [
                "severity"
              ]
            }
          }
        },
        "required": [
          "risks"
        ]
      },
      "then": {
        "properties": {
          "overall_score": {
            "maximum": 59
          }
        }
      }
    },
    {
      "oneOf": [
        {
          "required": [
            "dish_ref"
          ],
          "not": {
            "required": [
              "ingredient_ref"
            ]
          }
        },
        {
          "required": [
            "ingredient_ref"
          ],
          "not": {
            "required": [
              "dish_ref"
            ]
          }
        }
      ]
    }
  ],
  "$defs": {
    "match_type_entry": {
      "type": "object",
      "required": [
        "type",
        "score"
      ],
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "harmony",
            "contrast",
            "bridge",
            "intensity_alignment",
            "structural_balance",
            "refresh"
          ],
          "description": "Match type (vocab/match_types.json)."
        },
        "score": {
          "type": "integer",
          "minimum": 0,
          "maximum": 100,
          "description": "Sub-score for this dimension."
        },
        "description": {
          "$ref": "common.schema.json#/$defs/lstring"
        },
        "primary": {
          "type": "boolean",
          "description": "Whether this is the dominant match type."
        }
      },
      "additionalProperties": false,
      "patternProperties": {
        "^x-": true
      }
    },
    "explanation_code_entry": {
      "type": "object",
      "required": [
        "code"
      ],
      "properties": {
        "code": {
          "$ref": "common.schema.json#/$defs/explanation_code_id"
        },
        "weight": {
          "type": "number",
          "minimum": 0,
          "maximum": 1,
          "description": "Relevance weight, 0.0-1.0."
        },
        "detail": {
          "$ref": "common.schema.json#/$defs/lstring",
          "description": "Elaboration specific to this pairing."
        }
      },
      "additionalProperties": false,
      "patternProperties": {
        "^x-": true
      }
    },
    "risk_entry": {
      "type": "object",
      "required": [
        "code",
        "severity"
      ],
      "properties": {
        "code": {
          "$ref": "common.schema.json#/$defs/explanation_code_id",
          "description": "A negative explanation code. Direction is checked at conformance level L2."
        },
        "severity": {
          "type": "string",
          "enum": [
            "low",
            "medium",
            "high",
            "exclude"
          ],
          "description": "Risk severity. 'exclude' (new in v0.4) means the pairing should not be recommended; the overall score must then be below 60."
        },
        "description": {
          "$ref": "common.schema.json#/$defs/lstring"
        },
        "mitigation": {
          "$ref": "common.schema.json#/$defs/lstring"
        }
      },
      "additionalProperties": false,
      "patternProperties": {
        "^x-": true
      }
    },
    "evidence": {
      "type": "object",
      "description": "How the pairing was assessed. See spec/ogs-match.md §8.",
      "properties": {
        "method": {
          "$ref": "common.schema.json#/$defs/provenance_method"
        },
        "sources": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "notes": {
          "$ref": "common.schema.json#/$defs/lstring"
        }
      },
      "additionalProperties": true
    },
    "scoring": {
      "type": "object",
      "description": "How the scores were produced (new in v0.4; required at L3 for 0.4 documents). See spec/ogs-match.md §4.",
      "required": [
        "method",
        "aggregation"
      ],
      "properties": {
        "method": {
          "type": "string",
          "minLength": 1,
          "description": "Name of the scoring method, e.g. a producer's engine or panel protocol."
        },
        "version": {
          "type": "string",
          "description": "Version of the scoring method."
        },
        "aggregation": {
          "type": "string",
          "enum": [
            "weighted_mean",
            "weakest_dimension",
            "holistic",
            "other"
          ],
          "description": "How overall_score relates to the match-type sub-scores."
        },
        "notes": {
          "$ref": "common.schema.json#/$defs/lstring"
        }
      },
      "patternProperties": {
        "^x-": true
      },
      "additionalProperties": false
    }
  }
}
