<?php

namespace Database\Seeders;

use App\Models\Author;
use App\Models\Category;
use App\Models\Post;
use App\Models\PostCategory;
use Faker\Factory as FakerFactory;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class PostsSeeder extends Seeder
{
    /*
     * Run the database seeds.
     */
    public function run(): void
    {
        $authors = Author::where('enabled', true)->get();

        $categories = Category::where('enabled', true)->get();

        $faker = FakerFactory::create();

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

            $photo_id = (($i + 1) % 16); // placekitten has only 16 photos

            $post_title = $faker->sentence;
            $post_slug = Str::slug($post_title, '-');

            $cloned_authors = clone $authors;
            $cloned_categories = clone $categories;

            $createdAt = $faker->dateTimeBetween('-1 year', 'now');

            $post = Post::create([
                'title' => $post_title,
                'slug' => $post_slug,
                'excerpt' => $faker->paragraph,
                'author_id' => $cloned_authors->shuffle()->first()->id,
                'featured' => rand(0, 1),
                'featured_image' => "https://placekitten.com/1920/1080?image={$photo_id}",
                'editor' => 'editorjs',
                'post_format' => 'standard',
                'comment_count' => rand(0, 100),
                'likes_count' => rand(0, 100),
                'status' => 'publish',
                'body' => "{\"time\":1563816717958,\"blocks\":[{\"data\":{\"text\":\"Editor.js\",\"level\":2},\"type\":\"header\"},{\"data\":{\"text\":\"Hey. Meet the new Editor. On this page you can see it in action \\u2014 try to edit this text.\"},\"type\":\"paragraph\"},{\"data\":{\"text\":\"Key features\",\"level\":3},\"type\":\"header\"},{\"data\":{\"items\":[\"It is a block-styled editor\",\"It returns clean data output in JSON\",\"Designed to be extendable and pluggable with a simple API\"],\"style\":\"unordered\"},\"type\":\"list\"},{\"data\":{\"text\":\"What does it mean \\u00abblock-styled editor\\u00bb\",\"level\":3},\"type\":\"header\"},{\"data\":{\"text\":\"Workspace in classic editors is made of a single contenteditable element, used to create different HTML markups. Editor.js <mark class=\\\"cdx-marker\\\">workspace consists of separate Blocks: paragraphs, headings, images, lists, quotes, etc<\\\/mark>. Each of them is an independent contenteditable element (or more complex structure) provided by Plugin and united by Editor's Core.\"},\"type\":\"paragraph\"},{\"data\":{\"text\":\"There are dozens of <a href=\\\"https:\\\/\\\/github.com\\\/editor-js\\\">ready-to-use Blocks<\\\/a> and the <a href=\\\"https:\\\/\\\/editorjs.io\\\/creating-a-block-tool\\\">simple API<\\\/a> for creation any Block you need. For example, you can implement Blocks for Tweets, Instagram posts, surveys and polls, CTA-buttons and even games.\"},\"type\":\"paragraph\"},{\"data\":{\"text\":\"What does it mean clean data output\",\"level\":3},\"type\":\"header\"},{\"data\":{\"text\":\"Classic WYSIWYG-editors produce raw HTML-markup with both content data and content appearance. On the contrary, Editor.js outputs JSON object with data of each Block. You can see an example below\"},\"type\":\"paragraph\"},{\"data\":{\"text\":\"Given data can be used as you want: render with HTML for <code class=\\\"inline-code\\\">Web clients<\\\/code>, render natively for <code class=\\\"inline-code\\\">mobile apps<\\\/code>, create markup for <code class=\\\"inline-code\\\">Facebook Instant Articles<\\\/code> or <code class=\\\"inline-code\\\">Google AMP<\\\/code>, generate an <code class=\\\"inline-code\\\">audio version<\\\/code> and so on.\"},\"type\":\"paragraph\"},{\"data\":{\"text\":\"Clean data is useful to sanitize, validate and process on the backend.\"},\"type\":\"paragraph\"},{\"data\":[],\"type\":\"delimiter\"},{\"data\":{\"text\":\"We have been working on this project more than three years. Several large media projects help us to test and debug the Editor, to make it's core more stable. At the same time we significantly improved the API. Now, it can be used to create any plugin for any task. Hope you enjoy. \\ud83d\\ude0f\"},\"type\":\"paragraph\"},{\"data\":{\"file\":{\"url\":\"https:\\\/\\\/codex.so\\\/upload\\\/redactor_images\\\/o_e48549d1855c7fc1807308dd14990126.jpg\"},\"caption\":\"Image caption\",\"stretched\":false,\"withBorder\":true,\"withBackground\":false},\"type\":\"image\"}],\"version\":\"2.15.0\"}",
                'created_at' => $createdAt,
                'updated_at' => $createdAt,
            ]);

            $post_category = PostCategory::create([
                'post_id' => $post->id,
                'category_id' => $cloned_categories->shuffle()->first()->id,
            ]);
        }
    }
}
