{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://rctp.dev/schemas/attestation-entry.schema.json",
  "title": "Deal record entry",
  "description": "One hash-chained entry of a deal record (vct attestation.transaction.1; Draft 0.5 10.1; binding 8.2). Entries for decisions carry action, tier and result, and for agent calls authenticated_by and agent_profile; entries for events carry event. Never records a floor (INV-1) or personal data (INV-9).",
  "type": "object",
  "properties": {
    "seq": {
      "type": "integer",
      "minimum": 1
    },
    "t": {
      "$ref": "common.schema.json#/$defs/Timestamp"
    },
    "author": {
      "$ref": "common.schema.json#/$defs/PartyRef"
    },
    "prev_hash": {
      "$ref": "common.schema.json#/$defs/Digest"
    },
    "deal_id": {
      "$ref": "common.schema.json#/$defs/DealId"
    },
    "negotiation_id": {
      "$ref": "common.schema.json#/$defs/NegotiationId"
    },
    "listing_id": {
      "$ref": "common.schema.json#/$defs/ListingId"
    },
    "action": {
      "$ref": "common.schema.json#/$defs/Action"
    },
    "tier": {
      "$ref": "common.schema.json#/$defs/Tier"
    },
    "result": {
      "type": "string",
      "pattern": "^[a-z_]{1,64}$"
    },
    "authenticated_by": {
      "$ref": "common.schema.json#/$defs/AuthenticationMechanism",
      "description": "For an entry written for an agent call: how the call was authenticated (Draft 0.5 4.1)"
    },
    "agent_profile": {
      "type": "string",
      "format": "uri",
      "maxLength": 2048,
      "description": "For an entry written for an agent call: the caller's UCP-Agent profile URL (an operator profile, never a person)"
    },
    "client_id": {
      "type": "string",
      "pattern": "^[A-Za-z0-9._:-]{1,128}$",
      "description": "For OAuth and API-key calls: the client that authenticated (Draft 0.5 4.1(6)). A signed call has none; its key is in the profile."
    },
    "offered": {
      "$ref": "common.schema.json#/$defs/Money"
    },
    "approval": {
      "type": "object",
      "properties": {
        "id": {
          "$ref": "common.schema.json#/$defs/ApprovalId"
        },
        "level": {
          "$ref": "common.schema.json#/$defs/ApprovalLevel"
        },
        "at": {
          "$ref": "common.schema.json#/$defs/Timestamp"
        },
        "displayed_text": {
          "$ref": "common.schema.json#/$defs/Digest"
        }
      },
      "additionalProperties": false,
      "required": [
        "id",
        "level",
        "at"
      ]
    },
    "terms_commitment": {
      "$ref": "common.schema.json#/$defs/Digest"
    },
    "event": {
      "$ref": "common.schema.json#/$defs/DealEventType"
    },
    "reason": {
      "$ref": "common.schema.json#/$defs/RejectionReason",
      "description": "With event rejected only"
    },
    "observation": {
      "$ref": "common.schema.json#/$defs/Observation"
    },
    "reported_by": {
      "$ref": "common.schema.json#/$defs/PartyRef"
    },
    "source_event_id": {
      "type": "string",
      "pattern": "^[A-Za-z0-9._:-]{1,96}$"
    },
    "evidence": {
      "$ref": "common.schema.json#/$defs/Evidence"
    },
    "location": {
      "$ref": "common.schema.json#/$defs/Location"
    },
    "ucp_order_ref": {
      "type": "string",
      "pattern": "^[A-Za-z0-9._:/-]{1,128}$"
    },
    "ap2_refs": {
      "type": "array",
      "items": {
        "$ref": "common.schema.json#/$defs/Digest"
      },
      "description": "Digests of AP2 mandates and receipts"
    },
    "event_jws": {
      "$ref": "common.schema.json#/$defs/CompactJws",
      "description": "The writer's own signed deal event (rctp-deal-event+jwt)"
    },
    "countersignatures": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "author": {
            "$ref": "common.schema.json#/$defs/PartyRef"
          },
          "sig": {
            "$ref": "common.schema.json#/$defs/CompactJws"
          }
        },
        "additionalProperties": false,
        "required": [
          "author",
          "sig"
        ]
      },
      "description": "Optional signatures of other parties; a refusal to countersign never invalidates the entry"
    },
    "sig": {
      "$ref": "common.schema.json#/$defs/CompactJws",
      "description": "The enforcement point's JWS over this entry without sig"
    }
  },
  "additionalProperties": false,
  "required": [
    "seq",
    "t",
    "author",
    "prev_hash"
  ],
  "allOf": [
    {
      "if": {
        "properties": {
          "observation": {
            "const": "relayed"
          }
        },
        "required": [
          "observation"
        ]
      },
      "then": {
        "required": [
          "reported_by",
          "source_event_id"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "event": {
            "const": "rejected"
          }
        },
        "required": [
          "event"
        ]
      },
      "then": {
        "required": [
          "reason"
        ]
      },
      "else": {
        "not": {
          "required": [
            "reason"
          ]
        }
      }
    },
    {
      "if": {
        "properties": {
          "authenticated_by": {
            "enum": [
              "oauth",
              "api_key"
            ]
          }
        },
        "required": [
          "authenticated_by"
        ]
      },
      "then": {
        "required": [
          "client_id"
        ]
      }
    }
  ]
}
