<?php

namespace Database\Factories;

use App\Models\BlogCategory;
use App\Models\BlogPost;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/**
 * @extends Factory<BlogPost>
 */
class BlogPostFactory extends Factory
{
    protected $model = BlogPost::class;

    /**
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $title = fake()->sentence(4);

        return [
            'author_id' => User::factory(),
            'blog_category_id' => BlogCategory::query()->first()?->id
                ?? BlogCategory::query()->create([
                    'name' => 'General',
                    'slug' => 'general-'.Str::lower(Str::random(6)),
                    'description' => null,
                    'sort_order' => 0,
                ])->id,
            'title' => $title,
            'slug' => Str::slug($title).'-'.Str::lower(Str::random(6)),
            'excerpt' => fake()->optional()->sentence(12),
            'cover_image' => null,
            'content' => '<p>'.fake()->paragraphs(3, true).'</p>',
            'status' => 'published',
            'is_featured' => false,
            'featured_order' => 0,
            'meta_title' => null,
            'meta_description' => null,
            'published_at' => now(),
        ];
    }

    public function draft(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => 'draft',
            'published_at' => null,
        ]);
    }

    public function featured(): static
    {
        return $this->state(fn (array $attributes) => [
            'is_featured' => true,
            'featured_order' => 1,
        ]);
    }
}
