<?php

/**
 * ITSWEBER CMS – Default Seed Data
 *
 * Inserts default settings, pages, menus, and homepage sections
 * during installation.
 */

use App\Core\Database;

function seedDefaultData(Database $db, array $siteConfig = [], string $language = 'de'): void
{
    $siteName = $siteConfig['site_name'] ?? ($language === 'en' ? 'My Website' : 'Meine Website');
    $siteTagline = $siteConfig['site_tagline'] ?? '';
    $contactEmail = $siteConfig['contact_email'] ?? '';

    $copyrightSuffix = $language === 'en' ? '. All rights reserved.' : '. Alle Rechte vorbehalten.';

    // Default settings
    $settings = [
        ['site_name', $siteName, 'general'],
        ['site_tagline', $siteTagline, 'general'],
        ['contact_email', $contactEmail, 'general'],
        ['contact_phone', '', 'general'],
        ['footer_text', '', 'general'],
        ['copyright', '© ' . date('Y') . ' ' . $siteName . $copyrightSuffix, 'general'],
        ['active_theme', $siteConfig['theme'] ?? 'default', 'branding'],
        ['logo', '', 'branding'],
        ['favicon', '', 'branding'],
        ['seo_title', $siteName, 'seo'],
        ['seo_description', '', 'seo'],
        ['social_facebook', '', 'social'],
        ['social_instagram', '', 'social'],
        ['social_linkedin', '', 'social'],
        ['social_x', '', 'social'],
        ['social_youtube', '', 'social'],
    ];

    foreach ($settings as [$key, $value, $group]) {
        $db->insert('iw_settings', [
            'setting_key'   => $key,
            'setting_value' => $value,
            'setting_group' => $group,
        ]);
    }

    // Language-dependent labels
    $isEn = ($language === 'en');

    // Default pages
    $db->insert('iw_pages', [
        'title'      => 'Home',
        'slug'       => 'home',
        'content'    => '',
        'template'   => 'home',
        'status'     => 'published',
        'sort_order' => 0,
        'is_homepage' => 1,
        'created_at' => date('Y-m-d H:i:s'),
        'updated_at' => date('Y-m-d H:i:s'),
    ]);

    $db->insert('iw_pages', [
        'title'      => $isEn ? 'About' : 'Über uns',
        'slug'       => $isEn ? 'about' : 'ueber-uns',
        'content'    => '',
        'template'   => 'page',
        'status'     => 'published',
        'sort_order' => 1,
        'is_homepage' => 0,
        'created_at' => date('Y-m-d H:i:s'),
        'updated_at' => date('Y-m-d H:i:s'),
    ]);

    $db->insert('iw_pages', [
        'title'      => $isEn ? 'Contact' : 'Kontakt',
        'slug'       => 'contact',
        'content'    => '',
        'template'   => 'contact',
        'status'     => 'published',
        'sort_order' => 2,
        'is_homepage' => 0,
        'created_at' => date('Y-m-d H:i:s'),
        'updated_at' => date('Y-m-d H:i:s'),
    ]);

    // Default menus
    $mainMenuId = $db->insert('iw_menus', [
        'location' => 'main',
        'name'     => $isEn ? 'Main Navigation' : 'Hauptnavigation',
    ]);

    $footerMenuId = $db->insert('iw_menus', [
        'location' => 'footer',
        'name'     => $isEn ? 'Footer Navigation' : 'Footer-Navigation',
    ]);

    // Main menu items
    $aboutUrl = $isEn ? '/about' : '/ueber-uns';
    $db->insert('iw_menu_items', ['menu_id' => $mainMenuId, 'label' => $isEn ? 'Home' : 'Startseite', 'url' => '/', 'sort_order' => 0, 'is_visible' => 1, 'target' => '_self']);
    $db->insert('iw_menu_items', ['menu_id' => $mainMenuId, 'label' => $isEn ? 'About' : 'Über uns', 'url' => $aboutUrl, 'sort_order' => 1, 'is_visible' => 1, 'target' => '_self']);
    $db->insert('iw_menu_items', ['menu_id' => $mainMenuId, 'label' => 'Blog', 'url' => '/blog', 'sort_order' => 2, 'is_visible' => 1, 'target' => '_self']);
    $db->insert('iw_menu_items', ['menu_id' => $mainMenuId, 'label' => $isEn ? 'Gallery' : 'Galerie', 'url' => '/gallery', 'sort_order' => 3, 'is_visible' => 1, 'target' => '_self']);
    $db->insert('iw_menu_items', ['menu_id' => $mainMenuId, 'label' => $isEn ? 'Contact' : 'Kontakt', 'url' => '/contact', 'sort_order' => 4, 'is_visible' => 1, 'target' => '_self']);

    // Footer menu items
    $privacyUrl = $isEn ? '/privacy' : '/datenschutz';
    $imprintUrl = $isEn ? '/imprint' : '/impressum';
    $db->insert('iw_menu_items', ['menu_id' => $footerMenuId, 'label' => $isEn ? 'Privacy Policy' : 'Datenschutz', 'url' => $privacyUrl, 'sort_order' => 0, 'is_visible' => 1, 'target' => '_self']);
    $db->insert('iw_menu_items', ['menu_id' => $footerMenuId, 'label' => $isEn ? 'Imprint' : 'Impressum', 'url' => $imprintUrl, 'sort_order' => 1, 'is_visible' => 1, 'target' => '_self']);

    // Default homepage sections (language-aware)
    $sections = [
        ['hero', 'Hero', json_encode([
            'title'    => $siteName,
            'subtitle' => $siteTagline,
            'image'    => '',
            'cta_text' => $isEn ? 'Learn More' : 'Mehr erfahren',
            'cta_url'  => $aboutUrl,
        ]), 1, 0],
        ['intro', $isEn ? 'Introduction' : 'Einleitung', json_encode([
            'title' => $isEn ? 'Welcome' : 'Willkommen',
            'text'  => '',
        ]), 1, 1],
        ['features', $isEn ? 'Features' : 'Leistungen', json_encode([
            'title' => $isEn ? 'What We Offer' : 'Unsere Leistungen',
            'items' => [],
        ]), 1, 2],
        ['gallery_preview', $isEn ? 'Gallery Preview' : 'Galerie-Vorschau', json_encode([
            'title' => $isEn ? 'Our Work' : 'Unsere Arbeiten',
            'count' => 6,
        ]), 0, 3],
        ['blog_preview', $isEn ? 'Blog Preview' : 'Blog-Vorschau', json_encode([
            'title' => $isEn ? 'Latest News' : 'Aktuelle Beiträge',
            'count' => 3,
        ]), 0, 4],
        ['events_preview', $isEn ? 'Upcoming Events' : 'Kommende Veranstaltungen', json_encode([
            'title'    => $isEn ? 'Upcoming Events' : 'Kommende Veranstaltungen',
            'count'    => 2,
            'cta_text' => $isEn ? 'View all events' : 'Alle Events ansehen',
            'cta_url'  => '/events',
        ]), 0, 5],
        ['cta', 'Call to Action', json_encode([
            'title'    => $isEn ? 'Get in Touch' : 'Kontakt aufnehmen',
            'text'     => '',
            'cta_text' => $isEn ? 'Contact Us' : 'Kontakt',
            'cta_url'  => '/contact',
        ]), 1, 6],
        // topic_cards and highlight_grid are inserted by migration
        // 012_add_new_homepage_sections (INSERT IGNORE) – omitted here to
        // prevent a duplicate-key error on fresh installs where the migration
        // always runs before the seed.
    ];

    foreach ($sections as [$key, $title, $content, $active, $order]) {
        $db->insert('iw_homepage_sections', [
            'section_key' => $key,
            'title'       => $title,
            'content'     => $content,
            'is_active'   => $active,
            'sort_order'  => $order,
            'updated_at'  => date('Y-m-d H:i:s'),
        ]);
    }
}
