{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://docs.stackshift.cloud/agent-intents/v1/message.schema.json",
  "title": "Agent intents for email: message (agent-intents/1)",
  "description": "Receivers drop unknown members, cap the part at 32 KB and refuse anything that does not validate.",
  "type": "object",
  "required": [
    "@context",
    "profile",
    "id",
    "type",
    "thread",
    "from",
    "to",
    "nonce",
    "iat",
    "exp",
    "acting_for",
    "agent"
  ],
  "$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}$"
        }
      }
    },
    "address": {
      "type": "string",
      "maxLength": 320,
      "format": "email"
    },
    "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}$"
    }
  },
  "properties": {
    "@context": {
      "const": [
        "https://schema.org",
        "https://docs.stackshift.cloud/agent-intents/v1"
      ]
    },
    "profile": {
      "const": "agent-intents/1"
    },
    "id": {
      "$ref": "#/$defs/uuid"
    },
    "type": {
      "enum": [
        "ask_quote",
        "quote",
        "propose_times",
        "accept",
        "decline",
        "cancel"
      ]
    },
    "thread": {
      "$ref": "#/$defs/uuid"
    },
    "in_reply_to": {
      "$ref": "#/$defs/uuid"
    },
    "from": {
      "type": "object",
      "required": [
        "address"
      ],
      "properties": {
        "party": {
          "type": "string",
          "pattern": "^pty_[a-z0-9]{20}$"
        },
        "address": {
          "$ref": "#/$defs/address"
        },
        "name": {
          "type": "string",
          "maxLength": 120
        },
        "card": {
          "type": "string",
          "pattern": "^https://",
          "maxLength": 500
        }
      }
    },
    "to": {
      "type": "object",
      "required": [
        "address"
      ],
      "properties": {
        "address": {
          "$ref": "#/$defs/address"
        }
      }
    },
    "nonce": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_-]{22}$"
    },
    "iat": {
      "type": "integer",
      "minimum": 1
    },
    "exp": {
      "type": "integer",
      "minimum": 1
    },
    "prev": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "acting_for": {
      "type": "object",
      "required": [
        "org_name",
        "contact"
      ],
      "properties": {
        "org_name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 120
        },
        "contact": {
          "type": "string",
          "minLength": 1,
          "maxLength": 320
        }
      }
    },
    "agent": {
      "type": "object",
      "required": [
        "name",
        "kind"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 120
        },
        "kind": {
          "enum": [
            "stackie",
            "connected",
            "api",
            "person"
          ]
        }
      }
    },
    "items": {
      "type": "array",
      "maxItems": 50,
      "items": {
        "$ref": "#/$defs/item"
      }
    },
    "wanted_by": {
      "$ref": "#/$defs/date"
    },
    "total": {
      "$ref": "#/$defs/money"
    },
    "delivery_by": {
      "$ref": "#/$defs/date"
    },
    "replaces": {
      "$ref": "#/$defs/uuid"
    },
    "slots": {
      "type": "array",
      "minItems": 1,
      "maxItems": 5,
      "items": {
        "$ref": "#/$defs/slot"
      }
    },
    "purpose": {
      "type": "string",
      "maxLength": 200
    },
    "location": {
      "type": "string",
      "maxLength": 500
    },
    "ref": {
      "$ref": "#/$defs/uuid"
    },
    "slot": {
      "type": "integer",
      "minimum": 0,
      "maximum": 4
    },
    "reason": {
      "enum": [
        "price",
        "timing",
        "not_available",
        "other"
      ]
    },
    "note": {
      "type": "string",
      "maxLength": 2000
    },
    "authority": {
      "$ref": "#/$defs/authority"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "type": {
            "const": "ask_quote"
          }
        }
      },
      "then": {
        "required": [
          "items"
        ],
        "not": {
          "anyOf": [
            {
              "required": [
                "total"
              ]
            },
            {
              "required": [
                "slots"
              ]
            },
            {
              "required": [
                "ref"
              ]
            },
            {
              "required": [
                "slot"
              ]
            },
            {
              "required": [
                "replaces"
              ]
            },
            {
              "required": [
                "reason"
              ]
            }
          ]
        }
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "quote"
          }
        }
      },
      "then": {
        "required": [
          "items",
          "total"
        ],
        "not": {
          "anyOf": [
            {
              "required": [
                "slots"
              ]
            },
            {
              "required": [
                "ref"
              ]
            },
            {
              "required": [
                "slot"
              ]
            },
            {
              "required": [
                "reason"
              ]
            }
          ]
        }
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "propose_times"
          }
        }
      },
      "then": {
        "required": [
          "slots"
        ],
        "not": {
          "anyOf": [
            {
              "required": [
                "items"
              ]
            },
            {
              "required": [
                "total"
              ]
            },
            {
              "required": [
                "ref"
              ]
            },
            {
              "required": [
                "slot"
              ]
            },
            {
              "required": [
                "reason"
              ]
            }
          ]
        }
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "accept"
          }
        }
      },
      "then": {
        "required": [
          "ref"
        ],
        "not": {
          "anyOf": [
            {
              "required": [
                "items"
              ]
            },
            {
              "required": [
                "total"
              ]
            },
            {
              "required": [
                "slots"
              ]
            },
            {
              "required": [
                "replaces"
              ]
            },
            {
              "required": [
                "reason"
              ]
            }
          ]
        }
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "decline"
          }
        }
      },
      "then": {
        "required": [
          "ref"
        ],
        "not": {
          "anyOf": [
            {
              "required": [
                "items"
              ]
            },
            {
              "required": [
                "total"
              ]
            },
            {
              "required": [
                "slots"
              ]
            },
            {
              "required": [
                "replaces"
              ]
            },
            {
              "required": [
                "slot"
              ]
            }
          ]
        }
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "cancel"
          }
        }
      },
      "then": {
        "required": [
          "ref"
        ],
        "not": {
          "anyOf": [
            {
              "required": [
                "items"
              ]
            },
            {
              "required": [
                "total"
              ]
            },
            {
              "required": [
                "slots"
              ]
            },
            {
              "required": [
                "replaces"
              ]
            },
            {
              "required": [
                "slot"
              ]
            },
            {
              "required": [
                "reason"
              ]
            }
          ]
        }
      }
    }
  ]
}
