<?php

namespace Database\Factories;

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

use App\Models\Version;

class BlogFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Blog::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'author' => $this->faker->firstName.' '.$this->faker->lastName,
        ];
    }

    public function configure()
    {
        return $this->afterCreating(function (Blog $blog) {
            $version = Version::factory()->blog()->create([
                'versionable_type' => get_class($blog),
                'versionable_id' => $blog->id,
            ]);
        });
    }

    public function published()
    {
        return $this->state([
            'published_version_id' => Version::factory()->published()->for(Blog::factory(), 'versionable'),
        ]);
    }

    public function unpublished()
    {
        return $this->state([
            'published_version_id' => null,
        ]);
    }
}
