{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://docs.stackshift.cloud/agent-intents/v1/record.schema.json",
  "title": "Agent intents for email: agreement record (agent-agreement/1)",
  "type": "object",
  "required": [
    "profile",
    "id",
    "kind",
    "thread",
    "parties",
    "offer",
    "terms",
    "agreed_at",
    "chain",
    "offer_current",
    "seal"
  ],
  "$defs": {
    "uuid": {
      "type": "string",
      "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
    },
    "money": {
      "type": "object",
      "required": [
        "amount",
        "currency"
      ],
      "properties": {
        "amount": {
          "type": "string",
          "pattern": "^(0|[1-9][0-9]{0,17})$"
        },
        "currency": {
          "type": "string",
          "pattern": "^[A-Z]{3}$"
        }
      }
    },
    "authority": {
      "type": "object",
      "required": [
        "decided_by"
      ],
      "properties": {
        "decided_by": {
          "enum": [
            "limits",
            "owner",
            "person"
          ]
        },
        "level": {
          "type": "string",
          "maxLength": 40
        },
        "limits_version": {
          "type": "integer",
          "minimum": 1
        }
      }
    },
    "item": {
      "type": "object",
      "required": [
        "description",
        "quantity"
      ],
      "properties": {
        "description": {
          "type": "string",
          "minLength": 1,
          "maxLength": 300
        },
        "quantity": {
          "type": "string",
          "pattern": "^[0-9]{1,12}(\\.[0-9]{1,6})?$"
        },
        "unit": {
          "type": "string",
          "maxLength": 32
        },
        "unit_price": {
          "$ref": "#/$defs/money"
        }
      }
    },
    "slot": {
      "type": "object",
      "required": [
        "start",
        "end",
        "time_zone"
      ],
      "properties": {
        "start": {
          "type": "string",
          "format": "date-time"
        },
        "end": {
          "type": "string",
          "format": "date-time"
        },
        "time_zone": {
          "type": "string",
          "maxLength": 64
        }
      }
    },
    "date": {
      "type": "string",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$"
    },
    "message": {
      "type": "object",
      "required": [
        "hash"
      ],
      "properties": {
        "jws": {
          "type": "string",
          "maxLength": 65536
        },
        "canonical": {
          "type": "object"
        },
        "hash": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        }
      },
      "oneOf": [
        {
          "required": [
            "jws"
          ]
        },
        {
          "required": [
            "canonical"
          ]
        }
      ]
    }
  },
  "properties": {
    "profile": {
      "const": "agent-agreement/1"
    },
    "id": {
      "$ref": "#/$defs/uuid"
    },
    "kind": {
      "enum": [
        "two_signatures",
        "our_signature",
        "their_reply"
      ]
    },
    "thread": {
      "$ref": "#/$defs/uuid"
    },
    "parties": {
      "type": "array",
      "minItems": 2,
      "maxItems": 2,
      "items": {
        "type": "object",
        "required": [
          "role",
          "address",
          "org_name"
        ],
        "properties": {
          "role": {
            "enum": [
              "offerer",
              "accepter"
            ]
          },
          "party": {
            "type": "string",
            "pattern": "^pty_[a-z0-9]{20}$"
          },
          "address": {
            "type": "string",
            "maxLength": 320
          },
          "org_name": {
            "type": "string",
            "maxLength": 120
          },
          "kid": {
            "type": "string",
            "maxLength": 64
          },
          "authority": {
            "$ref": "#/$defs/authority"
          }
        }
      }
    },
    "offer": {
      "$ref": "#/$defs/message"
    },
    "acceptance": {
      "$ref": "#/$defs/message"
    },
    "reply": {
      "type": "object",
      "required": [
        "hash",
        "authentication_results",
        "received_at"
      ],
      "properties": {
        "hash": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        },
        "authentication_results": {
          "type": "string",
          "maxLength": 4000
        },
        "received_at": {
          "type": "integer"
        }
      }
    },
    "terms": {
      "type": "object",
      "required": [
        "type"
      ],
      "properties": {
        "type": {
          "enum": [
            "quote",
            "propose_times"
          ]
        },
        "items": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/item"
          }
        },
        "total": {
          "$ref": "#/$defs/money"
        },
        "delivery_by": {
          "$ref": "#/$defs/date"
        },
        "slot": {
          "$ref": "#/$defs/slot"
        },
        "purpose": {
          "type": "string",
          "maxLength": 200
        },
        "location": {
          "type": "string",
          "maxLength": 500
        }
      }
    },
    "agreed_at": {
      "type": "integer",
      "minimum": 1
    },
    "chain": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "string",
        "pattern": "^[0-9a-f]{64}$"
      }
    },
    "offer_current": {
      "type": "boolean"
    },
    "seal": {
      "type": "string",
      "maxLength": 65536
    }
  },
  "oneOf": [
    {
      "properties": {
        "kind": {
          "const": "their_reply"
        }
      },
      "required": [
        "reply"
      ]
    },
    {
      "properties": {
        "kind": {
          "enum": [
            "two_signatures",
            "our_signature"
          ]
        }
      },
      "required": [
        "acceptance"
      ]
    }
  ]
}
