<?php

namespace Database\Seeders;

use App\Models\BlogCategory;
use App\Models\BlogPost;
use App\Models\BlogTag;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class BlogSeeder extends Seeder
{
    public function run()
    {
        // Create categories
        $categories = [
            ['name' => 'Web Development', 'color' => '#10B981'],
            ['name' => 'Digital Marketing', 'color' => '#3B82F6'],
            ['name' => 'Business Strategy', 'color' => '#8B5CF6'],
            ['name' => 'SEO', 'color' => '#F59E0B'],
            ['name' => 'Mobile Apps', 'color' => '#EC4899'],
        ];

        foreach ($categories as $category) {
            BlogCategory::create([
                'name' => $category['name'],
                'slug' => Str::slug($category['name']),
                'color' => $category['color'],
                'is_active' => true,
                'sort_order' => 0,
            ]);
        }

        // Create tags
        $tags = ['Laravel', 'Vue.js', 'React', 'Tailwind', 'SEO', 'Marketing', 'Business', 'E-commerce'];
        
        foreach ($tags as $tag) {
            BlogTag::create([
                'name' => $tag,
                'slug' => Str::slug($tag),
                'is_active' => true,
            ]);
        }

        // Get first user as author
        $author = User::first();

        if ($author) {
            // Create sample blog posts
            $posts = [
                [
                    'title' => 'Digital Transformation Trends Shaping 2024',
                    'excerpt' => 'Discover the key technologies and strategies that will define digital success for businesses this year.',
                    'content' => 'Full article content here...',
                    'is_featured' => true,
                    'is_published' => true,
                    'category_id' => 1,
                ],
                [
                    'title' => 'AI in Digital Marketing: Revolutionizing Customer Engagement',
                    'excerpt' => 'How artificial intelligence is transforming marketing strategies and creating personalized customer experiences.',
                    'content' => 'Full article content here...',
                    'is_featured' => false,
                    'is_published' => true,
                    'category_id' => 2,
                ],
                [
                    'title' => 'E-commerce Growth Strategies for Saudi Market',
                    'excerpt' => 'Optimize your online store for the Saudi market with these proven strategies and best practices.',
                    'content' => 'Full article content here...',
                    'is_featured' => true,
                    'is_published' => true,
                    'category_id' => 3,
                ],
            ];

            foreach ($posts as $post) {
                $blogPost = BlogPost::create([
                    'title' => $post['title'],
                    'slug' => Str::slug($post['title']),
                    'excerpt' => $post['excerpt'],
                    'content' => $post['content'],
                    'category_id' => $post['category_id'],
                    'author_id' => $author->id,
                    'is_featured' => $post['is_featured'],
                    'is_published' => $post['is_published'],
                    'published_at' => now(),
                    'read_time' => 5,
                    'views' => rand(100, 1000),
                ]);

                // Attach random tags
                $tagIds = BlogTag::inRandomOrder()->limit(3)->pluck('id')->toArray();
                $blogPost->tags()->attach($tagIds);
            }
        }
    }
}