{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://woahwhattheheck.github.io/commons/revenue/outcome_commerce/outcome-contract.schema.json",
  "title": "Reusable Outcome Contract",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "kind",
    "contract_id",
    "listing_id",
    "accepted_at",
    "given",
    "when",
    "then",
    "evidence",
    "decision_window",
    "reopen",
    "refund",
    "source"
  ],
  "properties": {
    "schema_version": {"const": "outcome-contract/v1"},
    "kind": {"const": "OUTCOME_CONTRACT"},
    "contract_id": {"$ref": "#/$defs/id"},
    "listing_id": {"$ref": "#/$defs/id"},
    "accepted_at": {"$ref": "#/$defs/timestamp"},
    "given": {"type": "string", "minLength": 1},
    "when": {"type": "string", "minLength": 1},
    "then": {"type": "string", "minLength": 1},
    "evidence": {
      "type": "object",
      "additionalProperties": false,
      "required": ["required", "schema", "result_address"],
      "properties": {
        "required": {"const": true},
        "schema": {"type": "string", "minLength": 1},
        "result_address": {"type": "string", "minLength": 1}
      }
    },
    "decision_window": {
      "type": "object",
      "additionalProperties": false,
      "required": ["start", "end", "timezone"],
      "properties": {
        "start": {"$ref": "#/$defs/timestamp"},
        "end": {"$ref": "#/$defs/timestamp"},
        "timezone": {"type": "string", "minLength": 1}
      }
    },
    "reopen": {
      "type": "object",
      "additionalProperties": false,
      "required": ["window_seconds", "effect"],
      "properties": {
        "window_seconds": {"type": "integer", "minimum": 0},
        "effect": {"enum": ["NONE", "REVERSE_OUTCOME", "REPAIR_WINDOW"]}
      }
    },
    "refund": {
      "type": "object",
      "additionalProperties": false,
      "required": ["on_miss", "source"],
      "properties": {
        "on_miss": {"enum": ["NONE", "FULL", "PARTIAL", "REPAIR_OPTION"]},
        "source": {"type": "string", "minLength": 1}
      }
    },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "sha256"],
      "properties": {
        "path": {"type": "string", "minLength": 1},
        "sha256": {"type": "string", "pattern": "^[0-9a-f]{64}$"}
      }
    }
  },
  "$defs": {
    "id": {"type": "string", "minLength": 8, "maxLength": 80, "pattern": "^[A-Za-z0-9._-]+$"},
    "timestamp": {"type": "string", "format": "date-time", "pattern": "(?:Z|[+-][0-9]{2}:[0-9]{2})$"}
  }
}
