{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://rctp.dev/schemas/offer.schema.json",
  "title": "Offer",
  "description": "An offer or counter on a unique listing (capability dev.rctp.deal; binding 5). by says which side made it; neither side's identity is carried (INV-9).",
  "type": "object",
  "properties": {
    "id": {
      "$ref": "common.schema.json#/$defs/OfferId"
    },
    "by": {
      "type": "string",
      "enum": [
        "buyer",
        "seller"
      ]
    },
    "listing_id": {
      "$ref": "common.schema.json#/$defs/ListingId"
    },
    "listing_hash": {
      "$ref": "common.schema.json#/$defs/Digest"
    },
    "negotiation_id": {
      "$ref": "common.schema.json#/$defs/NegotiationId"
    },
    "supersedes": {
      "$ref": "common.schema.json#/$defs/OfferId",
      "description": "The offer this one answers or replaces; it becomes superseded"
    },
    "deal_terms": {
      "$ref": "deal-terms.schema.json"
    },
    "status": {
      "$ref": "offer-state.schema.json"
    },
    "expires_at": {
      "$ref": "common.schema.json#/$defs/Timestamp",
      "description": "Capped at the gateway maximum and at the listing's available_until"
    },
    "created_at": {
      "$ref": "common.schema.json#/$defs/Timestamp"
    },
    "updated_at": {
      "$ref": "common.schema.json#/$defs/Timestamp"
    },
    "approval_id": {
      "$ref": "common.schema.json#/$defs/ApprovalId",
      "description": "Set while awaiting_approval"
    },
    "checkout": {
      "$ref": "common.schema.json#/$defs/CompactJws",
      "description": "Business-signed checkout (rctp-checkout+jwt), once accepted"
    },
    "deal_id": {
      "$ref": "common.schema.json#/$defs/DealId"
    },
    "history": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "actor": {
            "type": "string",
            "enum": [
              "buyer",
              "seller",
              "enforcement_point"
            ]
          },
          "event": {
            "type": "string",
            "enum": [
              "created",
              "countered",
              "escalated",
              "accepted",
              "declined",
              "expired",
              "superseded",
              "refused"
            ]
          },
          "offer_id": {
            "$ref": "common.schema.json#/$defs/OfferId",
            "description": "The related offer: the counter for countered, the new offer for superseded"
          },
          "at": {
            "$ref": "common.schema.json#/$defs/Timestamp"
          }
        },
        "additionalProperties": false,
        "required": [
          "actor",
          "event",
          "at"
        ]
      }
    }
  },
  "additionalProperties": false,
  "required": [
    "id",
    "by",
    "listing_id",
    "listing_hash",
    "negotiation_id",
    "deal_terms",
    "status",
    "expires_at",
    "created_at",
    "updated_at"
  ],
  "allOf": [
    {
      "if": {
        "properties": {
          "by": {
            "const": "seller"
          }
        },
        "required": [
          "by"
        ]
      },
      "then": {
        "required": [
          "supersedes"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "status": {
            "const": "accepted"
          }
        },
        "required": [
          "status"
        ]
      },
      "then": {
        "required": [
          "checkout"
        ]
      }
    }
  ]
}
