<?php

use App\Database\Migration;

/**
 * Phase 6d – Add consent / cookie-management settings.
 *
 * All rows use INSERT IGNORE so:
 * – New installations get fully configured defaults.
 * – Existing installations keep their current state undisturbed.
 *
 * The consent module is disabled by default (consent_enabled = '0').
 * Admins activate it explicitly in Admin → Cookie-Einstellungen.
 */
class AddConsentSettings extends Migration
{
    public function up(): void
    {
        $rows = [
            // ── Core toggle ───────────────────────────────────────────────────
            ['consent_enabled',           '0',                'consent'],

            // ── Banner texts (German defaults) ────────────────────────────────
            ['consent_title',             'Datenschutz & Cookies',     'consent'],
            ['consent_text',
             'Diese Website verwendet Cookies und ähnliche Technologien. '
             . 'Technisch notwendige Cookies werden immer gesetzt. '
             . 'Für optionale Funktionen bitten wir um Ihre Einwilligung.',
             'consent'],

            // ── Button labels ─────────────────────────────────────────────────
            ['consent_accept_label',      'Alle akzeptieren',          'consent'],
            ['consent_reject_label',      'Nur notwendige',            'consent'],
            ['consent_settings_label',    'Einstellungen',             'consent'],
            ['consent_save_label',        'Auswahl speichern',         'consent'],

            // ── Category descriptions ─────────────────────────────────────────
            ['consent_cat_necessary_label',     'Technisch notwendig',   'consent'],
            ['consent_cat_necessary_desc',
             'Diese Cookies sind für den Betrieb der Website unbedingt erforderlich und können nicht deaktiviert werden.',
             'consent'],
            ['consent_cat_functional_label',    'Funktional',            'consent'],
            ['consent_cat_functional_desc',
             'Diese Cookies ermöglichen erweiterte Funktionen und Personalisierung.',
             'consent'],

            // ── Links & behaviour ─────────────────────────────────────────────
            ['consent_privacy_url',       '/datenschutz',              'consent'],
            ['consent_privacy_label',     'Datenschutzerklärung',      'consent'],
            ['consent_cookie_name',       'iw_consent',                'consent'],
            ['consent_cookie_days',       '365',                       'consent'],

            // ── Optional: show branding logo in banner ────────────────────────
            ['consent_show_logo',         '0',                         'consent'],

            // ── Footer reopen link ────────────────────────────────────────────
            ['consent_show_reopen',       '1',                         'consent'],
            ['consent_reopen_label',      'Cookie-Einstellungen',      'consent'],
        ];

        foreach ($rows as [$key, $value, $group]) {
            $this->db->query(
                "INSERT IGNORE INTO iw_settings (setting_key, setting_value, setting_group) VALUES (:k, :v, :g)",
                ['k' => $key, 'v' => $value, 'g' => $group]
            );
        }
    }

    public function down(): void
    {
        $this->db->query(
            "DELETE FROM iw_settings WHERE setting_group = 'consent'"
        );
    }
}
