{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:open-utility-stack:flex-settlement:1",
  "title": "flex-settlement@1",
  "description": "The settlement of one flex-order@1 for one day: per ISP, baseline, ordered, actual and delivered power and the deficiency, and the price less the penalty. A profile of UFTP 3.1 FlexOrderSettlement (§ 4.2.28), computed exactly as § 2.6, so both sides recompute it and compare one digest. Money reaches the account system as rated-charge@1.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "profile",
    "market",
    "client_reference",
    "baseline",
    "price",
    "penalty",
    "net_settlement",
    "isps"
  ],
  "properties": {
    "profile": {
      "const": "flex-settlement@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": {
      "description": "The order's reference (UFTP OrderReference).",
      "$ref": "urn:open-utility-stack:common:reference:1#/$defs/ClientReference"
    },
    "baseline": {
      "$ref": "urn:open-utility-stack:common:flex:1#/$defs/Baseline"
    },
    "price": {
      "$ref": "urn:open-utility-stack:common:money:1#/$defs/Money",
      "description": "The order's price × its activation factor (uftp SS-26), before the penalty."
    },
    "penalty": {
      "description": "Deducted from the price for the deficiency; zero or positive.",
      "allOf": [
        {
          "$ref": "urn:open-utility-stack:common:money:1#/$defs/Money"
        },
        {
          "properties": {
            "value": {
              "not": {
                "pattern": "^-"
              }
            }
          }
        }
      ]
    },
    "net_settlement": {
      "description": "Price minus penalty.",
      "$ref": "urn:open-utility-stack:common:money:1#/$defs/Money"
    },
    "settlement_digest": {
      "description": "The common/digest.md digest of this document without the settlement_digest field, which the other side recomputes from its own settlement.",
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "isps": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "start",
          "duration",
          "baseline_power",
          "ordered_flex_power",
          "actual_power",
          "delivered_flex_power",
          "power_deficiency"
        ],
        "properties": {
          "start": {
            "$ref": "urn:open-utility-stack:common:flex:1#/$defs/IspNumber"
          },
          "duration": {
            "$ref": "urn:open-utility-stack:common:flex:1#/$defs/IspNumber"
          },
          "baseline_power": {
            "$ref": "urn:open-utility-stack:common:flex:1#/$defs/Watts"
          },
          "ordered_flex_power": {
            "$ref": "urn:open-utility-stack:common:flex:1#/$defs/Watts",
            "description": "The order's power for this ISP × the activation factor, rounded half away from zero to whole watts."
          },
          "actual_power": {
            "$ref": "urn:open-utility-stack:common:flex:1#/$defs/Watts"
          },
          "delivered_flex_power": {
            "description": "Signed like the order: the deviation along the order, capped at it, never below zero along it.",
            "$ref": "urn:open-utility-stack:common:flex:1#/$defs/Watts"
          },
          "power_deficiency": {
            "description": "A non-negative magnitude in watts: how far delivery fell short of the order; a site that moved the wrong way owes more than was ordered.",
            "type": "integer",
            "minimum": 0,
            "maximum": 9007199254740992
          }
        }
      }
    }
  }
}
