{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://torneo.ai/schemas/answer_rule.v3",
  "title": "answer_rule.v3 outcome",
  "description": "Outcome of the exhaustive decision table (PRODUCT-SPEC/SCORING.md section 3) as returned by torneo_platform.decide.answer_rule_v3: the first matching row gives the verdict and its stable reason_code. Only row 12 gives EVIDENCE. Only row 4 (SUPERSEDED) carries a replacement. $defs.facts describes the input facts.",
  "type": "object",
  "required": [
    "verdict",
    "reason_code",
    "row"
  ],
  "additionalProperties": false,
  "properties": {
    "verdict": {
      "enum": [
        "ABSTAIN",
        "EVIDENCE"
      ]
    },
    "reason_code": {
      "type": "string",
      "pattern": "^(LIFECYCLE_INVALID|FRESHNESS_UNPROVEN|WITNESS_UNAVAILABLE|NO_EVIDENCE|FIXTURE_NOT_EVIDENCE|REVOKED|SUSPENDED|SUPERSEDED|VERIFY_BLOCKED|STALE|TOO_OLD_FOR_CONSUMER|OUT_OF_SCOPE:.+|NOT_MEASURED:.+|INDETERMINATE|LOCAL_VALIDITY|EVIDENCE)$"
    },
    "row": {
      "enum": [
        "0",
        "0b",
        "0c",
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "10",
        "11",
        "12"
      ]
    },
    "replacement": {
      "type": "string",
      "minLength": 1
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "row": {
            "const": "0"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "LIFECYCLE_INVALID"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "0b"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "FRESHNESS_UNPROVEN"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "0c"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "WITNESS_UNAVAILABLE"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "1"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "enum": [
              "NO_EVIDENCE",
              "FIXTURE_NOT_EVIDENCE"
            ]
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "2"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "REVOKED"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "3"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "SUSPENDED"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "4"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "SUPERSEDED"
          }
        },
        "required": [
          "replacement"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "5"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "VERIFY_BLOCKED"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "6"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "STALE"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "7"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "TOO_OLD_FOR_CONSUMER"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "8"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "type": "string",
            "pattern": "^OUT_OF_SCOPE:.+$"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "9"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "type": "string",
            "pattern": "^NOT_MEASURED:.+$"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "10"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "INDETERMINATE"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "11"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "LOCAL_VALIDITY"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "12"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "EVIDENCE"
          },
          "reason_code": {
            "const": "EVIDENCE"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "4"
          }
        },
        "required": [
          "row"
        ]
      },
      "else": {
        "not": {
          "required": [
            "replacement"
          ]
        }
      }
    }
  ],
  "$defs": {
    "facts": {
      "type": "object",
      "description": "Input facts of the decision table (decide.answer_rule_v3). A key left out or null is 'not consulted' for head_fresh and witness_ok.",
      "additionalProperties": false,
      "properties": {
        "lifecycle_invalid": {
          "type": [
            "string",
            "null"
          ]
        },
        "head_fresh": {
          "type": [
            "boolean",
            "null"
          ]
        },
        "witness_ok": {
          "type": [
            "boolean",
            "null"
          ]
        },
        "found": {
          "type": "boolean"
        },
        "fixture": {
          "type": "boolean"
        },
        "lifecycle": {
          "enum": [
            "ACTIVE",
            "REVOKED",
            "SUSPENDED",
            "SUPERSEDED"
          ]
        },
        "replacement": {
          "type": [
            "string",
            "null"
          ]
        },
        "verification": {
          "type": [
            "string",
            "null"
          ]
        },
        "observed_at": {
          "type": "string"
        },
        "valid_until": {
          "type": "string"
        },
        "as_of": {
          "type": "string"
        },
        "max_age_days": {
          "type": [
            "integer",
            "null"
          ]
        },
        "status": {
          "type": [
            "string",
            "null"
          ]
        },
        "constraints": {
          "type": "array",
          "items": {
            "type": "array",
            "prefixItems": [
              {
                "type": "string"
              },
              {
                "enum": [
                  "measured",
                  "not_measured",
                  "out_of_scope"
                ]
              }
            ],
            "items": false,
            "minItems": 2
          }
        }
      }
    }
  }
}
