<?php

namespace Database\Factories;

use App\Models\Admin;
use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Post>
 */
class PostFactory extends Factory
{
    protected $model = Post::class;

    public function definition(): array
    {
        $title = $this->faker->sentence(6);

        return [
            'type' => 'announcement',
            'subtype' => null,
            'status' => 'published',
            'title' => $title,
            'slug' => Str::slug($title),
            'content' => $this->faker->paragraphs(4, true),
            'excerpt' => $this->faker->paragraph(),
            'featured_image' => null,
            'meta_title' => null,
            'meta_description' => null,
            'meta_keywords' => null,
            'is_featured' => false,
            'view_count' => 0,
            'sort_order' => 0,
            'published_at' => now(),
            'author_id' => Admin::query()->inRandomOrder()->value('id'),
        ];
    }

    public function announcement(?string $subtype = 'general'): static
    {
        return $this->state(fn () => ['type' => 'announcement', 'subtype' => $subtype]);
    }

    public function kbArticle(): static
    {
        return $this->state(fn () => ['type' => 'kb_article', 'subtype' => null]);
    }

    public function blogPost(): static
    {
        return $this->state(fn () => [
            'type' => 'blog_post',
            'subtype' => null,
            'featured_image' => '/img/blog-default.png',
        ]);
    }

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

    public function archived(): static
    {
        return $this->state(fn () => ['status' => 'archived']);
    }
}
