{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:open-utility-stack:order:1",
  "title": "order@1",
  "description": "An order handed to a contract system changed state. A profile of TMF622 Product Ordering v5: its ProductOrderStateType values and item actions. The contract system emits it, whichever engine it is; the party that handed the order over consumes it (G5: the outcome of a command arrives as events).",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "profile",
    "client_reference",
    "state"
  ],
  "properties": {
    "profile": {
      "const": "order@1"
    },
    "client_reference": {
      "description": "The hand-over's reference, unchanged (REF-2); `subject` carries the same value.",
      "$ref": "urn:open-utility-stack:common:reference:1#/$defs/ClientReference"
    },
    "order": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id"
      ],
      "properties": {
        "id": {
          "description": "The contract system's own id for the order.",
          "type": "string",
          "pattern": "\\S",
          "maxLength": 128
        }
      }
    },
    "state": {
      "description": "TMF622 ProductOrderStateType. `acknowledged`: received and checked for form; `inProgress`: accepted and being carried out, also while some items are done and others open; `completed`: finished, every item delivered; `partial`: finished, some items delivered and some failed; `failed`: finished, every item failed; `rejected`: refused before work began; `cancelled`: withdrawn, such as a § 355 BGB Widerruf.",
      "enum": [
        "acknowledged",
        "rejected",
        "inProgress",
        "held",
        "pending",
        "completed",
        "partial",
        "failed",
        "cancelled"
      ]
    },
    "items": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "id",
          "action",
          "state"
        ],
        "properties": {
          "id": {
            "description": "The item id from the hand-over.",
            "type": "string",
            "pattern": "\\S",
            "maxLength": 64
          },
          "action": {
            "enum": [
              "add",
              "modify",
              "suspend",
              "resume",
              "delete",
              "noChange"
            ]
          },
          "state": {
            "enum": [
              "acknowledged",
              "rejected",
              "inProgress",
              "held",
              "pending",
              "completed",
              "failed",
              "cancelled"
            ]
          },
          "product": {
            "description": "The product the item created or changed; its product-lifecycle@1 events carry the same id.",
            "type": "object",
            "additionalProperties": false,
            "required": [
              "id"
            ],
            "properties": {
              "id": {
                "type": "string",
                "pattern": "\\S",
                "maxLength": 128
              }
            }
          },
          "reason": {
            "$ref": "urn:open-utility-stack:common:reference:1#/$defs/Reason"
          },
          "suspension": {
            "description": "Echoed from the request on a suspend item, so the outcome names the earliest date it answers.",
            "$ref": "#/$defs/Suspension"
          },
          "effective": {
            "description": "The business day the item took effect, such as the supply start of a completed `add`. Set it on a completed item whenever the emitter knows it; a consumer must not wait for the product event to learn it.",
            "$ref": "urn:open-utility-stack:common:time:1#/$defs/ZonedDate"
          }
        }
      }
    },
    "reason": {
      "$ref": "urn:open-utility-stack:common:reference:1#/$defs/Reason",
      "description": "Required for rejected, failed, partial, cancelled and held. `code` is one of the profile's codes: order.invalid (the order failed the contract system's form or content checks), order.refused (an operator of the contract system refused it), market.rejected (a market counterparty refused it, such as a grid operator's or previous supplier's answer), withdrawn (the customer withdrew it, § 355 BGB), other; for a suspend item suspension.too-early (before `earliest` or before the law's leads have run), suspension.protected (the engine's law forbids it for this customer, such as a hardship or medical case), suspension.below-minimum, suspension.disputed (the claim is disputed in the engine), suspension.not-active (the product is not active), suspension.unknown-product. A counterparty's original code, its code list and an operator's free text stay in the emitter's extension (`extensions[\"de.<emitter>\"]`), not in `reason`."
    },
    "at": {
      "description": "When the contract system reached the state.",
      "$ref": "urn:open-utility-stack:common:time:1#/$defs/Instant"
    },
    "extensions": {
      "type": "object",
      "propertyNames": {
        "pattern": "^[a-z][a-z0-9-]*(\\.[a-z][a-z0-9-]*)*$"
      },
      "additionalProperties": {
        "type": "object"
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "state": {
            "enum": [
              "rejected",
              "failed",
              "partial",
              "cancelled",
              "held"
            ]
          }
        }
      },
      "then": {
        "required": [
          "reason"
        ]
      }
    },
    {
      "properties": {
        "reason": {
          "properties": {
            "code": {
              "enum": [
                "order.invalid",
                "order.refused",
                "market.rejected",
                "withdrawn",
                "suspension.too-early",
                "suspension.protected",
                "suspension.below-minimum",
                "suspension.disputed",
                "suspension.not-active",
                "suspension.unknown-product",
                "other"
              ]
            }
          }
        },
        "items": {
          "items": {
            "properties": {
              "reason": {
                "properties": {
                  "code": {
                    "enum": [
                      "order.invalid",
                      "order.refused",
                      "market.rejected",
                      "withdrawn",
                      "suspension.too-early",
                      "suspension.protected",
                      "suspension.below-minimum",
                      "suspension.disputed",
                      "suspension.not-active",
                      "suspension.unknown-product",
                      "other"
                    ]
                  }
                }
              }
            }
          }
        }
      }
    }
  ],
  "$defs": {
    "Suspension": {
      "description": "What a suspend item needs: the decision was taken by a person (D2), and the engine performs the stop under its own law (§ 19 StromGVV, § 61 TKG, AVBFernwärmeV § 33, AVBWasserV § 33). The reviewer's reasons stay with the account system.",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "earliest",
        "reviewer",
        "decided_on",
        "evidence_digest"
      ],
      "properties": {
        "earliest": {
          "description": "The first day the supply may be suspended; the engine may act later, never earlier.",
          "$ref": "urn:open-utility-stack:common:time:1#/$defs/ZonedDate"
        },
        "reviewer": {
          "description": "Who decided: an id of the person in the account system, not a name and not a reason.",
          "type": "string",
          "pattern": "\\S",
          "maxLength": 128
        },
        "decided_on": {
          "$ref": "urn:open-utility-stack:common:time:1#/$defs/Date"
        },
        "evidence_digest": {
          "description": "Digest of the account system's evidence for the decision (the threat, the announcement, the open items).",
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        }
      }
    },
    "Request": {
      "description": "The order a party hands over: the command, sent over REST with `Idempotency-Key` = `client_reference` (REF-3). Validate against `urn:open-utility-stack:order:1#/$defs/Request`. The answer is an acknowledgement; the outcome arrives as order@1 events.",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "profile",
        "client_reference",
        "items"
      ],
      "properties": {
        "profile": {
          "const": "order@1"
        },
        "client_reference": {
          "$ref": "urn:open-utility-stack:common:reference:1#/$defs/ClientReference"
        },
        "account": {
          "description": "The customer's contract account (konto's reference).",
          "type": "string",
          "pattern": "\\S",
          "maxLength": 128
        },
        "creditor": {
          "description": "The party the claim belongs to: the tenant named in the event `source` (CE-1), by its id in the stack.",
          "type": "string",
          "pattern": "\\S",
          "maxLength": 128
        },
        "items": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "id",
              "action"
            ],
            "properties": {
              "id": {
                "type": "string",
                "pattern": "\\S",
                "maxLength": 64
              },
              "action": {
                "enum": [
                  "add",
                  "modify",
                  "suspend",
                  "resume",
                  "delete",
                  "noChange"
                ]
              },
              "product": {
                "type": "object",
                "additionalProperties": false,
                "required": [
                  "id"
                ],
                "properties": {
                  "id": {
                    "type": "string",
                    "pattern": "\\S",
                    "maxLength": 128
                  }
                }
              },
              "suspension": {
                "$ref": "#/$defs/Suspension"
              },
              "extensions": {
                "type": "object",
                "propertyNames": {
                  "pattern": "^[a-z][a-z0-9-]*(\\.[a-z][a-z0-9-]*)*$"
                },
                "additionalProperties": {
                  "type": "object"
                }
              }
            },
            "allOf": [
              {
                "if": {
                  "properties": {
                    "action": {
                      "const": "suspend"
                    }
                  },
                  "required": [
                    "action"
                  ]
                },
                "then": {
                  "required": [
                    "suspension",
                    "product"
                  ]
                },
                "else": {
                  "not": {
                    "required": [
                      "suspension"
                    ]
                  }
                }
              },
              {
                "if": {
                  "properties": {
                    "action": {
                      "enum": [
                        "modify",
                        "suspend",
                        "resume",
                        "delete"
                      ]
                    }
                  },
                  "required": [
                    "action"
                  ]
                },
                "then": {
                  "required": [
                    "product"
                  ]
                }
              }
            ]
          }
        },
        "extensions": {
          "type": "object",
          "propertyNames": {
            "pattern": "^[a-z][a-z0-9-]*(\\.[a-z][a-z0-9-]*)*$"
          },
          "additionalProperties": {
            "type": "object"
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "items": {
                "contains": {
                  "properties": {
                    "action": {
                      "const": "suspend"
                    }
                  },
                  "required": [
                    "action"
                  ]
                }
              }
            }
          },
          "then": {
            "required": [
              "account",
              "creditor"
            ]
          }
        }
      ]
    }
  }
}
