<?php

/**
 * Theme-specific seed: Editorial (Blog/Magazine)
 *
 * Creates magazine-style demo content with featured posts, multiple categories,
 * and a rich blog experience. Used during installation when the Editorial theme
 * is selected.
 */
function seedEditorialTheme(\App\Core\Database $db, string $language = 'de'): void
{
    $isDE = $language === 'de';

    // Additional blog categories
    $categories = [
        ['name' => $isDE ? 'Meinung' : 'Opinion', 'slug' => $isDE ? 'meinung' : 'opinion', 'description' => $isDE ? 'Kommentare und Einschaetzungen' : 'Commentary and perspectives', 'sort_order' => 2],
        ['name' => $isDE ? 'Kultur' : 'Culture', 'slug' => $isDE ? 'kultur' : 'culture', 'description' => $isDE ? 'Kunst, Musik und Gesellschaft' : 'Art, music and society', 'sort_order' => 3],
        ['name' => $isDE ? 'Technologie' : 'Technology', 'slug' => $isDE ? 'technologie' : 'technology', 'description' => $isDE ? 'Digitale Trends und Innovation' : 'Digital trends and innovation', 'sort_order' => 4],
    ];

    foreach ($categories as $cat) {
        $exists = $db->fetch("SELECT id FROM iw_blog_categories WHERE slug = :slug LIMIT 1", ['slug' => $cat['slug']]);
        if (!$exists) {
            $db->insert('iw_blog_categories', $cat);
        }
    }

    // Featured blog posts
    $opinionCat = $db->fetch("SELECT id FROM iw_blog_categories WHERE slug = :slug LIMIT 1", ['slug' => $isDE ? 'meinung' : 'opinion']);
    $cultureCat = $db->fetch("SELECT id FROM iw_blog_categories WHERE slug = :slug LIMIT 1", ['slug' => $isDE ? 'kultur' : 'culture']);
    $techCat    = $db->fetch("SELECT id FROM iw_blog_categories WHERE slug = :slug LIMIT 1", ['slug' => $isDE ? 'technologie' : 'technology']);

    $posts = [
        [
            'title'       => $isDE ? 'Die Zukunft des digitalen Journalismus' : 'The Future of Digital Journalism',
            'slug'        => $isDE ? 'zukunft-digitaler-journalismus' : 'future-of-digital-journalism',
            'excerpt'     => $isDE ? 'Wie sich Medien im digitalen Zeitalter neu erfinden muessen.' : 'How media must reinvent itself in the digital age.',
            'content'     => $isDE ? '<p>Der digitale Wandel hat die Medienlandschaft grundlegend veraendert. Traditionelle Geschaeftsmodelle funktionieren nicht mehr, und Redaktionen muessen neue Wege finden, um qualitativ hochwertigen Journalismus zu finanzieren.</p><p>In diesem Beitrag beleuchten wir die wichtigsten Trends und Herausforderungen.</p>' : '<p>The digital transformation has fundamentally changed the media landscape. Traditional business models no longer work, and newsrooms must find new ways to fund quality journalism.</p><p>In this article, we explore the key trends and challenges ahead.</p>',
            'status'      => 'published',
            'is_featured' => 1,
            'category_id' => $opinionCat['id'] ?? null,
            'published_at' => date('Y-m-d H:i:s'),
        ],
        [
            'title'       => $isDE ? 'Kunst im oeffentlichen Raum: Mehr als Dekoration' : 'Public Art: More Than Decoration',
            'slug'        => $isDE ? 'kunst-im-oeffentlichen-raum' : 'public-art-more-than-decoration',
            'excerpt'     => $isDE ? 'Warum Staedte in Kunst investieren sollten.' : 'Why cities should invest in art.',
            'content'     => $isDE ? '<p>Kunst im oeffentlichen Raum praegt das Gesicht einer Stadt und schafft Identitaet. Von Skulpturen bis zu Wandmalereien – oeffentliche Kunst regt zum Nachdenken an und macht Kultur fuer alle zugaenglich.</p>' : '<p>Public art shapes a city\'s identity and creates belonging. From sculptures to murals, public art provokes thought and makes culture accessible to everyone.</p>',
            'status'      => 'published',
            'is_featured' => 1,
            'category_id' => $cultureCat['id'] ?? null,
            'published_at' => date('Y-m-d H:i:s', strtotime('-2 days')),
        ],
        [
            'title'       => $isDE ? 'KI und Kreativitaet: Partnerschaft oder Konkurrenz?' : 'AI and Creativity: Partnership or Competition?',
            'slug'        => $isDE ? 'ki-und-kreativitaet' : 'ai-and-creativity',
            'excerpt'     => $isDE ? 'Kuenstliche Intelligenz veraendert kreative Berufe.' : 'Artificial intelligence is transforming creative professions.',
            'content'     => $isDE ? '<p>Generative KI-Systeme koennen Texte schreiben, Bilder erstellen und Musik komponieren. Doch ersetzt die Maschine den Menschen – oder erweitert sie seine Moeglichkeiten?</p>' : '<p>Generative AI systems can write texts, create images, and compose music. But does the machine replace humans – or expand their possibilities?</p>',
            'status'      => 'published',
            'is_featured' => 0,
            'category_id' => $techCat['id'] ?? null,
            'published_at' => date('Y-m-d H:i:s', strtotime('-5 days')),
        ],
        [
            'title'       => $isDE ? 'Minimalismus im Webdesign: Weniger ist mehr' : 'Minimalism in Web Design: Less Is More',
            'slug'        => $isDE ? 'minimalismus-im-webdesign' : 'minimalism-in-web-design',
            'excerpt'     => $isDE ? 'Wie reduziertes Design die Nutzererfahrung verbessert.' : 'How reduced design improves user experience.',
            'content'     => $isDE ? '<p>Minimalismus im Webdesign bedeutet nicht Leere – sondern bewusste Reduktion auf das Wesentliche. Gute Typografie, grosszuegige Whitespace und klare Hierarchien schaffen ein angenehmes Leseerlebnis.</p>' : '<p>Minimalism in web design doesn\'t mean emptiness – it means deliberate reduction to the essentials. Good typography, generous whitespace, and clear hierarchies create a pleasant reading experience.</p>',
            'status'      => 'published',
            'is_featured' => 0,
            'category_id' => $techCat['id'] ?? null,
            'published_at' => date('Y-m-d H:i:s', strtotime('-8 days')),
        ],
    ];

    $adminUser = $db->fetch("SELECT id FROM iw_users LIMIT 1");
    $authorId  = $adminUser['id'] ?? 1;

    foreach ($posts as $post) {
        $exists = $db->fetch("SELECT id FROM iw_blog_posts WHERE slug = :slug LIMIT 1", ['slug' => $post['slug']]);
        if (!$exists) {
            $post['author_id']  = $authorId;
            $post['created_at'] = $post['published_at'];
            $post['updated_at'] = $post['published_at'];
            $db->insert('iw_blog_posts', $post);
        }
    }

    // Gallery albums for editorial
    $albums = [
        ['title' => $isDE ? 'Fotoreportage: Stadtleben' : 'Photo Report: City Life', 'slug' => $isDE ? 'fotoreportage-stadtleben' : 'photo-report-city-life', 'description' => $isDE ? 'Impressionen aus dem urbanen Alltag' : 'Impressions from urban everyday life', 'is_visible' => 1],
        ['title' => $isDE ? 'Redaktionsalltag' : 'Newsroom Life', 'slug' => $isDE ? 'redaktionsalltag' : 'newsroom-life', 'description' => $isDE ? 'Hinter den Kulissen unserer Redaktion' : 'Behind the scenes of our newsroom', 'is_visible' => 1],
    ];

    foreach ($albums as $album) {
        $exists = $db->fetch("SELECT id FROM iw_gallery_albums WHERE slug = :slug LIMIT 1", ['slug' => $album['slug']]);
        if (!$exists) {
            $album['created_at'] = date('Y-m-d H:i:s');
            $db->insert('iw_gallery_albums', $album);
        }
    }

    // ── Homepage Sections: Override starter content with Editorial-themed data ──

    // Hero: Magazine-style, typography-focused
    $heroContent = json_encode([
        'title'          => $isDE ? 'Geschichten, die bleiben' : 'Stories That Last',
        'eyebrow'        => $isDE ? 'Das Magazin' : 'The Magazine',
        'subtitle'       => $isDE
            ? 'Entdecken Sie tiefgruendige Reportagen, scharfe Kommentare und frische Perspektiven auf Kultur, Technologie und Gesellschaft.'
            : 'Discover in-depth reports, sharp commentary, and fresh perspectives on culture, technology, and society.',
        'image'          => '',
        'portrait_image' => '',
        'portrait_alt'   => '',
        'text_color'     => 'dark',
        'heading_size'   => 'xl',
        'heading_weight' => 'bold',
        'highlight_word' => $isDE ? 'bleiben' : 'Last',
        'cta_text'       => $isDE ? 'Aktuelle Ausgabe' : 'Current Issue',
        'cta_url'        => '/blog',
        'cta_bg'         => '#e63946',
        'cta_color'      => '#ffffff',
        'cta2_text'      => $isDE ? 'Redaktion kontaktieren' : 'Contact the Editors',
        'cta2_url'       => '/contact',
    ], JSON_UNESCAPED_UNICODE);

    $db->query(
        "UPDATE iw_page_sections SET content = :c, is_active = 1 WHERE section_type = 'hero' AND page_context = 'homepage'",
        ['c' => $heroContent]
    );

    // Intro: Editorial voice
    $introContent = json_encode([
        'title'              => $isDE ? 'Unabhaengig. Kritisch. Relevant.' : 'Independent. Critical. Relevant.',
        'text'               => $isDE
            ? 'Seit unserer Gruendung stehen wir fuer Qualitaetsjournalismus, der Orientierung bietet und Debatten anstosst. Unsere Autorinnen und Autoren bringen Expertise aus den verschiedensten Bereichen mit.'
            : 'Since our founding, we stand for quality journalism that provides orientation and sparks debate. Our writers bring expertise from a wide range of fields.',
        'color_scheme'       => 'alt',
    ], JSON_UNESCAPED_UNICODE);

    $db->query(
        "UPDATE iw_page_sections SET content = :c, is_active = 1 WHERE section_type = 'intro' AND page_context = 'homepage'",
        ['c' => $introContent]
    );

    // Topic Cards: Magazine sections
    $topicContent = json_encode([
        'title' => $isDE ? 'Unsere Rubriken' : 'Our Sections',
        'items' => $isDE ? [
            ['icon' => 'pen-tool', 'title' => 'Meinung', 'text' => 'Kommentare, Leitartikel und Gastbeitraege zu aktuellen Themen.', 'url' => '/blog'],
            ['icon' => 'palette', 'title' => 'Kultur', 'text' => 'Berichte aus der Welt der Kunst, Musik, Film und Literatur.', 'url' => '/blog'],
            ['icon' => 'smartphone', 'title' => 'Technologie', 'text' => 'Digitale Trends, Innovation und ihre Auswirkungen auf die Gesellschaft.', 'url' => '/blog'],
        ] : [
            ['icon' => 'pen-tool', 'title' => 'Opinion', 'text' => 'Commentary, editorials and guest contributions on current topics.', 'url' => '/blog'],
            ['icon' => 'palette', 'title' => 'Culture', 'text' => 'Reports from the world of art, music, film and literature.', 'url' => '/blog'],
            ['icon' => 'smartphone', 'title' => 'Technology', 'text' => 'Digital trends, innovation and their impact on society.', 'url' => '/blog'],
        ],
        'color_scheme'       => 'none',
        'card_style'         => 'bordered',
    ], JSON_UNESCAPED_UNICODE);

    $db->query(
        "UPDATE iw_page_sections SET content = :c, is_active = 1 WHERE section_type = 'topic_cards' AND page_context = 'homepage'",
        ['c' => $topicContent]
    );

    // Features: Editorial strengths
    $featuresContent = json_encode([
        'title' => $isDE ? 'Was uns ausmacht' : 'What Sets Us Apart',
        'items' => $isDE ? [
            ['title' => 'Investigative Recherche', 'text' => 'Gruendliche Recherche und Faktencheck bilden die Grundlage jeder Geschichte.'],
            ['title' => 'Vielfalt der Stimmen', 'text' => 'Wir geben unterschiedlichen Perspektiven Raum – fuer einen differenzierten Blick auf die Welt.'],
            ['title' => 'Lesernaehe', 'text' => 'Unsere Community gestaltet aktiv mit: durch Kommentare, Leserbriefe und Veranstaltungen.'],
        ] : [
            ['title' => 'Investigative Research', 'text' => 'Thorough research and fact-checking form the foundation of every story.'],
            ['title' => 'Diversity of Voices', 'text' => 'We give space to different perspectives for a nuanced view of the world.'],
            ['title' => 'Reader Engagement', 'text' => 'Our community actively participates through comments, letters, and events.'],
        ],
        'color_scheme' => 'alt',
        'card_style'   => 'flat',
    ], JSON_UNESCAPED_UNICODE);

    $db->query(
        "UPDATE iw_page_sections SET content = :c, is_active = 1 WHERE section_type = 'features' AND page_context = 'homepage'",
        ['c' => $featuresContent]
    );

    // Blog Preview
    $blogContent = json_encode([
        'title'    => $isDE ? 'Neueste Beitraege' : 'Latest Articles',
        'count'    => 3,
        'cta_text' => $isDE ? 'Zum Archiv' : 'View Archive',
        'cta_url'  => '/blog',
    ], JSON_UNESCAPED_UNICODE);

    $db->query(
        "UPDATE iw_page_sections SET content = :c, is_active = 1 WHERE section_type = 'blog_preview' AND page_context = 'homepage'",
        ['c' => $blogContent]
    );

    // Events Preview
    $eventsContent = json_encode([
        'title'    => $isDE ? 'Veranstaltungen & Lesungen' : 'Events & Readings',
        'count'    => 3,
        'cta_text' => $isDE ? 'Alle Termine' : 'All Events',
        'cta_url'  => '/events',
    ], JSON_UNESCAPED_UNICODE);

    $db->query(
        "UPDATE iw_page_sections SET content = :c, is_active = 1 WHERE section_type = 'events_preview' AND page_context = 'homepage'",
        ['c' => $eventsContent]
    );

    // CTA: Subscribe / Contact
    $ctaContent = json_encode([
        'title'        => $isDE ? 'Bleiben Sie informiert' : 'Stay Informed',
        'text'         => $isDE
            ? 'Abonnieren Sie unseren Newsletter und verpassen Sie keine wichtige Geschichte mehr. Oder nehmen Sie direkt Kontakt mit unserer Redaktion auf.'
            : 'Subscribe to our newsletter and never miss an important story. Or get in touch with our editorial team directly.',
        'cta_text'     => $isDE ? 'Kontakt aufnehmen' : 'Get in Touch',
        'cta_url'      => '/contact',
        'color_scheme' => 'primary',
        'section_text_color' => 'white',
    ], JSON_UNESCAPED_UNICODE);

    $db->query(
        "UPDATE iw_page_sections SET content = :c, is_active = 1 WHERE section_type = 'cta' AND page_context = 'homepage'",
        ['c' => $ctaContent]
    );
}
