<?php

namespace Database\Seeders;

use App\Domain\Blog\ContentFormat;
use App\Domain\Plugin\PluginManager;
use App\Models\Article;
use App\Models\Category;
use App\Models\Comment;
use App\Models\Link;
use App\Models\Stylevar;
use App\Models\Tag;
use App\Models\User;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;

class DemoBlogSeeder extends Seeder
{
    public function run(): void
    {
        Role::findOrCreate('admin');
        Role::findOrCreate('editor');
        Role::findOrCreate('member');

        $plugins = app(PluginManager::class);
        $plugins->syncDiscoveredPlugins();
        try {
            $plugins->enable('larablog/ai-comment-moderation');
        } catch (\Throwable) {
            //
        }

        try {
            $plugins->enable('larablog/payment');
            $plugins->enable('larablog/membership');
        } catch (\Throwable) {
            //
        }

        $admin = User::query()
            ->where('email', 'admin@larablog.test')
            ->orWhere('username', 'admin')
            ->first();

        if ($admin === null) {
            $admin = User::query()->create([
                'email' => 'admin@larablog.test',
                'name' => 'Admin',
                'username' => 'admin',
                'password' => 'password',
            ]);
        } else {
            $admin->forceFill([
                'email' => 'admin@larablog.test',
                'name' => 'Admin',
                'username' => 'admin',
                'password' => 'password',
            ])->save();
        }
        $admin->assignRole('admin');

        $category = Category::query()->updateOrCreate(
            ['id' => 1],
            [
                'name' => '随笔',
                'display_order' => 0,
                'articles_count' => 2,
                'description' => '随手记下的阅读与技术笔记。',
                'intro' => '这里收录日常随笔：产品、写作与站点搭建过程中的短文。',
                'keywords' => '随笔,博客,笔记',
            ]
        );

        $html = Article::query()->updateOrCreate(
            ['id' => 1],
            [
                'category_id' => $category->id,
                'user_id' => $admin->id,
                'title' => '欢迎使用 LaraBlog（HTML 样例）',
                'content' => '<p>这是一篇 <strong>HTML</strong> 正文，来自旧站风格。</p><p>你可以在后台切换主题、启停插件，并用 Markdown 继续写作。</p>',
                'content_format' => ContentFormat::HTML,
                'description' => 'HTML 格式演示',
                'published_at' => now()->subDay(),
                'visible' => true,
                'stick' => true,
            ]
        );

        $md = Article::query()->updateOrCreate(
            ['id' => 2],
            [
                'category_id' => $category->id,
                'user_id' => $admin->id,
                'title' => 'Markdown 才是未来',
                'content' => "# Hello Markdown\n\n这是 **Markdown** 正文。\n\n- 主题可切换\n- 插件可启停\n- SEO / llms.txt 已就绪\n",
                'content_format' => ContentFormat::MARKDOWN,
                'description' => 'Markdown 格式演示',
                'published_at' => now(),
                'visible' => true,
            ]
        );

        $tag = Tag::query()->updateOrCreate(['name' => 'larablog'], ['use_count' => 2]);
        $tag->articles()->syncWithoutDetaching([$html->id, $md->id]);

        Comment::query()->updateOrCreate(
            ['id' => 1],
            [
                'article_id' => $md->id,
                'author' => '访客',
                'content' => '看起来不错',
                'moderation_status' => Comment::STATUS_APPROVED,
                'published_at' => now(),
            ]
        );

        Link::query()->updateOrCreate(
            ['id' => 1],
            [
                'name' => 'Laravel',
                'url' => 'https://laravel.com',
                'note' => 'PHP 框架',
                'visible' => true,
            ]
        );

        Stylevar::query()->updateOrCreate(
            ['id' => 1],
            [
                'title' => 'sidebar_note',
                'value' => 'LaraBlog · 现代博客引擎',
                'visible' => true,
            ]
        );
    }
}
