<?php

namespace Database\Seeders;

use App\Models\BlogPost;
use App\Models\Media;
use Illuminate\Database\Seeder;

class BlogPostSeeder extends Seeder
{
    public function run(): void
    {
        $posts = [
            [
                'title' => 'How to audit a GA4 and GTM setup before scaling paid traffic',
                'slug' => 'how-to-audit-a-ga4-and-gtm-setup-before-scaling-paid-traffic',
                'badge' => 'Featured guide',
                'reading_time' => '7 min read',
                'excerpt' => 'A simple framework for checking events, conversions, tags, naming, and data quality before you make optimization decisions from broken reporting.',
                'content' => '<p>Start with the events that matter most to revenue and lead quality.</p><p>Then confirm that every conversion action is mapped consistently across GA4, Google Ads, and any supporting CRM or form tools.</p><p>Finally, review naming, triggers, and QA notes inside GTM so the setup stays maintainable as campaigns scale.</p>',
                'featured_image_media_id' => $this->mediaId('uploads/system/hero-analytics-dashboard.svg'),
                'sort_order' => 0,
                'is_featured' => true,
                'is_published' => true,
                'published_at' => now()->subDays(5),
                'seo_title' => 'How to Audit a GA4 and GTM Setup Before Scaling Paid Traffic',
                'seo_description' => 'Use this checklist to validate GA4 and GTM before scaling paid traffic and making decisions from bad data.',
            ],
            [
                'title' => 'What to validate after a fresh GA4 implementation',
                'slug' => 'what-to-validate-after-a-fresh-ga4-implementation',
                'badge' => 'GA4',
                'reading_time' => '5 min read',
                'excerpt' => 'A quick checklist for event naming, conversion setup, enhanced measurement, filters, and reporting sanity checks.',
                'content' => '<p>Validate the events first so your reports reflect the actions that actually matter.</p><p>Then confirm conversions, filters, and reporting views before anyone uses the data for campaign decisions.</p>',
                'featured_image_media_id' => $this->mediaId('uploads/media/v79FaD7gAYHhwhCVU9jXaW6Wyo19v4wpO9OFcmuQ.png'),
                'sort_order' => 1,
                'is_featured' => false,
                'is_published' => true,
                'published_at' => now()->subDays(4),
                'seo_title' => 'What to Validate After a Fresh GA4 Implementation',
                'seo_description' => 'A quick GA4 post-launch checklist for events, conversions, filters, and reporting sanity checks.',
            ],
            [
                'title' => 'How to keep your GTM container organized as campaigns grow',
                'slug' => 'how-to-keep-your-gtm-container-organized-as-campaigns-grow',
                'badge' => 'GTM',
                'reading_time' => '6 min read',
                'excerpt' => 'A practical structure for folders, tag naming, triggers, variables, and QA notes so scaling does not create chaos.',
                'content' => '<p>Use folders, naming conventions, and documentation from the beginning.</p><p>A clean GTM workspace makes audits, updates, and handoffs much easier as the account becomes more complex.</p>',
                'featured_image_media_id' => $this->mediaId('uploads/media/kiMpmVYtvcfuAtyLh02wRHiwsu2vLmTiV6kQpQYl.jpg'),
                'sort_order' => 2,
                'is_featured' => false,
                'is_published' => true,
                'published_at' => now()->subDays(3),
                'seo_title' => 'How to Keep Your GTM Container Organized as Campaigns Grow',
                'seo_description' => 'Structure your GTM container with folders, naming rules, and QA notes so it stays manageable as campaigns scale.',
            ],
            [
                'title' => 'What a useful Looker Studio dashboard should actually include',
                'slug' => 'what-a-useful-looker-studio-dashboard-should-actually-include',
                'badge' => 'Dashboards',
                'reading_time' => '4 min read',
                'excerpt' => 'Build reporting views that help teams act faster by focusing on trend context, conversion quality, and business-ready KPIs.',
                'content' => '<p>A useful dashboard should explain performance, not just display metrics.</p><p>Focus on KPIs, trend context, and the decisions each view is meant to support.</p>',
                'featured_image_media_id' => $this->mediaId('uploads/media/Y4Bi7JUJUcsDY20kUyTIBtVMZKoSRUljI2g5AXoN.png'),
                'sort_order' => 3,
                'is_featured' => false,
                'is_published' => true,
                'published_at' => now()->subDays(2),
                'seo_title' => 'What a Useful Looker Studio Dashboard Should Include',
                'seo_description' => 'Learn what makes a Looker Studio dashboard actionable for reporting, optimization, and stakeholder communication.',
            ],
            [
                'title' => 'When server-side tracking is worth the extra setup',
                'slug' => 'when-server-side-tracking-is-worth-the-extra-setup',
                'badge' => 'Server-Side',
                'reading_time' => '8 min read',
                'excerpt' => 'Learn when the extra implementation effort improves data quality, privacy compliance, and platform attribution enough to matter.',
                'content' => '<p>Server-side tracking is most useful when browser-side loss is affecting attribution and optimization.</p><p>The extra setup pays off when better data quality improves reporting confidence and ad platform feedback loops.</p>',
                'featured_image_media_id' => $this->mediaId('uploads/media/iJcm5ue8eXcbASnWFv0OMQgo4xkilmSfM3vT7Gd8.png'),
                'sort_order' => 4,
                'is_featured' => false,
                'is_published' => true,
                'published_at' => now()->subDay(),
                'seo_title' => 'When Server-Side Tracking Is Worth the Extra Setup',
                'seo_description' => 'See when server-side tracking improves attribution, privacy, and reporting enough to justify the added implementation work.',
            ],
        ];

        foreach ($posts as $post) {
            BlogPost::updateOrCreate(
                ['slug' => $post['slug']],
                $post
            );
        }
    }

    protected function mediaId(string $path): ?int
    {
        return Media::query()->where('file_path', $path)->value('id');
    }
}
