{
  "openapi": "3.1.0",
  "info": {
    "title": "King Connect API",
    "version": "1.0.0",
    "summary": "Auftraggeber-Integration für KING (Kingsworth Adcon).",
    "description": "Read-only v1 (Wasserstopp). Jeder Schlüssel ist an genau einen Auftraggeber gebunden und sieht ausschließlich dessen Daten. Authentifizierung per Bearer-Token (ka_live_…), das nur als SHA-256-Hash gespeichert wird. Versioniert unter /v1/ und abwärtskompatibel (nur additive Änderungen).",
    "contact": { "name": "King Connect", "url": "https://intern.kingsworthadcon.com/KAConnect.html" }
  },
  "servers": [
    { "url": "https://zwdwzbkdorqtyatgtjou.supabase.co/functions/v1/api", "description": "Produktiv (live)" }
  ],
  "security": [ { "bearerAuth": [] } ],
  "paths": {
    "/v1/ping": {
      "get": {
        "summary": "Verbindungstest",
        "operationId": "ping",
        "responses": {
          "200": {
            "description": "Verbunden",
            "content": { "application/json": { "example": { "ok": true, "message": "King Connect ist verbunden. Wasserstopp aktiv.", "version": "v1", "auftraggeber": { "id": "uuid", "name": "Northern Fiber Holding" }, "scopes": ["read"], "zeit": "2026-06-22T10:00:00Z" } } }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" }
        }
      }
    },
    "/v1/auftraege": {
      "get": {
        "summary": "Eigene Aufträge auflisten",
        "operationId": "listAuftraege",
        "parameters": [
          { "name": "status", "in": "query", "schema": { "type": "string" }, "description": "Filter auf KING-Status (z.B. akzeptiert)" },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "default": 50, "maximum": 200 } },
          { "name": "offset", "in": "query", "schema": { "type": "integer", "default": 0 } }
        ],
        "responses": {
          "200": {
            "description": "Liste",
            "content": { "application/json": { "example": { "version": "v1", "anzahl": 1, "limit": 50, "offset": 0, "daten": [ { "auftrag_nr": "AUF-2026-2358", "status": "akzeptiert", "tarif": "lünecom 300", "kunde": "Mustermann GmbH", "erfasst_am": "2026-06-18T10:32:19Z" } ] } } }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    },
    "/v1/auftraege/{nr}": {
      "get": {
        "summary": "Einzelauftrag per Auftragsnummer",
        "operationId": "getAuftrag",
        "parameters": [ { "name": "nr", "in": "path", "required": true, "schema": { "type": "string" }, "example": "AUF-2026-2358" } ],
        "responses": {
          "200": { "description": "Auftrag", "content": { "application/json": { "example": { "version": "v1", "auftrag": { "auftrag_nr": "AUF-2026-2358", "status": "akzeptiert", "tarif": "lünecom 300", "kunde": "Mustermann GmbH", "erfasst_am": "2026-06-18T10:32:19Z" } } } } },
          "404": { "$ref": "#/components/responses/NotFound" },
          "401": { "$ref": "#/components/responses/Unauthorized" }
        }
      }
    },
    "/v1/produkte": {
      "get": {
        "summary": "Tarif-/Produktverteilung der eigenen Aufträge",
        "operationId": "listProdukte",
        "responses": {
          "200": { "description": "Aggregiert", "content": { "application/json": { "example": { "version": "v1", "anzahl": 2, "daten": [ { "tarif": "lünecom 300", "anzahl": 2 }, { "tarif": "lünecom 200 MFH", "anzahl": 1 } ] } } } },
          "401": { "$ref": "#/components/responses/Unauthorized" }
        }
      }
    },
    "/v1/auftraege/{nr}/status": {
      "put": {
        "summary": "Status zurückmelden (Rückkanal, braucht write-Scope)",
        "operationId": "putStatus",
        "parameters": [ { "name": "nr", "in": "path", "required": true, "schema": { "type": "string" } } ],
        "requestBody": { "required": true, "content": { "application/json": { "example": { "status": "freigeschaltet", "notiz": "Anschluss aktiv ab 01.07.2026" } } } },
        "responses": {
          "200": { "description": "Aktualisiert", "content": { "application/json": { "example": { "version": "v1", "ok": true, "auftrag": { "auftrag_nr": "AUF-2026-2358", "ag_status": "freigeschaltet", "ag_status_text": "Anschluss aktiv ab 01.07.2026", "ag_status_ts": "2026-06-22T10:00:00Z" } } } } },
          "403": { "description": "Kein write-Scope" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "401": { "$ref": "#/components/responses/Unauthorized" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": { "type": "http", "scheme": "bearer", "bearerFormat": "ka_live_…", "description": "Schlüssel im Self-Service in King Connect erzeugen." }
    },
    "responses": {
      "Unauthorized": { "description": "Schlüssel fehlt/ungültig/widerrufen", "content": { "application/json": { "example": { "error": "unauthorized" } } } },
      "NotFound": { "description": "Nicht gefunden", "content": { "application/json": { "example": { "error": "not_found" } } } },
      "RateLimited": { "description": "Zu viele Anfragen", "content": { "application/json": { "example": { "error": "rate_limited" } } } }
    }
  }
}
