{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "kadi-v2/schemas/qaf-value-stream-import.schema.json",
  "title": "ValueStreamImport",
  "description": "Audit-/Herkunfts-Record eines QAF→Wertstrom-Imports (Tabelle value_stream_imports, QVS-P2). Ein Record je Import-Vorgang; 1 Wertstrom kann über die Zeit mehrere Records tragen (Re-Import-Historie).",
  "type": "object",
  "required": [
    "id",
    "value_stream_id",
    "parser_version",
    "mapping_version",
    "imported_step_count",
    "excluded_step_count",
    "created_at"
  ],
  "properties": {
    "id": { "type": "string", "format": "uuid" },
    "value_stream_id": { "type": "string", "format": "uuid" },
    "qaf_file_id": {
      "type": ["string", "null"],
      "format": "uuid",
      "description": "FK auf qaf_file; NULL nach Löschung der Quelle (ON DELETE SET NULL) — Record bleibt als Audit erhalten"
    },
    "project_id": {
      "type": ["string", "null"],
      "format": "uuid",
      "description": "FK auf projects; NULL nach Löschung des Projekts (ON DELETE SET NULL, Review-Fix 1) — Record bleibt als Audit erhalten, Sichtbarkeit fällt auf created_by zurück"
    },
    "source_file_name": { "type": ["string", "null"] },
    "file_hash": {
      "type": ["string", "null"],
      "description": "SHA-256 des Quell-Workbooks (aus qaf_file.file_hash) — Basis des Duplikat-Checks"
    },
    "variant_selector": {
      "type": ["object", "null"],
      "description": "Bei Multi-QAF-Quelle: gewählte Strategie + Varianten",
      "properties": {
        "strategy": { "enum": ["shared_flow", "per_variant"] },
        "variantKeys": { "type": "array", "items": { "type": "string" } }
      }
    },
    "parser_version": { "type": "string" },
    "mapping_version": { "type": "string", "description": "z. B. qvs-1 (mappings/qaf-to-value-stream-mapping.yaml)" },
    "engine_context": {
      "type": ["object", "null"],
      "description": "Engine-/Config-Kontext zum Import-Zeitpunkt (configVersion, parseConfidence, sequenceConfidence, …). Hinweis: die DB-Spalte ist bewusst strenger (NOT NULL DEFAULT '{}') als dieses Schema — dokumentiert in der Migration, folgenlos für Consumer.",
      "properties": {
        "reimportHistory": {
          "type": "array",
          "description": "QVS-P5 (KAR-974): Audit-Verlauf über Reimport-/Sync-Vorgänge — der Import-Record wird bei jedem Reimport fortgeschrieben (UPDATE, keine neue Zeile; siehe ADR-024 'N Imports über die Zeit' — hier bewusst als Array-in-einer-Zeile statt N Zeilen modelliert, weil P5 keine neue Migration eröffnet und ein reines UPDATE ausreicht). Älteste Einträge fallen ab 20 Einträgen weg (reimport.ts MAX_REIMPORT_HISTORY_ENTRIES).",
          "items": {
            "type": "object",
            "required": ["appliedAt", "sourceQafFileId", "summary"],
            "properties": {
              "appliedAt": { "type": "string", "format": "date-time" },
              "sourceQafFileId": { "type": "string", "format": "uuid" },
              "sourceFileHash": { "type": ["string", "null"] },
              "summary": {
                "type": "object",
                "description": "QvsSyncApplySummary (lib/qaf-value-stream/internal/types.ts) — fieldsAdopted/stepsAdded/stepsRemoved/conflictsResolvedTakeSource/conflictsResolvedKeepLocal/fieldsKeptLocal."
              }
            }
          }
        }
      }
    },
    "imported_step_count": { "type": "integer", "minimum": 0 },
    "excluded_step_count": { "type": "integer", "minimum": 0 },
    "warnings": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["code", "severity", "message"],
        "properties": {
          "code": { "type": "string" },
          "severity": { "enum": ["information", "warning", "blocking_error"] },
          "message": { "type": "string" },
          "stepRef": { "type": ["string", "null"] }
        }
      }
    },
    "missing_field_reasons": {
      "type": "object",
      "description": "Je Feld-Key: Grund + Zählwert, warum Werte fehlen",
      "additionalProperties": {
        "type": "object",
        "required": ["reason", "count"],
        "properties": {
          "reason": {
            "enum": ["no_column_mapped", "cell_empty", "not_numeric", "below_confidence", "not_in_source_model"]
          },
          "count": { "type": "integer", "minimum": 0 },
          "rawTextSamples": { "type": "array", "items": { "type": "string" }, "maxItems": 5 }
        }
      }
    },
    "import_snapshot": {
      "type": ["object", "null"],
      "description": "Nodes+Connections exakt wie erzeugt — Referenz für Sync-Delta (P5) und Nachvollziehbarkeit",
      "properties": {
        "nodes": { "type": "array" },
        "connections": { "type": "array" }
      }
    },
    "manual_preview_corrections": {
      "type": ["array", "null"],
      "description": "Vom Nutzer im Preview vorgenommene Korrekturen (abgewählte Schritte, Titel-Änderung, Namens-Korrekturen)",
      "items": {
        "type": "object",
        "required": ["kind"],
        "properties": {
          "kind": { "enum": ["step_deselected", "title_changed", "name_corrected", "order_changed"] },
          "stepRef": { "type": ["string", "null"] },
          "from": {},
          "to": {}
        }
      }
    },
    "created_by": { "type": ["string", "null"], "format": "uuid" },
    "created_at": { "type": "string", "format": "date-time" }
  }
}
