-- Migration 030: DSFA Vollständiges Schema — alle Felder für Sections 0–7
-- Fügt alle fehlenden Spalten zur compliance_dsfas Tabelle hinzu.
-- TypeScript-Typen in types.ts erwarten 60+ Felder; bisher waren nur 15 vorhanden.

-- Section 1: Verarbeitungsbeschreibung + Rechtsgrundlage
ALTER TABLE compliance.compliance_dsfas
  ADD COLUMN IF NOT EXISTS processing_description    TEXT,
  ADD COLUMN IF NOT EXISTS processing_purpose        TEXT,
  ADD COLUMN IF NOT EXISTS legal_basis               VARCHAR(500),
  ADD COLUMN IF NOT EXISTS legal_basis_details       TEXT;

-- Section 2: Notwendigkeit & Verhältnismäßigkeit
ALTER TABLE compliance.compliance_dsfas
  ADD COLUMN IF NOT EXISTS necessity_assessment      TEXT,
  ADD COLUMN IF NOT EXISTS proportionality_assessment TEXT,
  ADD COLUMN IF NOT EXISTS data_minimization         TEXT,
  ADD COLUMN IF NOT EXISTS alternatives_considered   TEXT,
  ADD COLUMN IF NOT EXISTS retention_justification   TEXT;

-- Section 3: KI-Flags + Risikobewertung
ALTER TABLE compliance.compliance_dsfas
  ADD COLUMN IF NOT EXISTS involves_ai               BOOLEAN DEFAULT FALSE,
  ADD COLUMN IF NOT EXISTS overall_risk_level        VARCHAR(50),
  ADD COLUMN IF NOT EXISTS risk_score                INTEGER DEFAULT 0;

-- Section 6: DSB & Aufsichtsbehörde
ALTER TABLE compliance.compliance_dsfas
  ADD COLUMN IF NOT EXISTS dpo_consulted             BOOLEAN DEFAULT FALSE,
  ADD COLUMN IF NOT EXISTS dpo_consulted_at          TIMESTAMPTZ,
  ADD COLUMN IF NOT EXISTS dpo_name                  VARCHAR(255),
  ADD COLUMN IF NOT EXISTS dpo_opinion               TEXT,
  ADD COLUMN IF NOT EXISTS dpo_approved              BOOLEAN,
  ADD COLUMN IF NOT EXISTS authority_consulted       BOOLEAN DEFAULT FALSE,
  ADD COLUMN IF NOT EXISTS authority_consulted_at    TIMESTAMPTZ,
  ADD COLUMN IF NOT EXISTS authority_reference       VARCHAR(255),
  ADD COLUMN IF NOT EXISTS authority_decision        TEXT;

-- Versionierung & Metadaten
ALTER TABLE compliance.compliance_dsfas
  ADD COLUMN IF NOT EXISTS version                   INTEGER DEFAULT 1,
  ADD COLUMN IF NOT EXISTS previous_version_id       UUID,
  ADD COLUMN IF NOT EXISTS conclusion                TEXT,
  ADD COLUMN IF NOT EXISTS federal_state             VARCHAR(100),
  ADD COLUMN IF NOT EXISTS authority_resource_id     VARCHAR(100),
  ADD COLUMN IF NOT EXISTS submitted_for_review_at   TIMESTAMPTZ,
  ADD COLUMN IF NOT EXISTS submitted_by              VARCHAR(255);

-- JSONB Arrays
ALTER TABLE compliance.compliance_dsfas
  ADD COLUMN IF NOT EXISTS data_subjects              JSONB DEFAULT '[]'::jsonb,
  ADD COLUMN IF NOT EXISTS affected_rights            JSONB DEFAULT '[]'::jsonb,
  ADD COLUMN IF NOT EXISTS triggered_rule_codes       JSONB DEFAULT '[]'::jsonb,
  ADD COLUMN IF NOT EXISTS ai_trigger_ids             JSONB DEFAULT '[]'::jsonb,
  ADD COLUMN IF NOT EXISTS wp248_criteria_met         JSONB DEFAULT '[]'::jsonb,
  ADD COLUMN IF NOT EXISTS art35_abs3_triggered       JSONB DEFAULT '[]'::jsonb,
  ADD COLUMN IF NOT EXISTS tom_references             JSONB DEFAULT '[]'::jsonb,
  ADD COLUMN IF NOT EXISTS risks                      JSONB DEFAULT '[]'::jsonb,
  ADD COLUMN IF NOT EXISTS mitigations                JSONB DEFAULT '[]'::jsonb,
  ADD COLUMN IF NOT EXISTS stakeholder_consultations  JSONB DEFAULT '[]'::jsonb,
  ADD COLUMN IF NOT EXISTS review_triggers            JSONB DEFAULT '[]'::jsonb,
  ADD COLUMN IF NOT EXISTS review_comments            JSONB DEFAULT '[]'::jsonb;

-- JSONB Objekte
ALTER TABLE compliance.compliance_dsfas
  ADD COLUMN IF NOT EXISTS threshold_analysis        JSONB,
  ADD COLUMN IF NOT EXISTS consultation_requirement  JSONB,
  ADD COLUMN IF NOT EXISTS review_schedule           JSONB,
  ADD COLUMN IF NOT EXISTS section_progress          JSONB DEFAULT '{}'::jsonb,
  ADD COLUMN IF NOT EXISTS metadata                  JSONB DEFAULT '{}'::jsonb;

-- Indizes für häufig gefilterte Spalten
CREATE INDEX IF NOT EXISTS idx_dsfas_federal_state ON compliance.compliance_dsfas(federal_state);
CREATE INDEX IF NOT EXISTS idx_dsfas_involves_ai   ON compliance.compliance_dsfas(involves_ai);
