-- Migration 107: Vendor-agnostische Felder fuer Banner-Consents
-- Erweitert compliance_banner_consents um Felder die fuer beliebige
-- Tracking-Anbieter (GA4, Matomo, HubSpot etc.) benoetigt werden.
-- Alle Felder nullable → backward-compatible.

-- Consent-Methode: wie hat der User entschieden
ALTER TABLE compliance_banner_consents
    ADD COLUMN IF NOT EXISTS consent_method TEXT;
    -- "accept_all" / "reject_all" / "custom_selection"

-- Banner-Version + Config-Hash zum Zeitpunkt des Consents (Art. 7(1) DSGVO)
ALTER TABLE compliance_banner_consents
    ADD COLUMN IF NOT EXISTS banner_version INTEGER;

ALTER TABLE compliance_banner_consents
    ADD COLUMN IF NOT EXISTS banner_config_hash TEXT;

-- Geo-Daten fuer EWR-Only-Logik
ALTER TABLE compliance_banner_consents
    ADD COLUMN IF NOT EXISTS geo_country TEXT;

ALTER TABLE compliance_banner_consents
    ADD COLUMN IF NOT EXISTS geo_region TEXT;

-- Geltungsbereich: "page" / "domain" / "cross-domain"
ALTER TABLE compliance_banner_consents
    ADD COLUMN IF NOT EXISTS consent_scope TEXT DEFAULT 'domain';

-- Tracking-Kontext
ALTER TABLE compliance_banner_consents
    ADD COLUMN IF NOT EXISTS page_url TEXT;

ALTER TABLE compliance_banner_consents
    ADD COLUMN IF NOT EXISTS referrer TEXT;

-- Device-Informationen (parsed aus User-Agent im Frontend)
ALTER TABLE compliance_banner_consents
    ADD COLUMN IF NOT EXISTS device_type TEXT;
    -- "mobile" / "desktop" / "tablet"

ALTER TABLE compliance_banner_consents
    ADD COLUMN IF NOT EXISTS browser TEXT;

ALTER TABLE compliance_banner_consents
    ADD COLUMN IF NOT EXISTS os TEXT;

ALTER TABLE compliance_banner_consents
    ADD COLUMN IF NOT EXISTS screen_resolution TEXT;

-- Session-basierte Zuordnung
ALTER TABLE compliance_banner_consents
    ADD COLUMN IF NOT EXISTS session_id TEXT;

-- Audit-Log ebenfalls erweitern (consent_method fuer Nachweis)
ALTER TABLE compliance_banner_consent_audit_log
    ADD COLUMN IF NOT EXISTS consent_method TEXT;

ALTER TABLE compliance_banner_consent_audit_log
    ADD COLUMN IF NOT EXISTS page_url TEXT;

-- Index auf consent_method fuer Reporting
CREATE INDEX IF NOT EXISTS idx_banner_consent_method
    ON compliance_banner_consents (consent_method)
    WHERE consent_method IS NOT NULL;

-- Index auf session_id fuer Session-Lookup
CREATE INDEX IF NOT EXISTS idx_banner_consent_session
    ON compliance_banner_consents (session_id)
    WHERE session_id IS NOT NULL;
