{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:open-utility-stack:flex-order:1",
  "title": "flex-order@1",
  "description": "A grid operator orders flexibility, usually one option of a flex-offer@1, at a share of it. A profile of UFTP 3.1 FlexOrder (§ 4.2.27). The activation it carries reaches devices as a limit or schedule. `client_reference` is UFTP's OrderReference: the DSO's order number, quoted again in settlement (REF-1).",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "profile",
    "market",
    "client_reference",
    "unsolicited",
    "activation_factor",
    "price",
    "isps"
  ],
  "properties": {
    "profile": {
      "const": "flex-order@1"
    },
    "market": {
      "$ref": "urn:open-utility-stack:common:flex:1#/$defs/Market"
    },
    "contract_id": {
      "type": "string",
      "pattern": "\\S",
      "maxLength": 128
    },
    "extensions": {
      "type": "object",
      "propertyNames": {
        "pattern": "^[a-z][a-z0-9-]*(\\.[a-z][a-z0-9-]*)*$"
      },
      "additionalProperties": {
        "type": "object"
      }
    },
    "client_reference": {
      "$ref": "urn:open-utility-stack:common:reference:1#/$defs/ClientReference"
    },
    "unsolicited": {
      "type": "boolean"
    },
    "flex_offer": {
      "$ref": "urn:open-utility-stack:common:flex:1#/$defs/MessageId"
    },
    "option_reference": {
      "type": "string",
      "pattern": "\\S",
      "maxLength": 64
    },
    "activation_factor": {
      "$ref": "urn:open-utility-stack:common:flex:1#/$defs/Factor",
      "description": "The share of the option ordered. Applied once, at settlement: the ordered flex power per ISP is the option's power × this factor, rounded half away from zero to whole watts, and the settled price is the price × this factor. Never applied to this document's isps or price."
    },
    "service_type": {
      "type": "string",
      "pattern": "\\S",
      "maxLength": 64
    },
    "baseline": {
      "$ref": "urn:open-utility-stack:common:flex:1#/$defs/Baseline"
    },
    "price": {
      "$ref": "urn:open-utility-stack:common:money:1#/$defs/Money",
      "description": "The option's price, unscaled; settlement multiplies it by the activation factor."
    },
    "isps": {
      "description": "The option's power per ISP, unscaled, exactly as on the UFTP wire (a FlexOrder copies the option's ISPs); negative for less consumption. The activation factor applies once, at settlement, to power and price (uftp SS-26).",
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "urn:open-utility-stack:common:flex:1#/$defs/PowerIsp"
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "unsolicited": {
            "const": false
          }
        }
      },
      "then": {
        "required": [
          "flex_offer",
          "option_reference"
        ]
      }
    }
  ]
}
