<?php

use Illuminate\Database\Seeder;
use App\Topic;
use App\Rank;
use App\Comment;
use App\CommentDetail;
use App\TopicKeyword;
use App\Http\Resources\TopicResource;

class TopicFaker extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $imgPath = public_path('uploads/image/topic');

        //rest topic table
        Topic::truncate();
        Comment::truncate();
        CommentDetail::truncate();
        Rank::truncate();
        try {
            shell_exec('rm -rf ' . $imgPath . '/*.jpg');
            shell_exec('rm -rf ' . $imgPath . '/thumbnail_60x60/*.jpg');
        } catch (Exception $exception) {
            logger($exception->getMessage());
        }

        $limit = 1500;
        $commentPerTopic = 20;
        $faker = Faker\Factory::create();
        $listRankIds = [];

        $startDate = 10;
        $currentMonth = date('Y-m');
        $currentDate = date('d');
        for ($j = $startDate; $j <= $currentDate; $j++) {
            $dt = ($j < 10) ? '0' . $j : $j;
            $date = $currentMonth . '-' . $dt;
            $checkRank = Rank::where('created_date', $date)->first();
            $checkMax = Rank::orderBy('rank_number', 'DESC')->first();
            $rank_number = isset($checkMax) ? $checkMax->rank_number : 0;
            $rank_number += 1;
            if (!isset($checkRank)) {
                $checkRank = Rank::create([
                    'created_date' => $date,
                    'rank_number'  => $rank_number,
                    'total_post'   => 0,
                    'created_at'   => $date . ' ' . date('H:i:s'),
                    'updated_at'   => $date . ' ' . date('H:i:s'),
                ]);
                print "Created Rank: " . $date . "\r\n";
            }
            $listRankIds[] = $checkRank->id;
        }

        //Get rank list
        $ranks = Rank::where('created_date', '>=', $currentMonth . "-" . ($startDate < 10 ? "0" . $startDate : $startDate))->get();
        foreach ($ranks as $rank) {
            $listRankIds[] = $rank->id;
        }

        for ($i = 0; $i < $limit; $i++) {
            //$thumbnailPath = $faker->image(storage_path('app/public'), $width = 640, $height = 480);
            $thumbnailPath = $faker->file(public_path('randompic'), storage_path('app/public'));
            try {
                $thumbnail = TopicResource::createThumb($thumbnailPath);
                unlink($thumbnailPath);
            } catch (Exception $exception) {
                $thumbnail = basename($thumbnailPath);
                logger("[Ex] Image faker error." . $exception->getMessage());
            }

            $is_anonymous = rand(0, 1);
            $author = ($is_anonymous == 1) ? "" : $faker->name;
            $content = $faker->text(200);
            $title = $faker->text(50);
            $idRand = rand(0, count($listRankIds) - 1);
            $rankIt = Rank::where('id', $listRankIds[$idRand])->first();

            $topic = Topic::create([
                'code'             => TopicResource::generateCode(),
                'title'            => $title,
                'thumbnail'        => $thumbnail,
                'author'           => $author,
                'rank_id'          => $rankIt->id,
                'is_allow_comment' => 1,
                'is_activated'     => 1,
                'is_deleted'       => 0,
                'created_at'       => $rankIt->created_date . ' ' . date('H:i:s'),
                'updated_at'       => $rankIt->created_date . ' ' . date('H:i:s'),
            ]);

            $comment = Comment::create([
                'idx'              => 1,
                'topic_id'         => $topic->id,
                /*'parent_id'        => 0,*/
                'content'          => $content,
                'image'            => $thumbnail,
                'author'           => $author,
                'like'             => rand(5000, 10000),
                'dislike'          => rand(1000, 5000),
                'is_anonymous'     => $is_anonymous,
                'is_show_id'       => 0,
                'is_allow_comment' => 1,
                'is_activated'     => 1,
                'is_deleted'       => 0,
                'created_at'       => $rankIt->created_date . ' ' . date('H:i:s'),
                'updated_at'       => $rankIt->created_date . ' ' . date('H:i:s'),
            ]);

            CommentDetail::create([
                'comment_id'  => $comment->id,
                'content'     => $content,
                'current_img' => -1,
                'is_url'      => 0,
                'created_at'  => $rankIt->created_date . ' ' . date('H:i:s'),
                'updated_at'  => $rankIt->created_date . ' ' . date('H:i:s'),
            ]);

            for ($j = 1; $j <= $commentPerTopic; $j++) {
                $cmtContent = $faker->text(150);
                $comment = Comment::create([
                    'idx'              => ($j + 1),
                    'topic_id'         => $topic->id,
                    /*'parent_id'        => 0,*/
                    'content'          => $cmtContent,
                    'like'             => rand(5000, 10000),
                    'dislike'          => rand(1000, 5000),
                    'created_at'       => $rankIt->created_date . ' ' . date('H:i:s'),
                    'updated_at'       => $rankIt->created_date . ' ' . date('H:i:s'),
                ]);

                CommentDetail::create([
                    'comment_id'  => $comment->id,
                    'content'     => $cmtContent,
                    'current_img' => -1,
                    'is_url'      => 0,
                    'created_at'  => $rankIt->created_date . ' ' . date('H:i:s'),
                    'updated_at'  => $rankIt->created_date . ' ' . date('H:i:s'),
                ]);
            }

            $topic->total_comment = $commentPerTopic + 1;
            $topic->save();

            print "Created topic: " . $title . "\r\n";
        }

        TopicKeyword::truncate();
        $this->call(KeywordImport::class);
        $this->call(CategoryImport::class);
    }
}
