<?php

namespace Database\Seeders;

use App\Models\Blog;
use App\Models\Category;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class BlogSeeder extends Seeder
{
    public function run(): void
    {
        $jane = User::where('username', 'jane')->first();
        if (!$jane) return;

        $programming = Category::where('slug', 'programming')->first();
        $llm = Category::where('slug', 'large-language-models')->first();
        $travel = Category::where('slug', 'travel')->first();
        $food = Category::where('slug', 'food')->first();

        $samples = [
            [
                'title'    => 'The Quiet Revolution in Modern Web Development',
                'category' => $programming,
                'excerpt'  => 'How edge functions, signals, and view transitions are quietly reshaping how we ship interfaces.',
                'tags'     => ['javascript', 'web', 'frontend'],
                'language' => 'en',
                'content'  => <<<HTML
<p>For most of the last decade, web development has been dominated by a familiar trio: a framework, a bundler, and a server. The conversation revolved around which framework, which bundler, and which server. But underneath that surface, a quieter shift has been happening.</p>
<h2>Edge functions changed the deployment model</h2>
<p>Code now runs at the boundary of the network rather than in a single region. Latency drops from hundreds of milliseconds to single digits. The implications go beyond performance — entire categories of caching workarounds become unnecessary.</p>
<h2>Signals are replacing virtual DOM diffing</h2>
<p>Fine-grained reactivity, long the preserve of frameworks like Knockout and later Vue, is now appearing in React-like contexts. The performance characteristics differ in ways that matter for real applications.</p>
<h2>View transitions arrived without much noise</h2>
<p>Native cross-document and same-document view transitions ship in browsers today. Effects that previously required a full SPA framework are now achievable with a few CSS lines and a small bit of script.</p>
<p>None of these are revolutions in isolation. Together, they redraw the map.</p>
HTML
            ],
            [
                'title'    => 'Why LLM Output Quality Depends More on Prompts Than People Admit',
                'category' => $llm,
                'excerpt'  => 'A practitioner\'s view on why the same model gives wildly different results depending on input shape.',
                'tags'     => ['ai', 'llm', 'prompting'],
                'language' => 'en',
                'content'  => <<<HTML
<p>Talk to anyone shipping LLM-powered features and the same observation comes up: identical models produce dramatically different output quality depending on how the request is framed.</p>
<h2>Structure matters more than verbosity</h2>
<p>A short, well-structured prompt with explicit roles and constraints will outperform a long, rambling one nine times out of ten. The model is responding to the latent structure of your input.</p>
<h2>Examples beat instructions</h2>
<p>Telling the model what you want is fine. Showing it two or three examples of acceptable outputs is dramatically better.</p>
<h2>Context window discipline pays off</h2>
<p>Stuffing the context window with everything you have access to feels safe but introduces noise. The signal-to-noise ratio in your prompt is one of the strongest predictors of output quality.</p>
HTML
            ],
            [
                'title'    => 'A Slow Morning in Lisbon: A Walking Diary',
                'category' => $travel,
                'excerpt'  => 'Trams, tile, and the kind of light that only happens at this latitude.',
                'tags'     => ['travel', 'portugal', 'lisbon'],
                'language' => 'en',
                'content'  => <<<HTML
<p>Lisbon at 8 a.m. is a different city from the one in the guidebooks. The trams have just begun their first runs, the bakeries are pulling their first trays of pastéis from the oven, and the light angles in low across the river.</p>
<p>I started in Alfama, climbing the cobbled lanes that locals navigate with practiced indifference. The azulejos pick up the morning sun in a way that feels almost calculated.</p>
<h2>Coffee and a pause</h2>
<p>I stopped at a small café whose name I never learned. Three regulars at the counter, espresso served without ceremony.</p>
<h2>The view from Graça</h2>
<p>By 10 a.m. I was at the miradouro in Graça, looking back across the rooftops to the castle. There is a particular shade of terracotta that happens nowhere else.</p>
HTML
            ],
            [
                'title'    => 'On the Quiet Genius of a Well-Made Bowl of Rice',
                'category' => $food,
                'excerpt'  => 'Why the simplest dishes are often the hardest to get right.',
                'tags'     => ['food', 'cooking', 'japanese'],
                'language' => 'en',
                'content'  => <<<HTML
<p>Rice, water, time. Three ingredients, if you count time. And yet a perfectly cooked bowl of short-grain Japanese rice — gohan — is one of the harder things to produce consistently in a home kitchen.</p>
<h2>The water matters</h2>
<p>The rice should be rinsed until the water runs nearly clear. Three or four changes, gentle hands.</p>
<h2>Resting is not optional</h2>
<p>After cooking, the lid stays on for ten minutes. This is non-negotiable. The grains finish absorbing what's left and redistribute their moisture.</p>
<h2>What it teaches</h2>
<p>The dishes we treat as background reward attention disproportionately. Mastering them is not glamorous, but it is the foundation everything else rests on.</p>
HTML
            ],
            [
                'title'    => 'Building Habits That Survive Bad Weeks',
                'category' => null,
                'excerpt'  => 'On designing routines that bend without breaking.',
                'tags'     => ['productivity', 'habits'],
                'language' => 'en',
                'content'  => <<<HTML
<p>Most habit advice is built around an assumption that does not hold: that the days you most want to maintain the habit will look like the days you designed it for.</p>
<h2>The two-version rule</h2>
<p>For any habit worth keeping, define two versions: the full version and a five-minute version. On a good day, you do the full version. On a terrible day, you do the five-minute one.</p>
<h2>Floors before ceilings</h2>
<p>Most failures come from setting a ceiling that's too high rather than a floor that's too low. A daily walk of any length beats a 5K run that only happens twice a week, then never.</p>
HTML
            ],
            [
                'title'    => 'الكتابة بلغتك الأم: لماذا تستحق المحاولة',
                'category' => null,
                'excerpt'  => 'فكرة قصيرة عن قيمة النشر بلغة الأم في عالم رقمي.',
                'tags'     => ['writing', 'arabic'],
                'language' => 'ar',
                'content'  => <<<HTML
<p dir="rtl">كثيرون يكتبون بالإنجليزية بحثاً عن جمهور أوسع، لكن النشر بلغتك الأم له قيمة لا تُعوّض. اللغة التي تفكر بها هي اللغة التي ستكتب بها أصدق ما عندك.</p>
<h2 dir="rtl">الأصالة قبل الانتشار</h2>
<p dir="rtl">القارئ يلاحظ الفرق بين كاتب يترجم أفكاره وكاتب يكتب مباشرة بما يشعر به. الأصالة تسبق الانتشار وتدوم بعده.</p>
<h2 dir="rtl">منصة لكل اللغات</h2>
<p dir="rtl">هذه المنصة تدعم النشر بأي لغة. اكتب كما تتحدث.</p>
HTML
            ],
        ];

        foreach ($samples as $i => $s) {
            Blog::create([
                'user_id'      => $jane->id,
                'category_id'  => $s['category']?->id,
                'title'        => $s['title'],
                'slug'         => Str::slug($s['title']) . '-' . Str::random(5),
                'excerpt'      => $s['excerpt'],
                'content'      => $s['content'],
                'language'     => $s['language'],
                'tags'         => $s['tags'],
                'status'       => 'published',
                'published_at' => now()->subDays(6 - $i),
                'views'        => rand(50, 800),
            ]);
            $jane->increment('blogs_used');
        }
    }
}
