<?php

use Faker\Generator as Faker;

$factory->define(App\Models\Post::class, function (Faker $faker) {

    $adminsAndEditors = App\Models\User::select("id")->where("role_id", 1)->orWhere("role_id", 2)->get();

    return [
        "user_id" => $faker->numberBetween(1, 50),
        "category_id" => $faker->randomElement($array = array(1, 2, 3, 4)),
        "confirmed_by" => $faker->randomElement($array = $adminsAndEditors),
        "title" => ucfirst($faker->words(4, true)),
        "slug" => $faker->slug,
        "image_link" => $faker->image(public_path() . '/uploads', 1024, 768, 'city', false),
        "content" => $faker->realText(),
        "is_active" => $faker->boolean,
        "total_likes" => $faker->randomDigit,
        "is_confirmed" => $faker->boolean,
    ];
});

$factory->afterCreating(\App\Models\Post::class, function ($post, $faker) {

    for ($i=0; $i<rand(20, 50); $i++) {

        if(!$post->is_confirmed || !$post->is_active) continue;

        $post->like()->create([
            "post_id" => $post->id,
            "ip_address" => $faker->ipv4
        ]);

        $post->comment()->create([
            "post_id" => $post->id,
            "name" => $faker->name,
            "email" => $faker->email,
            "comment" => $faker->text,
            "is_active" => $faker->boolean,
        ]);

        $post->increment("total_likes");
        $post->save();
    }
});
