{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:open-utility-stack:collection-terms:1",
  "title": "collection-terms@1",
  "description": "The rules under which an overdue claim from one contract may lead to a suspension of supply, as the contract's engine states them under its own law (§ 19 StromGVV and §§ 41f, 41g EnWG for electricity and gas, § 61 TKG, AVBFernwärmeV § 33, AVBWasserV § 33). The engine publishes it per contract or product; the account system (konto) evaluates it and proposes, never decides (D2). A suspension it proposes reaches the engine as an order@1 Request with a suspend item.",
  "type": "object",
  "additionalProperties": false,
  "required": ["profile", "contract", "producer", "title", "claim_nature", "minimum", "excluded", "threat_lead", "announcement_lead", "suspension", "legal_basis"],
  "properties": {
    "profile": { "const": "collection-terms@1" },
    "contract": { "description": "The contract or product the terms apply to, as `<producer>:<kind>/<id>`.", "type": "string", "pattern": "^[a-z][a-z0-9-]*:[a-z][a-z0-9-]*/\\S+$", "maxLength": 160 },
    "producer": { "description": "The engine that states the terms and performs any suspension.", "type": "string", "pattern": "^[a-z][a-z0-9-]*$", "maxLength": 32 },
    "title": { "description": "What the customer calls the supply: Strom, Gas, Internet.", "type": "string", "pattern": "\\S", "maxLength": 80 },
    "claim_nature": { "description": "`civil` for a private-law claim; `public` for a fee under public law (some water and waste charges), where collection follows administrative enforcement instead.", "enum": ["civil", "public"] },
    "minimum": {
      "description": "The overdue amount below which no suspension may be threatened. Each present criterion is a lower bound; the claim must reach the highest of them.",
      "type": "object",
      "additionalProperties": false,
      "minProperties": 1,
      "properties": {
        "at_least": { "description": "A fixed amount, such as 100 EUR (§ 19 Abs. 2 StromGVV).", "$ref": "urn:open-utility-stack:common:money:1#/$defs/Money" },
        "instalment_multiple": { "description": "A multiple of the monthly instalment, such as 2.", "type": "string", "pattern": "^(0|[1-9][0-9]*)(\\.[0-9]+)?$" },
        "annual_fraction": { "description": "A fraction of the expected annual bill, such as one sixth.", "$ref": "urn:open-utility-stack:common:money:1#/$defs/Rate" }
      }
    },
    "excluded": {
      "description": "Parts of the claim that never count towards the minimum.",
      "type": "array",
      "uniqueItems": true,
      "items": { "enum": ["disputed", "in_plan", "in_dispute_procedure", "paused", "not_yet_due", "advance_payment"] }
    },
    "threat_lead": { "description": "How long before the announcement the suspension must be threatened.", "$ref": "#/$defs/Lead" },
    "announcement_lead": { "description": "How long before the suspension it must be announced.", "$ref": "#/$defs/Lead" },
    "suspension": { "description": "Whether this contract can be suspended at all.", "type": "boolean" },
    "plan": {
      "description": "The instalment plan the customer must be offered with the threat (§ 41f Abs. 2 EnWG). Absent when the law requires none.",
      "type": "object",
      "additionalProperties": false,
      "required": ["ranges", "interest_free"],
      "properties": {
        "ranges": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["min_months", "max_months"],
            "properties": {
              "above": { "description": "Applies when the claim exceeds this amount; the range with the highest matching `above` wins.", "$ref": "urn:open-utility-stack:common:money:1#/$defs/Money" },
              "min_months": { "type": "integer", "minimum": 1, "maximum": 60 },
              "max_months": { "type": "integer", "minimum": 1, "maximum": 60 }
            }
          }
        },
        "interest_free": { "type": "boolean" }
      }
    },
    "blocks": {
      "description": "Text the threat and the announcement must contain, by id, in the language of the letter.",
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "threat": { "$ref": "#/$defs/Blocks" },
        "announcement": { "$ref": "#/$defs/Blocks" }
      }
    },
    "legal_basis": { "description": "The norms these terms implement, cited as the law does: `§ 19 Abs. 2 StromGVV`.", "type": "array", "minItems": 1, "items": { "type": "string", "pattern": "\\S", "maxLength": 80 } },
    "extensions": {
      "type": "object",
      "propertyNames": { "pattern": "^[a-z][a-z0-9-]*(\\.[a-z][a-z0-9-]*)*$" },
      "additionalProperties": { "type": "object" }
    }
  },
  "allOf": [
    { "if": { "properties": { "suspension": { "const": false } } }, "then": { "not": { "required": ["plan"] } } }
  ],
  "$defs": {
    "Lead": {
      "type": "object",
      "additionalProperties": false,
      "required": ["days", "unit"],
      "properties": {
        "days": { "type": "integer", "minimum": 0, "maximum": 365 },
        "unit": { "description": "`working_days` are Monday to Saturday (Werktage), `business_days` Monday to Friday; public holidays come from the deployment's calendar.", "enum": ["calendar_days", "working_days", "business_days"] }
      }
    },
    "Blocks": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["id", "text"],
        "properties": {
          "id": { "type": "string", "pattern": "^[a-z][a-z0-9-]*$", "maxLength": 40 },
          "text": { "type": "string", "pattern": "\\S", "maxLength": 2000 }
        }
      }
    }
  }
}
