<?php

namespace App\Command;

use App\Core\Manager\EntityManager;
use App\Entity\Blog\Category;
use App\Entity\Blog\Comment;
use App\Entity\Blog\Post;
use App\Repository\Blog\PostRepositoryQuery;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class MigrateDataCommand extends Command
{
    protected static $defaultName = 'app:migrate-data';
    protected static $defaultDescription = '';

    public function __construct(EntityManager $entityManager, PostRepositoryQuery $postRepo)
    {
        $this->entityManager = $entityManager;
        $this->postRepo = $postRepo;

        parent::__construct();
    }

    protected function configure()
    {
        $this
            ->setDescription(self::$defaultDescription)
            // ->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
            // ->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        /*
        foreach ($this->postRepo->create()->find() as $post)
        {
            $tags = $post->getTags();
            $newTags = [];

            foreach ($tags as $k => $v) {
                if (!empty($v['label'])) {
                    $newTags[] = $v;
                }
            }

            $post->setTags($newTags);
            $this->entityManager->update($post);
        }

        require 'data/category.php';
        require 'data/comment.php';
        require 'data/post.php';
        require 'data/post_has_category.php';

        $categories = [];
        $posts = [];
        $comments = [];

        foreach ($oldCategories as $v) {
            $category = new Category();
            $category->id = (int) $v['id'];
            $category->setTitle($v['title']);
            $category->setSubTitle(null);
            $category->setDescription($v['description'] ?? '');
            $category->setIsActive((bool) $v['active']);

            $categories[$category->id] = $category;

            $this->entityManager->create($category);
        }

        foreach ($oldPosts as $v) {
            $post = new Post();
            $post->id = (int) $v['id'];
            $post->setTitle($v['title']);
            $post->setContent(str_replace("\r\n", "\n", $v['content']));
            $post->setContentFormat($v['content_format']);
            $post->setTags(explode(',', str_replace(', ', ',', $v['tags'])));
            $post->setStatus((int) $v['active']);
            $post->setImage($v['picture'] ? ('uploads/posts/2007-2021/'.$v['picture']) : null);
            $post->setCreatedAt(new \DateTime($v['created_at']));
            $post->setPublishedAt($v['published_at'] ? new \DateTime($v['published_at']) : null);
            $post->setIsQuick((int) $v['quick']);
            $post->setQuickUrl($v['quick_url']);
            $post->setQuickImage($v['quick_image']);
            $post->setQuickVideo($v['quick_video']);
            $post->setQuickVideoWidth($v['quick_video_width']);
            $post->setQuickVideoHeight($v['quick_video_height']);
            $post->setSlug($v['slug']);

            $posts[$post->id] = $post;

            $this->entityManager->create($post);
        }

        foreach ($post_has_category as $v) {
            $posts[(int) $v['post_id']]->addCategory($categories[(int) $v['category_id']]);

            $this->entityManager->update($posts[(int) $v['post_id']]);
        }

        foreach ($oldComments as $v) {
            $comment = new Comment();
            $comment->id = (int) $v['id'];
            $comment->setPost($posts[(int) $v['post_id']]);
            $comment->setParentComment($comments[(int) $v['parent_comment_id']] ?? null);
            $comment->setAuthor($v['author']);
            $comment->setWebsite($v['website']);
            $comment->setEmail($v['email']);
            $comment->setContent(str_replace("\r\n", "\n", $v['content']));
            $comment->setIsActive((bool) $v['active']);
            $comment->setCreatedAt(new \DateTime($v['created_at']));

            $comments[$comment->id] = $comment;

            $this->entityManager->create($comment);
        }
        */

        return Command::SUCCESS;
    }
}
