{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:open-utility-stack:product-lifecycle:1",
  "title": "product-lifecycle@1",
  "description": "A product (a supply contract, a line, a charging contract) changed state. A profile of TMF637 Product Inventory v5: its ProductStatusType values and nothing else. Every role that holds products emits it, whatever its own event types are called (G2).",
  "type": "object",
  "additionalProperties": false,
  "required": ["profile", "product", "state", "effective", "sector"],
  "properties": {
    "profile": { "const": "product-lifecycle@1" },
    "client_reference": {
      "description": "The reference of the order that caused the change, unchanged (REF-2). Absent when no order did, such as an Ersatzversorgung.",
      "$ref": "urn:open-utility-stack:common:reference:1#/$defs/ClientReference"
    },
    "product": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id"],
      "properties": {
        "id": { "description": "The emitter's product id, stable for the product's life.", "type": "string", "pattern": "\\S", "maxLength": 128 },
        "number": { "description": "The number a customer sees on letters.", "type": "string", "pattern": "\\S", "maxLength": 64 },
        "account": { "description": "The customer account the product belongs to (konto's account reference), where the emitter knows it.", "type": "string", "pattern": "\\S", "maxLength": 128 }
      }
    },
    "state": {
      "description": "TMF637 ProductStatusType.",
      "enum": ["created", "pendingActive", "active", "suspended", "pendingTerminate", "terminated", "cancelled", "aborted"]
    },
    "previous_state": {
      "enum": ["created", "pendingActive", "active", "suspended", "pendingTerminate", "terminated", "cancelled", "aborted"]
    },
    "effective": {
      "description": "The business date the state takes or took effect: a Lieferbeginn, a Lieferende.",
      "$ref": "urn:open-utility-stack:common:time:1#/$defs/ZonedDate"
    },
    "sector": { "enum": ["electricity", "gas", "heat", "water", "telco", "charging", "other"] },
    "reason": {
      "description": "Why the state was reached. Required on `pendingTerminate`: who terminated, or `withdrawn` for a Widerruf. A withdrawal carries `withdrawn` on every product event it causes, whatever the state.",
      "$ref": "urn:open-utility-stack:common:reference:1#/$defs/Reason"
    },
    "extensions": {
      "description": "Sector or emitter detail, one object per namespace: `energy` (BO4E names, such as `marktlokation`), `de.mako` (the emitter's own event data).",
      "type": "object",
      "propertyNames": { "pattern": "^[a-z][a-z0-9-]*(\\.[a-z][a-z0-9-]*)*$" },
      "additionalProperties": { "type": "object" }
    }
  },
  "allOf": [
    {
      "if": { "properties": { "state": { "const": "created" } } },
      "then": { "not": { "required": ["previous_state"] } }
    },
    {
      "if": { "properties": { "state": { "const": "pendingTerminate" } }, "required": ["state"] },
      "then": {
        "required": ["reason"],
        "properties": { "reason": { "properties": { "code": { "enum": ["terminated-by-customer", "terminated-by-supplier", "withdrawn", "other"] } } } }
      }
    }
  ]
}
