<?php

declare(strict_types=1);

/* For licensing terms, see /license.txt */

namespace Chamilo\Tests\CourseBundle\Repository;

use Chamilo\CourseBundle\Entity\CForum;
use Chamilo\CourseBundle\Entity\CForumAttachment;
use Chamilo\CourseBundle\Entity\CForumPost;
use Chamilo\CourseBundle\Entity\CForumThread;
use Chamilo\CourseBundle\Repository\CForumAttachmentRepository;
use Chamilo\CourseBundle\Repository\CForumPostRepository;
use Chamilo\CourseBundle\Repository\CForumRepository;
use Chamilo\CourseBundle\Repository\CForumThreadRepository;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
use DateTime;

class CForumPostRepositoryTest extends AbstractApiTest
{
    use ChamiloTestTrait;

    public function testCreate(): void
    {
        $course = $this->createCourse('new');
        $teacher = $this->createUser('teacher');

        $forumRepo = self::getContainer()->get(CForumRepository::class);
        $threadRepo = self::getContainer()->get(CForumThreadRepository::class);
        $postRepo = self::getContainer()->get(CForumPostRepository::class);
        $attachmentRepo = self::getContainer()->get(CForumAttachmentRepository::class);
        $request_stack = $this->getMockedRequestStack([
            'session' => ['studentview' => 1],
        ]);
        $postRepo->setRequestStack($request_stack);

        $forum = (new CForum())
            ->setTitle('forum')
            ->setParent($course)
            ->setCreator($teacher)
        ;
        $forumRepo->create($forum);

        $thread = (new CForumThread())
            ->setTitle('thread title')
            ->setForum($forum)
            ->setParent($course)
            ->setCreator($teacher)
        ;
        $threadRepo->create($thread);
        $date = new DateTime();
        $post = (new CForumPost())
            ->setTitle('post')
            ->setPostText('text')
            ->setPostDate($date)
            ->setPostNotification(true)
            ->setVisible(true)
            ->setStatus(1)
            ->setPostParent(null)
            ->setParent($course)
            ->setCreator($teacher)
            ->setThread($thread)
            ->setForum($forum)
            ->setUser($teacher)
            ->addCourseLink($course)
        ;
        $postRepo->create($post);

        $this->assertSame('text', $post->getPostText());
        $this->assertSame($date, $post->getPostDate());
        $this->assertTrue($post->getVisible());
        $this->assertTrue($post->getPostNotification());
        $this->assertSame(1, $post->getStatus());
        $this->assertSame($post->getIid(), $post->getResourceIdentifier());

        $file = $this->getUploadedFile();

        $attachment = (new CForumAttachment())
            ->setComment('comment')
            ->setCId($course->getId())
            ->setFilename($file->getFilename())
            ->setPath('')
            ->setPost($post)
            ->setSize($file->getSize())
            ->setParent($post)
            ->setCreator($teacher)
            ->addCourseLink($course)
        ;

        $attachmentRepo->create($attachment);
        $attachmentRepo->addFile($attachment, $file);
        $attachmentRepo->update($attachment);

        $this->assertSame($attachment->getResourceIdentifier(), $attachment->getIid());
        $this->assertSame($file->getFilename(), (string) $attachment);
        $this->assertNotNull($attachment->getResourceNode());
        $this->assertNotNull($attachment->getResourceNode()->getResourceFiles()->first());

        $this->getEntityManager()->clear();

        $post = $postRepo->find($post->getIid());

        $this->assertSame('post', (string) $post);
        $this->assertSame(1, $post->getAttachments()->count());
        $this->assertSame(1, $postRepo->count([]));
        $this->assertSame(1, $threadRepo->count([]));
        $this->assertSame(1, $forumRepo->count([]));
        $this->assertSame(1, $attachmentRepo->count([]));

        /** @var CForumThread $thread */
        $thread = $threadRepo->find($thread->getIid());

        /** @var CForum $forum */
        $forum = $forumRepo->find($forum->getIid());

        $this->assertSame(1, $thread->getPosts()->count());
        $this->assertSame(1, $forum->getThreads()->count());
        $this->assertSame(1, $forum->getPosts()->count());

        $count = $postRepo->countCourseForumPosts($course);
        $this->assertSame(1, $count);

        $count = $postRepo->countUserForumPosts($teacher, $course);
        $this->assertSame(1, $count);

        $postRepo->delete($post);

        $this->assertSame(0, $postRepo->count([]));
        $this->assertSame(0, $attachmentRepo->count([]));
        // FIXME Bring back once behavior is fixed on the source.
        // Similar to category-forum a delete is triggering associated values
        // removal, it is pending to fix code and re-enable these assertions..
        // $this->assertSame(1, $threadRepo->count([]));
        // $this->assertSame(1, $forumRepo->count([]));

        $this->getEntityManager()->clear();

        /** @var CForum $forum */
        $forum = $forumRepo->find($forum->getIid());
        // FIXME Bring back once behavior is fixed on the source.
        // $forumRepo->delete($forum);

        // $this->assertSame(0, $threadRepo->count([]));
        // $this->assertSame(0, $forumRepo->count([]));
    }
}
