{
  "openapi": "3.1.0",
  "info": {
    "title": "billingd — multi-product billing",
    "description": "Calculates, issues, corrects and renders invoices (BO4E `Rechnung`, EN 16931 CII/UBL, ZUGFeRD), settles § 41e VPP dispatches, and watches the § 40c Abs. 2 EnWG deadlines. Every refusal answers with `Problem`; branch on `error.code`.",
    "contact": {
      "name": "hupe1980"
    },
    "license": {
      "name": "MIT OR Apache-2.0",
      "identifier": "MIT OR Apache-2.0"
    },
    "version": "0.49.0"
  },
  "paths": {
    "/api/v1/billing": {
      "get": {
        "tags": [
          "records"
        ],
        "operationId": "list_records",
        "parameters": [
          {
            "name": "malo_id",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "lf_mp_id",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "outcome",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "category",
            "in": "query",
            "description": "One category — `VPP`, `SAMMEL`, `STROM`, …",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "is_correction",
            "in": "query",
            "description": "`true` = only Storno/Korrektur rows, `false` = only originals.",
            "required": false,
            "schema": {
              "type": "boolean"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "format": "int64"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Billing records, newest first",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "403": {
            "description": "Cedar `read-billing` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/deadline-summary": {
      "get": {
        "tags": [
          "deadlines"
        ],
        "summary": "`GET /api/v1/billing/deadline-summary`",
        "description": "Per Lieferant: invoices owed, issued on time, issued late and overdue under\n§ 40c Abs. 2 EnWG.",
        "operationId": "get_deadline_summary",
        "responses": {
          "200": {
            "description": "Per Lieferant: owed, on time, late and overdue under § 40c Abs. 2 EnWG",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DeadlineSummary"
                }
              }
            }
          },
          "403": {
            "description": "Cedar `read-billing-watch` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/ggv/{ggv_id}": {
      "post": {
        "tags": [
          "billing"
        ],
        "summary": "`POST /api/v1/billing/ggv/{ggv_id}` — §42b EnWG community solar (GGV) billing.",
        "description": "Allocates plant generation across tenants (§42b EnWG proportional\nallocation) and emits one invoice per tenant. Rejects an empty `tenants` list\nor a zero total-kWh input.\n\n## §42b EnWG (Solarpaket I) — two billing models\n\n**Model A — Nutzungsplan-based (recommended)**: supply `pv_generation_kwh` +\n`nutzungsplan`. Each participant is allocated their Aufteilungsschlüssel\nshare of the shareable generation (§42b Abs. 2 Nr. 1 EnWG); the invoice shows\nthat portion at the agreed Gebäudestrom rate and the remainder as residual\ngrid supply, which §42b Abs. 3 EnWG explicitly contemplates because the\noperator owes no Vollversorgung.\n\n**Model B — Direct consumption**: omit `pv_generation_kwh`. Each\nparticipant's full `consumption_kwh` is billed as Gebäudestrom. Only valid\nwhen the plant covered every participant for the whole period.\n\nThe price advantage on the shared portion is contractual (§42b Abs. 2 Nr. 2\nEnWG names the Vergütung as a contract term), not a statutory rebate.",
        "operationId": "post_ggv_billing",
        "parameters": [
          {
            "name": "ggv_id",
            "in": "path",
            "description": "The Gemeinschaftliche Gebäudeversorgung",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "description": "`GgvBillingRequest`: participants, Aufteilungsschlüssel and the generation",
          "content": {
            "application/json": {
              "schema": {}
            }
          },
          "required": true
        },
        "responses": {
          "201": {
            "description": "One invoice per participant and the bundle",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "400": {
            "description": "Malformed period or participants",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "403": {
            "description": "Cedar `run-billing` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "The period already carries an issued document, or the number is in use",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "Well-formed, but it cannot be billed as asked — the code names the rule",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/nachrechnen": {
      "post": {
        "tags": [
          "nachrechnen"
        ],
        "summary": "`POST /api/v1/billing/nachrechnen` `{ \"token\": \"…\" }`",
        "description": "The masked projection the public page (portald) renders: no name, no\naddress, the MaLo-ID masked. An unknown, malformed and a revoked token all\nanswer `404 RECORD_NOT_FOUND`, so the answer says nothing about whether a\ntoken ever existed. The token travels in the body, never in a path a\nrequest log or a metrics label could keep.",
        "operationId": "post_public_nachrechnung",
        "requestBody": {
          "description": "The verify token printed on the invoice",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/VerifyTokenBody"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "The masked recomputation the public page shows",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "403": {
            "description": "Cedar `read-billing-by-token` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "No invoice carries this token, or it was revoked",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/review-queue": {
      "get": {
        "tags": [
          "risk"
        ],
        "summary": "`GET /api/v1/billing/review-queue?band=&limit=`",
        "description": "The analyst work list: REVIEW and HELD records, highest risk first, each\ncarrying its coded findings.",
        "operationId": "get_review_queue",
        "parameters": [
          {
            "name": "band",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "format": "int64"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Records the risk gate banded REVIEW or HELD, highest score first",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "403": {
            "description": "Cedar `read-billing` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/runs/sweep": {
      "post": {
        "tags": [
          "deadlines"
        ],
        "summary": "Request the scheduled billing workflow on today's Berlin business date.",
        "operationId": "post_billing_sweep",
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "description": "UUID identifying this attempt; reuse it after an uncertain response",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SweepRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "Billing run completed or receipt replayed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SweepReceipt"
                }
              }
            }
          },
          "400": {
            "description": "A single UUID Idempotency-Key is required",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          },
          "403": {
            "description": "Cedar `run-billing-sweep` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          },
          "409": {
            "description": "Another run is active",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          },
          "422": {
            "description": "Unknown sweep option",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          },
          "502": {
            "description": "The attempt failed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/sammelrechnung/{rahmenvertrag_id}": {
      "post": {
        "tags": [
          "billing"
        ],
        "summary": "`POST /api/v1/billing/sammelrechnung/{rahmenvertrag_id}`",
        "description": "Consolidated B2B invoice for a `Rahmenvertrag` with `rechnungsstellung=SAMMEL`.\n\n## Pipeline\n\n1. Enumerate the Rahmenvertrag's active MaLos from `vertragd`.\n2. **Phase 1, no transaction:** price every site through the ordinary\n   calculation pipeline and resolve its BG-7 buyer. Nothing is written, so a\n   failure here leaves no trace.\n3. Consolidate the per-site invoices into one document — VAT recomputed once\n   over the combined base per rate.\n4. **Phase 2, one transaction:** the per-MaLo records, the consolidated\n   document, the links between them and the outbox event commit together.\n\nPhase 2 is **one** transaction and not four independent writes. A site\nfailing after two of its siblings have committed would leave orphan invoices\noccupying `br_unique_original`, so the retry cannot succeed either, and a\ndropped link would leave the children counted alongside the bundle that\nalready contains them. The GGV endpoint has the same shape.",
        "operationId": "post_sammelrechnung",
        "parameters": [
          {
            "name": "rahmenvertrag_id",
            "in": "path",
            "description": "The Rahmenvertrag billed with rechnungsstellung SAMMEL",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "description": "`SammelrechnungRequest`: the period",
          "content": {
            "application/json": {
              "schema": {}
            }
          },
          "required": true
        },
        "responses": {
          "201": {
            "description": "The consolidated document and its per-site records",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "400": {
            "description": "Malformed period",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "403": {
            "description": "Cedar `run-billing` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "The period already carries an issued document, or the number is in use",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "Well-formed, but it cannot be billed as asked — the code names the rule",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "502": {
            "description": "An upstream service the calculation reads did not answer",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/unbilled": {
      "get": {
        "tags": [
          "deadlines"
        ],
        "summary": "The LF's refused periods, ordered by due date, with the latest discovery evidence.",
        "operationId": "get_unbilled_periods",
        "parameters": [
          {
            "name": "open",
            "in": "query",
            "description": "Only periods no invoice has discharged yet (default `true`).",
            "required": false,
            "schema": {
              "type": "boolean"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "format": "int64"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Refused periods ordered by due date, with the latest discovery evidence",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/UnbilledPeriods"
                }
              }
            }
          },
          "403": {
            "description": "Cedar `read-billing-watch` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/vpp/{vpp_id}": {
      "post": {
        "tags": [
          "vpp"
        ],
        "operationId": "post_vpp_billing",
        "parameters": [
          {
            "name": "vpp_id",
            "in": "path",
            "description": "The VPP's Marktlokation",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/VppBillingRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "201": {
            "description": "The § 41e Gutschrift",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "400": {
            "description": "Malformed dispatch",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "403": {
            "description": "Cedar `settle-flexibility` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "A concurrent settlement claimed a dispatch, or the number is in use",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "Well-formed, but it cannot be billed as asked — the code names the rule",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "502": {
            "description": "An upstream service the calculation reads did not answer",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/{id}": {
      "get": {
        "tags": [
          "records"
        ],
        "operationId": "get_record",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The billing record",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The record with its BO4E Rechnung",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "403": {
            "description": "Cedar `read-billing` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "No such billing record in this tenant (`RECORD_NOT_FOUND`)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/{id}/correction": {
      "post": {
        "tags": [
          "billing"
        ],
        "summary": "Reverse an issued invoice, optionally issuing its replacement in the same transaction.",
        "operationId": "post_correction",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The billing record",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "requestBody": {
          "description": "Reason for the cancellation and optional replacement calculation inputs",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CorrectionRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "201": {
            "description": "The Stornorechnung and, when requested, its replacement invoice",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "400": {
            "description": "Invalid request or billing dates",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "403": {
            "description": "Correction or replacement calculation is unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "No such billing record in this tenant (`RECORD_NOT_FOUND`)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "Invoice already cancelled, unissued, or replacement withheld",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "Missing reason, invalid replacement scope or unbillable inputs",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "502": {
            "description": "Billing input service unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/{id}/nachrechnung": {
      "get": {
        "tags": [
          "nachrechnen"
        ],
        "summary": "`GET /api/v1/billing/{id}/nachrechnung`",
        "description": "Rechnung nachrechnen: every line and total of the stored invoice recomputed\nto the cent, whether the issue-time seal still holds, and the inputs of\neach line (quantity and how it was obtained, unit price and its source,\nthe rule).",
        "operationId": "get_nachrechnung",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The billing record",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The invoice recomputed line by line, with the seal check",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "403": {
            "description": "Cedar `read-billing` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "No such billing record in this tenant (`RECORD_NOT_FOUND`)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "A Storno- or Korrekturrechnung; recompute its original",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/{id}/pdf": {
      "get": {
        "tags": [
          "documents"
        ],
        "summary": "`GET /api/v1/billing/{id}/pdf` — the ZUGFeRD document.",
        "description": "One file that is both things a customer and their accounting software need:\na page a person reads, and the EN 16931 invoice embedded inside it. Both come\nfrom the same stored model, so they cannot disagree.\n\nThe template is **pinned on the first render after dispatch**, so requesting\nthis a decade later reproduces the document that was issued rather than\nre-styling it with whatever layout is current. A draft still renders with\nthe current template every time — see [`crate::pg::pin_template`].\n\n# Conditional GET\n\nA **pinned** record's PDF is immutable by construction: same stored model,\nsame pinned template, and a creation timestamp taken from BT-2 rather than\nthe clock, so the bytes are identical on every render. That is exactly what\na strong `ETag` and `Cache-Control: immutable` describe, and an\n`If-None-Match` hit answers `304` without waking the renderer at all — a\ncustomer portal was otherwise spending outputd's full render budget every\ntime somebody re-opened an invoice they had already seen.\n\nA **draft** carries neither header: it re-renders with whatever template is\ncurrent, so it is not the same document twice and must not be cached.",
        "operationId": "get_invoice_pdf",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The billing record",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "ZUGFeRD PDF/A-3 with the CII XML embedded",
            "content": {
              "application/pdf": {
                "schema": {
                  "type": "array",
                  "items": {
                    "type": "integer",
                    "format": "int32",
                    "minimum": 0
                  }
                }
              }
            }
          },
          "304": {
            "description": "A pinned record's PDF is unchanged (`If-None-Match`)"
          },
          "403": {
            "description": "Cedar `read-billing` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "No such billing record in this tenant (`RECORD_NOT_FOUND`)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "The record cannot be rendered",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "502": {
            "description": "An upstream service the calculation reads did not answer",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/{id}/release": {
      "post": {
        "tags": [
          "risk"
        ],
        "summary": "`POST /api/v1/billing/{id}/release`",
        "description": "Analyst release of a HELD record: stamps who released it and dispatches\nthe CloudEvent that the risk gate withheld. 409 when the record is not\ncurrently held.",
        "operationId": "post_release",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The billing record",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Released and dispatched",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "403": {
            "description": "Cedar `release-billing` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "No such billing record in this tenant (`RECORD_NOT_FOUND`)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "The record is not held",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/{id}/reviewed": {
      "post": {
        "tags": [
          "risk"
        ],
        "summary": "`POST /api/v1/billing/{id}/reviewed`",
        "description": "An analyst's review of a REVIEW record: stamps who reviewed it and takes it\noff the work list. The invoice was already issued when the risk gate\nbanded it, so this sends nothing — a finding that holds is answered with a\ncorrection (`POST …/correction`), not here. 409 when the record is not an\nunreviewed REVIEW record.",
        "operationId": "post_reviewed",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The billing record",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The review is recorded; nothing is sent",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "403": {
            "description": "Cedar `review-billing` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "No such billing record in this tenant (`RECORD_NOT_FOUND`)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "The record is not banded REVIEW, or was reviewed already",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/{id}/submit-b2g": {
      "post": {
        "tags": [
          "documents"
        ],
        "summary": "`POST /api/v1/billing/{id}/submit-b2g`",
        "description": "Prepare an XRechnung 3.0 CII XML from the billing record and notify the\nconfigured ERP webhook so the ERP's PEPPOL AS4 gateway can transmit it\nto the ZRE / OZG-RE portal.\n\n## Why not send directly?\n\nPEPPOL AS4 transport requires an accredited access-point operator\n(Peppol AP) and a registered Peppol participant ID.  These are ERP /\nplatform operator responsibilities.  `billingd` generates the\nEN 16931-conformant XML and hands it to the ERP via CloudEvent;\nthe ERP's AS4 gateway performs the actual network submission.\n\n## Regulatory\n\nB2G e-invoicing to federal contracting authorities has been **mandatory\nsince 27.11.2020** — § 4a EGovG plus the E-Rechnungsverordnung (ERechV,\nin force 27.11.2018), which transpose EU Directive 2014/55/EU; direct\norders up to EUR 1 000 are exempt (§ 3 Abs. 3 ERechV). The 2027/2028 dates\nbelong to the separate **B2B** mandate in § 14 UStG and have nothing to do\nwith this endpoint.",
        "operationId": "post_submit_b2g",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The billing record",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "requestBody": {
          "description": "`SubmitB2gRequest`: the portal, the Leitweg-ID and the public buyer",
          "content": {
            "application/json": {
              "schema": {}
            }
          },
          "required": true
        },
        "responses": {
          "202": {
            "description": "The XRechnung is queued for the ERP to transmit",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "403": {
            "description": "Cedar `submit-b2g` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "No such billing record in this tenant (`RECORD_NOT_FOUND`)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "The document violates the XRechnung rules",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "503": {
            "description": "No ERP webhook is configured (`NO_ERP_WEBHOOK`)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/{id}/ubl": {
      "get": {
        "tags": [
          "documents"
        ],
        "summary": "`GET /api/v1/billing/{id}/ubl`",
        "description": "Render the stored EN 16931 model as PEPPOL BIS Billing 3.0 (UBL 2.1).\n\nUBL and CII are the **two permitted syntaxes** of EN 16931, not a hierarchy:\n§ 14 UStG requires conformance to the norm and accepts either, and so does\nDirective 2014/55/EU. CII is what German receivers overwhelmingly expect\n(XRechnung, ZUGFeRD); UBL is what a cross-border PEPPOL receiver usually\nwants, which is the reason both endpoints exist.",
        "operationId": "get_ubl",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The billing record",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "PEPPOL BIS Billing 3.0 UBL 2.1 rendered from the stored model",
            "content": {
              "application/xml": {
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "403": {
            "description": "Cedar `read-billing` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "No such billing record in this tenant (`RECORD_NOT_FOUND`)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "No stored EN 16931 model, or one that does not render",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/{id}/verify-token/revoke": {
      "post": {
        "tags": [
          "nachrechnen"
        ],
        "summary": "`POST /api/v1/billing/{id}/verify-token/revoke`",
        "description": "Revoke the invoice's verify token: its public page answers 404 from now on.",
        "operationId": "post_revoke_verify_token",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The billing record",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Revoked; the public page answers 404 from now on",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "403": {
            "description": "Cedar `correct-billing` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "No such billing record in this tenant (`RECORD_NOT_FOUND`)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/{id}/versenden": {
      "post": {
        "tags": [
          "documents"
        ],
        "summary": "`POST /api/v1/billing/{id}/versenden` — issue this invoice, or this\nGebührenbescheid, as a document and queue it for delivery.",
        "description": "`GET /{id}/pdf` renders on demand, which answers neither question a supplier\nis actually asked:\n\n* **reproduce what was issued** — § 14 Abs. 1 Satz 2 UStG and\n  § 147 Abs. 1 Nr. 2–3 AO keep the Rechnungsdoppel for eight years in the\n  form in which it was issued, and a re-render follows today's template;\n* **did the customer receive it** — § 40c Abs. 2 EnWG puts the invoice in\n  the customer's hands within three or six weeks of the period end.\n\nThis stores the rendered bytes in `outputd` and queues them on the\nrecipient's channels, idempotent on the Rechnungsnummer. A **draft** the\nrisk gate is holding is refused: it has no booked receivable, so sending it\nwould demand money the ledger does not record as owed.",
        "operationId": "post_invoice_versenden",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The billing record",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "202": {
            "description": "Recorded in outputd for 8 years and queued on the customer's channels",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "403": {
            "description": "Cedar `issue-billing` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "No such billing record in this tenant (`RECORD_NOT_FOUND`)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "A draft the risk gate holds",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "The record cannot be rendered",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "502": {
            "description": "An upstream service the calculation reads did not answer",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/{id}/xrechnung": {
      "get": {
        "tags": [
          "documents"
        ],
        "summary": "`GET /api/v1/billing/{id}/xrechnung` — ZUGFeRD 2.3 / XRechnung 3.0 CII XML.",
        "operationId": "get_xrechnung",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The billing record",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "EN 16931 CII XML rendered from the stored model",
            "content": {
              "application/xml": {
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "403": {
            "description": "Cedar `read-billing` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "No such billing record in this tenant (`RECORD_NOT_FOUND`)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "No stored EN 16931 model, or one that does not satisfy its profile",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/{malo_id}/calculate": {
      "post": {
        "tags": [
          "billing"
        ],
        "summary": "`POST /api/v1/billing/{malo_id}/calculate`",
        "description": "Pipeline:\n1. Parse + validate period\n2. Fetch `Product` from `productd` (or use request override)\n3. Fetch consumption from `edmd` (or use request override)\n4. Take grid pass-through from the request's `grid` (nothing fetches it from `marktd`)\n5. Dispatch to category-specific pure calculator\n6. Persist `billing_records` (idempotent on same malo+period+product)\n7. Emit `de.mako.billing.rechnung.erstellt` CloudEvent",
        "operationId": "post_calculate",
        "parameters": [
          {
            "name": "malo_id",
            "in": "path",
            "description": "11-digit Marktlokations-ID",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "description": "Supply period, Lieferant and optional pricing inputs. Set `recipient_electricity_reseller` to request electricity reverse charge.",
          "content": {
            "application/json": {
              "schema": {}
            }
          },
          "required": true
        },
        "responses": {
          "201": {
            "description": "Calculated, persisted and announced",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "400": {
            "description": "Malformed period or input",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "403": {
            "description": "Cedar `run-billing` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "The period already carries an issued document, or the number is in use",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "Billing refused. For RESELLER_EVIDENCE_REQUIRED, error.evidence lists each affected party and reason.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "502": {
            "description": "An upstream service the calculation reads did not answer",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/{malo_id}/preview": {
      "post": {
        "tags": [
          "billing"
        ],
        "summary": "`POST /api/v1/billing/{malo_id}/preview` — dry-run, no persist, no CloudEvent.",
        "operationId": "post_preview",
        "parameters": [
          {
            "name": "malo_id",
            "in": "path",
            "description": "11-digit Marktlokations-ID",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "description": "`CalculateRequest`, as for `/calculate`",
          "content": {
            "application/json": {
              "schema": {}
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "The positions, totals and warnings a run would produce; nothing persisted",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "400": {
            "description": "Malformed period or input",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "403": {
            "description": "Cedar `preview-billing` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "Billing refused. For RESELLER_EVIDENCE_REQUIRED, error.evidence lists each affected party and reason.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "502": {
            "description": "An upstream service the calculation reads did not answer",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/billing/{malo_id}/tarifwechsel": {
      "post": {
        "tags": [
          "billing"
        ],
        "summary": "`POST /api/v1/billing/{malo_id}/tarifwechsel`",
        "description": "Calculates a combined invoice for a billing period containing a price change\n(Tarifwechsel). The period is split at `switch_date`:\n\n- **Sub-period A**: `period_from` → `switch_date - 1` at `old_tariff`\n- **Sub-period B**: `switch_date` → `period_to` at `new_tariff`\n\nThe two invoices are merged via [`Invoice::merge()`]: positions are\nconcatenated, totals re-summed. Tax is applied **independently** per sub-period (correct for\nmid-month rate changes per §41 EnWG).\n\n## Legal basis\n\n§41 Abs. 1 Nr. 4 EnWG: every price change requires transparent itemisation\non the next invoice showing the old and new price with their respective\napplicable periods.",
        "operationId": "post_tarifwechsel",
        "parameters": [
          {
            "name": "malo_id",
            "in": "path",
            "description": "11-digit Marktlokations-ID",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "description": "`TarifwechselRequest`: the period, the switch date and both tariffs",
          "content": {
            "application/json": {
              "schema": {}
            }
          },
          "required": true
        },
        "responses": {
          "201": {
            "description": "One invoice across the price change",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "400": {
            "description": "Malformed period or switch date",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "403": {
            "description": "Cedar `run-billing` refused",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "The period already carries an issued document, or the number is in use",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "Well-formed, but it cannot be billed as asked — the code names the rule",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "502": {
            "description": "An upstream service the calculation reads did not answer",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/events": {
      "get": {
        "tags": [
          "webhooks"
        ],
        "summary": "The service's events within the retention window, in commit order — the\nfeed an integrator polls instead of, or beside, an endpoint. Each item is\nthe CloudEvent as delivered.",
        "operationId": "list_events",
        "parameters": [
          {
            "name": "type",
            "in": "query",
            "description": "A `mako_events::matches` pattern (`de.mako.vertrag.*`); every type when absent.",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "description": "Rows per page, 1–200; 50 when absent.",
            "required": false,
            "schema": {
              "type": "integer",
              "format": "int32",
              "minimum": 0
            }
          },
          {
            "name": "cursor",
            "in": "query",
            "description": "The `next_cursor` of the previous page.",
            "required": false,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Page_Value"
                }
              }
            }
          },
          "400": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/webhooks/deliveries/{id}/replay": {
      "post": {
        "tags": [
          "webhooks"
        ],
        "summary": "Queue one delivery again, whatever its state.",
        "operationId": "replay_delivery",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The delivery's id",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Replayed"
                }
              }
            }
          },
          "404": {
            "description": "`delivery_not_found`",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          },
          "415": {
            "description": "`json_required`",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          },
          "422": {
            "description": "`outside_retention`",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/webhooks/endpoints": {
      "get": {
        "tags": [
          "webhooks"
        ],
        "summary": "The endpoints, a page ordered by name.",
        "operationId": "list_endpoints",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "description": "Rows per page, 1–200; 50 when absent.",
            "required": false,
            "schema": {
              "type": "integer",
              "format": "int32",
              "minimum": 0
            }
          },
          {
            "name": "cursor",
            "in": "query",
            "description": "The `next_cursor` of the previous page; absent for the first.",
            "required": false,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Page_Endpoint"
                }
              }
            }
          },
          "400": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          }
        }
      },
      "post": {
        "tags": [
          "webhooks"
        ],
        "summary": "Register an endpoint; the answer shows its secret once.",
        "operationId": "create_endpoint",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/EndpointCreate"
              }
            }
          },
          "required": true
        },
        "responses": {
          "201": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/EndpointCreated"
                }
              }
            }
          },
          "409": {
            "description": "`endpoint_name_taken`",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          },
          "422": {
            "description": "`endpoint_url_not_allowed`, `endpoint_name_reserved`",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/webhooks/endpoints/{id}": {
      "get": {
        "tags": [
          "webhooks"
        ],
        "summary": "One endpoint, with its version as `ETag`.",
        "operationId": "get_endpoint",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The endpoint's id",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Endpoint"
                }
              }
            }
          },
          "404": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          }
        }
      },
      "put": {
        "tags": [
          "webhooks"
        ],
        "summary": "Replace an endpoint; `If-Match` names the version read.",
        "operationId": "update_endpoint",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The endpoint's id",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/EndpointUpdate"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Endpoint"
                }
              }
            }
          },
          "409": {
            "description": "`endpoint_config_owned`",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          },
          "412": {
            "description": "`endpoint_version_mismatch`",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          }
        }
      },
      "delete": {
        "tags": [
          "webhooks"
        ],
        "summary": "Delete an endpoint; its pending deliveries are cancelled with it.",
        "operationId": "delete_endpoint",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The endpoint's id",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "204": {
            "description": ""
          },
          "409": {
            "description": "`endpoint_config_owned`",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          },
          "412": {
            "description": "`endpoint_version_mismatch`",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/webhooks/endpoints/{id}/deliveries": {
      "get": {
        "tags": [
          "webhooks"
        ],
        "summary": "The deliveries of one endpoint, newest first.",
        "operationId": "list_deliveries",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The endpoint's id",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "status",
            "in": "query",
            "description": "Which deliveries; every one when absent.",
            "required": false,
            "schema": {
              "$ref": "#/components/schemas/DeliveryFilter"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "description": "Rows per page, 1–200; 50 when absent.",
            "required": false,
            "schema": {
              "type": "integer",
              "format": "int32",
              "minimum": 0
            }
          },
          {
            "name": "cursor",
            "in": "query",
            "description": "The `next_cursor` of the previous page.",
            "required": false,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Page_Delivery"
                }
              }
            }
          },
          "404": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/webhooks/endpoints/{id}/replay": {
      "post": {
        "tags": [
          "webhooks"
        ],
        "summary": "Queue every event of a time window again for one endpoint, including\nevents that never had a delivery to it.",
        "operationId": "replay_window",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The endpoint's id",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ReplayWindow"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Replayed"
                }
              }
            }
          },
          "422": {
            "description": "`outside_retention` (with `oldest_replayable`), `replay_window_invalid`, `replay_window_too_large`",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/webhooks/endpoints/{id}/rotate-secret": {
      "post": {
        "tags": [
          "webhooks"
        ],
        "summary": "Rotate the secret: the new one signs from now, the previous one beside it\nfor `overlap_hours`. The answer shows the new secret once.",
        "operationId": "rotate_secret",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The endpoint's id",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RotateSecret"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SecretRotated"
                }
              }
            }
          },
          "409": {
            "description": "`endpoint_config_owned`",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          },
          "422": {
            "description": "`overlap_too_long`",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/webhooks/endpoints/{id}/test": {
      "post": {
        "tags": [
          "webhooks"
        ],
        "summary": "Send `de.mako.webhook.test` to the endpoint now, signed like a delivery.",
        "operationId": "test_endpoint",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The endpoint's id",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TestDelivery"
                }
              }
            }
          },
          "404": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          },
          "415": {
            "description": "`json_required`",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/webhooks/vpp-dispatch": {
      "post": {
        "tags": [
          "vpp"
        ],
        "summary": "`POST /api/v1/webhooks/vpp-dispatch`",
        "description": "**Dispatch-confirmed auto-settlement.**\n\nReceives `de.mako.vpp.dispatch.confirmed` CloudEvents emitted by `makod` when the\nMSB sends a positive `EndantwortPositiv` for a WiM Steuerungsauftrag\nand writes one Gutschrift per dispatch against the\n§41e Aggregatorvertrag in force **on the day the dispatch executed** —\n`vertragd` owns that contract; billingd keeps no copy.\n\n## Idempotency\n\nEach `tx_id` is recorded in `vpp_dispatch_ledger` **inside the settlement\ntransaction**, so a dispatch is settled exactly once and a redelivery\nreturns `202 Accepted` without re-settling. That ledger is also why\nper-dispatch records are exempt from `br_unique_original`: a portfolio is\ndispatched several times a day, and a one-row-per-period index cannot\nexpress that.\n\n## HMAC verification\n\nWhen `[inbound_webhook_secret]` is configured in `billingd.toml`, the\n`webhook-signature` header is verified, together with the `webhook-timestamp`\nfreshness that stops a captured request replaying.  Requests with\ninvalid or missing signatures are rejected with `401 Unauthorized`.\n\n## Auto-billing disabled\n\nWhen `vpp_auto_billing = false` in config (the default), the webhook accepts\nevents and records them in `vpp_dispatch_ledger` but does **not** generate a\n`Rechnung`.  The manual `POST /api/v1/billing/vpp/{vpp_id}` endpoint remains\navailable.\n\n## CloudEvent data schema\n\n```json\n{\n  \"tx_id\":               \"abc123\",\n  \"location_id\":         \"C0001234567890\",\n  \"location_type\":       \"sr\",\n  \"execution_time_from\": \"2026-01-15T10:00:00Z\",\n  \"execution_time_until\": \"2026-01-15T10:15:00Z\",\n  \"max_power_kw\":        \"11.0\",\n  \"command_type\":        \"Konfiguration\",\n  \"sender_mp_id\":        \"9900123456789\",\n  \"produkt_code\":        \"TX-MODUL2-HT\"\n}\n```\n\n`sender_mp_id` must be the contracted `aggregator_mp_id` for the dispatch to\nsettle. § 14a EnWG and § 41e ride the same Steuerungsauftrag, so the sender\nis what separates a Netzbetreiber's grid intervention from the aggregator's\nflexibility call — see step 5b.\n\nThe event carries **no price**, deliberately: what a dispatch is worth is the\n§ 41e Aggregatorvertrag's `capacity_price_eur_per_kwh` in `vertragd`, and a\ncounterparty does not get to state what it is owed.",
        "operationId": "post_vpp_webhook",
        "requestBody": {
          "description": "A `de.mako.vpp.dispatch.confirmed` CloudEvent, signed per Standard Webhooks",
          "content": {
            "application/json": {
              "schema": {}
            }
          },
          "required": true
        },
        "responses": {
          "202": {
            "description": "Accepted — settled, or recorded as not this aggregator's to settle"
          },
          "401": {
            "description": "The signature does not verify",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "Dispatch power or the execution window is missing or invalid",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "500": {
            "description": "The dispatch ledger could not be read; retry",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "ApiError": {
        "type": "object",
        "description": "The problem body every refusal answers with. Branch on `code`; `detail` is for the developer.",
        "required": [
          "error",
          "detail"
        ],
        "properties": {
          "code": {
            "type": "string",
            "description": "A stable English machine code (`limit_too_large`), where the refusal has one."
          },
          "detail": {
            "type": "string",
            "description": "What went wrong, for the developer."
          },
          "error": {
            "type": "string",
            "description": "The HTTP status's reason phrase."
          },
          "error_id": {
            "type": "string",
            "format": "uuid",
            "description": "On a 5xx: the id the service's log line carries; the cause is never in the body."
          },
          "upstream": {
            "type": "string",
            "description": "On a 502/503/504: the service this one called that failed."
          }
        },
        "additionalProperties": true
      },
      "CorrectionRequest": {
        "type": "object",
        "description": "Request body for `POST /api/v1/billing/{id}/correction`.",
        "required": [
          "reason"
        ],
        "properties": {
          "reason": {
            "type": "string",
            "description": "Human-readable reason for the correction (e.g. \"Zählerstandskorrektur\")."
          },
          "rechnungsnummer": {
            "type": [
              "string",
              "null"
            ],
            "description": "Override the Stornorechnung's number. Absent — the normal case — takes\nthe next number of the tenant's `ST` series."
          },
          "replacement": {
            "type": [
              "object",
              "null"
            ],
            "description": "Re-rate the same issuer and period and issue both documents atomically."
          }
        },
        "additionalProperties": false
      },
      "DeadlineSummary": {
        "type": "object",
        "description": "`summarize_billing_deadlines`' answer.",
        "required": [
          "lieferanten"
        ],
        "properties": {
          "discovery": {
            "oneOf": [
              {
                "type": "null"
              },
              {
                "$ref": "#/components/schemas/Discovery"
              }
            ]
          },
          "lieferanten": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/DeadlineSummaryRow"
            },
            "description": "One row per Lieferant (MP-ID)."
          }
        }
      },
      "DeadlineSummaryRow": {
        "type": "object",
        "description": "One Lieferant's § 40c Abs. 2 EnWG standing — the Überblick tile.",
        "required": [
          "lf_mp_id",
          "owed",
          "issued_on_time",
          "issued_late",
          "overdue"
        ],
        "properties": {
          "issued_late": {
            "type": "integer",
            "format": "int64",
            "description": "Invoices issued after their `due_on`."
          },
          "issued_on_time": {
            "type": "integer",
            "format": "int64",
            "description": "Invoices issued on or before their `due_on` (Berlin date of issue)."
          },
          "lf_mp_id": {
            "type": "string"
          },
          "overdue": {
            "type": "integer",
            "format": "int64",
            "description": "Open unbilled periods past their deadline."
          },
          "owed": {
            "type": "integer",
            "format": "int64",
            "description": "Open unbilled periods still inside their deadline."
          }
        }
      },
      "Delivery": {
        "type": "object",
        "description": "One event to one endpoint.",
        "required": [
          "id",
          "endpoint_id",
          "event_id",
          "event_type",
          "status",
          "attempts",
          "created_at"
        ],
        "properties": {
          "attempts": {
            "type": "integer",
            "format": "int32",
            "description": "Attempts made."
          },
          "created_at": {
            "type": "string",
            "format": "date-time",
            "description": "When the delivery was created."
          },
          "endpoint_id": {
            "type": "string",
            "format": "uuid",
            "description": "The endpoint delivered to."
          },
          "event_id": {
            "type": "string",
            "description": "The CloudEvent `id`, sent as `webhook-id`."
          },
          "event_type": {
            "type": "string",
            "description": "The CloudEvent `type`."
          },
          "id": {
            "type": "string",
            "format": "uuid",
            "description": "The delivery's id — what a single replay names."
          },
          "last_error": {
            "type": [
              "string",
              "null"
            ],
            "description": "The last attempt's failure."
          },
          "last_status": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int32",
            "description": "The HTTP status of the last attempt; absent when it got no answer."
          },
          "latency_ms": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int32",
            "description": "How long the last answered attempt took, in milliseconds."
          },
          "next_attempt_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time",
            "description": "When the next attempt is due, while pending."
          },
          "ordering_key": {
            "type": [
              "string",
              "null"
            ],
            "description": "The aggregate the event is about; deliveries of one key arrive in order."
          },
          "status": {
            "$ref": "#/components/schemas/DeliveryStatus",
            "description": "`pending`, `delivered` or `dead_lettered`."
          }
        }
      },
      "DeliveryFilter": {
        "type": "string",
        "description": "Which deliveries the log lists.",
        "enum": [
          "all",
          "pending",
          "delivered",
          "failed"
        ]
      },
      "DeliveryStatus": {
        "type": "string",
        "description": "A delivery's state.",
        "enum": [
          "pending",
          "delivered",
          "dead_lettered"
        ]
      },
      "Discovery": {
        "type": "object",
        "required": [
          "run_id",
          "business_date",
          "started_at",
          "source",
          "origin",
          "status",
          "ended_from",
          "ended_through"
        ],
        "properties": {
          "business_date": {
            "type": "string",
            "format": "date"
          },
          "candidate_count": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64"
          },
          "ended_from": {
            "type": "string",
            "format": "date"
          },
          "ended_through": {
            "type": "string",
            "format": "date"
          },
          "error_code": {
            "type": [
              "string",
              "null"
            ]
          },
          "finished_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "last_failure_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "next_attempt_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "origin": {
            "type": "string"
          },
          "run_id": {
            "type": "string",
            "format": "uuid"
          },
          "source": {
            "type": "string"
          },
          "started_at": {
            "type": "string",
            "format": "date-time"
          },
          "status": {
            "type": "string"
          }
        }
      },
      "Endpoint": {
        "type": "object",
        "description": "A webhook endpoint, as listed. Its secrets are never part of it.",
        "required": [
          "id",
          "name",
          "url",
          "event_types",
          "patterns_matching_nothing",
          "extensions",
          "owner",
          "active",
          "version",
          "created_at"
        ],
        "properties": {
          "active": {
            "type": "boolean",
            "description": "Whether deliveries are made. A `410 Gone` deactivates an endpoint."
          },
          "created_at": {
            "type": "string",
            "format": "date-time",
            "description": "When the endpoint was registered."
          },
          "deactivated_reason": {
            "type": [
              "string",
              "null"
            ],
            "description": "Why the endpoint was deactivated (`gone`)."
          },
          "event_types": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "`mako_events::matches` patterns; empty matches every type."
          },
          "extensions": {
            "type": "object",
            "description": "CloudEvent extensions an event must carry with these values\n(`marktsparte`, `marktrole`).",
            "additionalProperties": {
              "type": "string"
            },
            "propertyNames": {
              "type": "string"
            }
          },
          "id": {
            "type": "string",
            "format": "uuid",
            "description": "The endpoint's id."
          },
          "name": {
            "type": "string",
            "description": "Unique per service; `default` is the config URL."
          },
          "owner": {
            "$ref": "#/components/schemas/Owner",
            "description": "`config` or `api`."
          },
          "partner": {
            "type": [
              "string",
              "null"
            ],
            "description": "The Vertriebspartner the endpoint is bound to; it receives only events\nstamped with that partner."
          },
          "patterns_matching_nothing": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "The patterns of `event_types` that match no event type mako emits.\nThey deliver nothing; replace them."
          },
          "previous_secret_expires_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time",
            "description": "Until when the previous secret still signs, after a rotation."
          },
          "url": {
            "type": "string",
            "description": "Where deliveries are POSTed."
          },
          "version": {
            "type": "integer",
            "format": "int64",
            "description": "The version `If-Match` names."
          }
        }
      },
      "EndpointCreate": {
        "type": "object",
        "description": "The body of `POST /api/v1/webhooks/endpoints`.",
        "required": [
          "name",
          "url"
        ],
        "properties": {
          "event_types": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "`mako_events::matches` patterns (`de.mako.vertrag.*`); empty matches every type."
          },
          "extensions": {
            "type": "object",
            "description": "CloudEvent extensions an event must carry with these values.",
            "additionalProperties": {
              "type": "string"
            },
            "propertyNames": {
              "type": "string"
            }
          },
          "name": {
            "type": "string",
            "description": "Unique per service; `default` is reserved for the config URL."
          },
          "url": {
            "type": "string",
            "description": "`https` URL deliveries are POSTed to."
          }
        },
        "additionalProperties": false
      },
      "EndpointCreated": {
        "type": "object",
        "description": "A created endpoint and its secret — the only answer that shows it.",
        "required": [
          "endpoint",
          "secret"
        ],
        "properties": {
          "endpoint": {
            "$ref": "#/components/schemas/Endpoint",
            "description": "The endpoint."
          },
          "secret": {
            "type": "string",
            "description": "The `whsec_` signing secret; shown once."
          }
        }
      },
      "EndpointUpdate": {
        "type": "object",
        "description": "The body of `PUT /api/v1/webhooks/endpoints/{id}` — the whole endpoint.",
        "required": [
          "url",
          "active"
        ],
        "properties": {
          "active": {
            "type": "boolean",
            "description": "Whether deliveries are made."
          },
          "event_types": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "`mako_events::matches` patterns; empty matches every type."
          },
          "extensions": {
            "type": "object",
            "description": "CloudEvent extensions an event must carry with these values.",
            "additionalProperties": {
              "type": "string"
            },
            "propertyNames": {
              "type": "string"
            }
          },
          "url": {
            "type": "string",
            "description": "`https` URL deliveries are POSTed to."
          }
        },
        "additionalProperties": false
      },
      "Owner": {
        "type": "string",
        "description": "Who owns an endpoint.",
        "enum": [
          "config",
          "api"
        ]
      },
      "Page_Delivery": {
        "type": "object",
        "description": "One page of a list.",
        "required": [
          "items"
        ],
        "properties": {
          "items": {
            "type": "array",
            "items": {
              "type": "object",
              "description": "One event to one endpoint.",
              "required": [
                "id",
                "endpoint_id",
                "event_id",
                "event_type",
                "status",
                "attempts",
                "created_at"
              ],
              "properties": {
                "attempts": {
                  "type": "integer",
                  "format": "int32",
                  "description": "Attempts made."
                },
                "created_at": {
                  "type": "string",
                  "format": "date-time",
                  "description": "When the delivery was created."
                },
                "endpoint_id": {
                  "type": "string",
                  "format": "uuid",
                  "description": "The endpoint delivered to."
                },
                "event_id": {
                  "type": "string",
                  "description": "The CloudEvent `id`, sent as `webhook-id`."
                },
                "event_type": {
                  "type": "string",
                  "description": "The CloudEvent `type`."
                },
                "id": {
                  "type": "string",
                  "format": "uuid",
                  "description": "The delivery's id — what a single replay names."
                },
                "last_error": {
                  "type": [
                    "string",
                    "null"
                  ],
                  "description": "The last attempt's failure."
                },
                "last_status": {
                  "type": [
                    "integer",
                    "null"
                  ],
                  "format": "int32",
                  "description": "The HTTP status of the last attempt; absent when it got no answer."
                },
                "latency_ms": {
                  "type": [
                    "integer",
                    "null"
                  ],
                  "format": "int32",
                  "description": "How long the last answered attempt took, in milliseconds."
                },
                "next_attempt_at": {
                  "type": [
                    "string",
                    "null"
                  ],
                  "format": "date-time",
                  "description": "When the next attempt is due, while pending."
                },
                "ordering_key": {
                  "type": [
                    "string",
                    "null"
                  ],
                  "description": "The aggregate the event is about; deliveries of one key arrive in order."
                },
                "status": {
                  "$ref": "#/components/schemas/DeliveryStatus",
                  "description": "`pending`, `delivered` or `dead_lettered`."
                }
              }
            },
            "description": "The rows of this page, in the list's order."
          },
          "next_cursor": {
            "type": [
              "string",
              "null"
            ],
            "description": "The cursor of the next page; absent on the last."
          },
          "total_estimate": {
            "oneOf": [
              {
                "type": "null"
              },
              {
                "$ref": "#/components/schemas/TotalEstimate",
                "description": "How many rows the whole list holds, where the list counts them."
              }
            ]
          }
        }
      },
      "Page_Endpoint": {
        "type": "object",
        "description": "One page of a list.",
        "required": [
          "items"
        ],
        "properties": {
          "items": {
            "type": "array",
            "items": {
              "type": "object",
              "description": "A webhook endpoint, as listed. Its secrets are never part of it.",
              "required": [
                "id",
                "name",
                "url",
                "event_types",
                "patterns_matching_nothing",
                "extensions",
                "owner",
                "active",
                "version",
                "created_at"
              ],
              "properties": {
                "active": {
                  "type": "boolean",
                  "description": "Whether deliveries are made. A `410 Gone` deactivates an endpoint."
                },
                "created_at": {
                  "type": "string",
                  "format": "date-time",
                  "description": "When the endpoint was registered."
                },
                "deactivated_reason": {
                  "type": [
                    "string",
                    "null"
                  ],
                  "description": "Why the endpoint was deactivated (`gone`)."
                },
                "event_types": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  },
                  "description": "`mako_events::matches` patterns; empty matches every type."
                },
                "extensions": {
                  "type": "object",
                  "description": "CloudEvent extensions an event must carry with these values\n(`marktsparte`, `marktrole`).",
                  "additionalProperties": {
                    "type": "string"
                  },
                  "propertyNames": {
                    "type": "string"
                  }
                },
                "id": {
                  "type": "string",
                  "format": "uuid",
                  "description": "The endpoint's id."
                },
                "name": {
                  "type": "string",
                  "description": "Unique per service; `default` is the config URL."
                },
                "owner": {
                  "$ref": "#/components/schemas/Owner",
                  "description": "`config` or `api`."
                },
                "partner": {
                  "type": [
                    "string",
                    "null"
                  ],
                  "description": "The Vertriebspartner the endpoint is bound to; it receives only events\nstamped with that partner."
                },
                "patterns_matching_nothing": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  },
                  "description": "The patterns of `event_types` that match no event type mako emits.\nThey deliver nothing; replace them."
                },
                "previous_secret_expires_at": {
                  "type": [
                    "string",
                    "null"
                  ],
                  "format": "date-time",
                  "description": "Until when the previous secret still signs, after a rotation."
                },
                "url": {
                  "type": "string",
                  "description": "Where deliveries are POSTed."
                },
                "version": {
                  "type": "integer",
                  "format": "int64",
                  "description": "The version `If-Match` names."
                }
              }
            },
            "description": "The rows of this page, in the list's order."
          },
          "next_cursor": {
            "type": [
              "string",
              "null"
            ],
            "description": "The cursor of the next page; absent on the last."
          },
          "total_estimate": {
            "oneOf": [
              {
                "type": "null"
              },
              {
                "$ref": "#/components/schemas/TotalEstimate",
                "description": "How many rows the whole list holds, where the list counts them."
              }
            ]
          }
        }
      },
      "Page_Value": {
        "type": "object",
        "description": "One page of a list.",
        "required": [
          "items"
        ],
        "properties": {
          "items": {
            "type": "array",
            "items": {},
            "description": "The rows of this page, in the list's order."
          },
          "next_cursor": {
            "type": [
              "string",
              "null"
            ],
            "description": "The cursor of the next page; absent on the last."
          },
          "total_estimate": {
            "oneOf": [
              {
                "type": "null"
              },
              {
                "$ref": "#/components/schemas/TotalEstimate",
                "description": "How many rows the whole list holds, where the list counts them."
              }
            ]
          }
        }
      },
      "Problem": {
        "type": "object",
        "description": "The body every billingd refusal answers with.",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "$ref": "#/components/schemas/ProblemDetail",
            "description": "What was refused."
          }
        }
      },
      "ProblemDetail": {
        "type": "object",
        "description": "The refusal inside [`Problem`]. A conflict or an unprocessable request\ncarries further keys that name what decided it (`record_id`, `warnings`, …).",
        "required": [
          "code",
          "message"
        ],
        "properties": {
          "code": {
            "type": "string",
            "description": "A stable machine code (`PERIOD_ALREADY_BILLED`); branch on it."
          },
          "evidence": {
            "type": [
              "array",
              "null"
            ],
            "items": {
              "$ref": "#/components/schemas/ResellerEvidenceGap"
            },
            "description": "On RESELLER_EVIDENCE_REQUIRED, one finding for each affected party."
          },
          "message": {
            "type": "string",
            "description": "What went wrong, for the developer."
          },
          "service": {
            "type": [
              "string",
              "null"
            ],
            "description": "On a 502: the upstream service that did not answer."
          }
        }
      },
      "ReplayWindow": {
        "type": "object",
        "description": "The body of `POST /api/v1/webhooks/endpoints/{id}/replay`.",
        "required": [
          "from",
          "to"
        ],
        "properties": {
          "from": {
            "type": "string",
            "format": "date-time",
            "description": "Start of the window, inclusive (RFC 3339)."
          },
          "to": {
            "type": "string",
            "format": "date-time",
            "description": "End of the window, exclusive (RFC 3339)."
          }
        },
        "additionalProperties": false
      },
      "Replayed": {
        "type": "object",
        "description": "How many deliveries a replay queued.",
        "required": [
          "queued"
        ],
        "properties": {
          "queued": {
            "type": "integer",
            "format": "int64",
            "description": "Deliveries queued again or for the first time."
          }
        }
      },
      "ResellerEvidenceGap": {
        "type": "object",
        "description": "A party whose reseller evidence does not cover the requested supply period.",
        "required": [
          "party",
          "reason"
        ],
        "properties": {
          "party": {
            "$ref": "#/components/schemas/ResellerParty"
          },
          "reason": {
            "$ref": "#/components/schemas/ResellerEvidenceReason"
          }
        }
      },
      "ResellerEvidenceReason": {
        "type": "string",
        "enum": [
          "missing",
          "expired",
          "outside_validity"
        ]
      },
      "ResellerParty": {
        "type": "string",
        "enum": [
          "issuer",
          "recipient"
        ]
      },
      "RotateSecret": {
        "type": "object",
        "description": "The body of `POST /api/v1/webhooks/endpoints/{id}/rotate-secret`.",
        "required": [
          "overlap_hours"
        ],
        "properties": {
          "overlap_hours": {
            "type": "integer",
            "format": "int32",
            "description": "How long the previous secret keeps signing, at most 168 hours.",
            "minimum": 0
          }
        },
        "additionalProperties": false
      },
      "SecretRotated": {
        "type": "object",
        "description": "A rotated secret — the only answer that shows it.",
        "required": [
          "secret",
          "previous_expires_at"
        ],
        "properties": {
          "previous_expires_at": {
            "type": "string",
            "format": "date-time",
            "description": "Until when the previous secret also signs."
          },
          "secret": {
            "type": "string",
            "description": "The new `whsec_` signing secret; shown once."
          }
        }
      },
      "SweepReceipt": {
        "type": "object",
        "description": "Receipt of a committed manual run. Its evidence is available in the billing watch.",
        "required": [
          "run_id"
        ],
        "properties": {
          "run_id": {
            "type": "string",
            "format": "uuid"
          }
        }
      },
      "SweepRequest": {
        "type": "object",
        "description": "The run uses the installation's clock and policy. Unknown options are refused.",
        "additionalProperties": false
      },
      "TestDelivery": {
        "type": "object",
        "description": "What a test delivery came to.",
        "required": [
          "delivered"
        ],
        "properties": {
          "delivered": {
            "type": "boolean",
            "description": "Whether the endpoint answered 2xx."
          },
          "error": {
            "type": [
              "string",
              "null"
            ],
            "description": "Why it was not delivered."
          },
          "latency_ms": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int32",
            "description": "How long it took, when it answered."
          },
          "status": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int32",
            "description": "The HTTP status, when it answered.",
            "minimum": 0
          }
        }
      },
      "TotalEstimate": {
        "type": "object",
        "description": "A count bounded at [`COUNT_CAP`]: a list over a million rows reports\n`{count: 10000, capped: true}` rather than counting them.",
        "required": [
          "count",
          "capped"
        ],
        "properties": {
          "capped": {
            "type": "boolean",
            "description": "`true` when there are more rows than `count`."
          },
          "count": {
            "type": "integer",
            "format": "int64",
            "description": "The rows counted, at most 10 000."
          }
        }
      },
      "UnbilledPeriodRow": {
        "type": "object",
        "description": "One recorded unbilled period, as the Desk's Heute and the MCP tool read it.",
        "required": [
          "malo_id",
          "lf_mp_id",
          "period_from",
          "period_to",
          "final_invoice",
          "reason_code",
          "due_on",
          "days_left",
          "attempts"
        ],
        "properties": {
          "attempts": {
            "type": "integer",
            "format": "int32"
          },
          "days_left": {
            "type": "integer",
            "format": "int32",
            "description": "Days from today (Europe/Berlin) to `due_on`; negative once overdue."
          },
          "due_on": {
            "type": "string",
            "format": "date",
            "description": "§ 40c Abs. 2 EnWG: the last day the invoice may reach the customer."
          },
          "final_invoice": {
            "type": "boolean",
            "description": "The period ends with the supply and is owed an Abschlussrechnung."
          },
          "lf_mp_id": {
            "type": "string"
          },
          "malo_id": {
            "type": "string"
          },
          "period_from": {
            "type": "string",
            "format": "date"
          },
          "period_to": {
            "type": "string",
            "format": "date"
          },
          "reason_code": {
            "type": "string",
            "description": "Stable refusal code — word it from the catalogue, never show it raw."
          },
          "settled_by": {
            "type": [
              "string",
              "null"
            ],
            "format": "uuid",
            "description": "The invoice that discharged the period; `None` while it is open."
          }
        }
      },
      "UnbilledPeriods": {
        "type": "object",
        "description": "`list_unbilled_periods`' answer.",
        "required": [
          "periods",
          "count",
          "saturated"
        ],
        "properties": {
          "count": {
            "type": "integer",
            "description": "How many periods `periods` holds.",
            "minimum": 0
          },
          "discovery": {
            "oneOf": [
              {
                "type": "null"
              },
              {
                "$ref": "#/components/schemas/Discovery"
              }
            ]
          },
          "periods": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/UnbilledPeriodRow"
            },
            "description": "The earliest `due_on` first."
          },
          "saturated": {
            "type": "boolean",
            "description": "At least `limit` periods matched: more may be waiting than were listed."
          }
        }
      },
      "VerifyTokenBody": {
        "type": "object",
        "description": "The body of [`post_public_nachrechnung`].",
        "required": [
          "token"
        ],
        "properties": {
          "token": {
            "type": "string",
            "description": "The verify token printed on the invoice."
          }
        },
        "additionalProperties": false
      },
      "VppBillingRequest": {
        "type": "object",
        "description": "Request body for `POST /api/v1/billing/vpp/{vpp_id}`.\n\n`vpp_id` is the operator-assigned virtual power plant identifier\n(typically the SR-ID of the `SteuerbareRessource` portfolio in `marktd`).",
        "required": [
          "lf_mp_id",
          "malo_id",
          "period_from",
          "period_to",
          "capacity_price_eur_per_kwh",
          "dispatch_events"
        ],
        "properties": {
          "capacity_price_eur_per_kwh": {
            "type": "string",
            "format": "decimal",
            "description": "Capacity price EUR/kWh (agreed in VPP contract or dynamic market price)."
          },
          "dispatch_events": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/VppDispatchEvent"
            },
            "description": "All confirmed dispatch events in the billing period."
          },
          "lf_mp_id": {
            "type": "string",
            "description": "Aggregator MP-ID — the party issuing the Gutschrift."
          },
          "malo_id": {
            "type": "string",
            "description": "MaLo-ID of the VPP aggregation point (or primary resource)."
          },
          "period_from": {
            "type": "string",
            "description": "Billing period start (`YYYY-MM-DD`)."
          },
          "period_to": {
            "type": "string",
            "description": "Billing period end (`YYYY-MM-DD`)."
          },
          "rechnungsnummer": {
            "type": [
              "string",
              "null"
            ],
            "description": "Override the settlement's number. Absent — the normal case — takes the\nnext number of the tenant's `VG` (Gutschrift) series."
          }
        },
        "additionalProperties": false
      },
      "VppDispatchEvent": {
        "type": "object",
        "description": "One confirmed dispatch event for VPP settlement billing.\n\nSource: WiM Steuerungsauftrag IFTSTA confirmation (PID 21039) or equivalent\nVPP aggregator dispatch confirmation.",
        "required": [
          "start_utc",
          "end_utc",
          "flexibility_kwh",
          "tx_id"
        ],
        "properties": {
          "end_utc": {
            "type": "string",
            "description": "UTC dispatch end — ISO-8601 e.g. `\"2026-01-15T10:15:00Z\"`."
          },
          "flexibility_kwh": {
            "type": "string",
            "format": "decimal",
            "description": "Actual flexibility delivered in kWh (positive = load reduction; negative = load increase)."
          },
          "process_id": {
            "type": [
              "string",
              "null"
            ],
            "description": "IFTSTA process UUID from makod (for §20 audit trail)."
          },
          "start_utc": {
            "type": "string",
            "description": "UTC dispatch start — ISO-8601 e.g. `\"2026-01-15T10:00:00Z\"`."
          },
          "tx_id": {
            "type": "string",
            "description": "Stable dispatch transaction ID shared with the auto-settlement webhook.",
            "pattern": "\\S"
          }
        },
        "additionalProperties": false
      }
    }
  },
  "tags": [
    {
      "name": "billing",
      "description": "Calculate, preview and correct invoices"
    },
    {
      "name": "records",
      "description": "Billing records"
    },
    {
      "name": "documents",
      "description": "Rendered documents and their issue"
    },
    {
      "name": "risk",
      "description": "The risk gate's review queue, release and review"
    },
    {
      "name": "deadlines",
      "description": "§ 40c Abs. 2 EnWG: unbilled periods and the per-Lieferant standing"
    },
    {
      "name": "nachrechnen",
      "description": "Rechnung nachrechnen: the recomputation and its verify token"
    },
    {
      "name": "vpp",
      "description": "§ 41e EnWG VPP dispatch settlement"
    },
    {
      "name": "webhooks",
      "description": "Webhook endpoints, deliveries, replay and the event feed"
    }
  ]
}
