<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CoreBundle\Tests\MessageHandler;

use Doctrine\ORM\EntityManagerInterface;
use Ferienpass\CoreBundle\Entity\User;
use Ferienpass\CoreBundle\Message\DeleteUserAccount as DeleteUserAccountCommand;
use Ferienpass\CoreBundle\MessageHandler\DeleteUserAccount;
use Ferienpass\CoreBundle\Repository\UserRepository;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Uid\Uuid;

final class DeleteUserAccountTest extends TestCase
{
    public function testDoesNothingWhenUserNotFound(): void
    {
        $users = $this->createMock(UserRepository::class);
        $users->method('findOneBy')->willReturn(null);

        $em = $this->createMock(EntityManagerInterface::class);
        $em->expects(self::never())->method('remove');

        $bus = $this->createMock(MessageBusInterface::class);
        $bus->expects(self::never())->method('dispatch');

        $handler = new DeleteUserAccount($bus, $users, $em);
        $handler(new DeleteUserAccountCommand('999'));
    }

    public function testRemovesUserWithNoParticipants(): void
    {
        $user = new User();

        $users = $this->createMock(UserRepository::class);
        $users->method('findOneBy')->willReturn($user);

        $removedEntities = [];
        $em = $this->createMock(EntityManagerInterface::class);
        $em->method('remove')->willReturnCallback(static function (object $entity) use (&$removedEntities) {
            $removedEntities[] = $entity;
        });

        $bus = $this->createMock(MessageBusInterface::class);
        $bus->expects(self::never())->method('dispatch');

        $handler = new DeleteUserAccount($bus, $users, $em);
        $handler(new DeleteUserAccountCommand(Uuid::v7()->toRfc4122()));

        self::assertContains($user, $removedEntities);
    }

    public function testRemovesParticipantsAndDispatchesReorderForOffers(): void
    {
        $user = new User();

        $offer = new \Ferienpass\CoreBundle\Entity\Offer\Base();

        $participant = new \Ferienpass\CoreBundle\Entity\Participant\BaseParticipant();
        $attendance = new \Ferienpass\CoreBundle\Entity\Attendance($offer, $participant);

        // Add participant to user
        $participantRp = new \ReflectionProperty($user, 'participants');
        $participantCollection = $participantRp->getValue($user);
        $participantCollection->add($participant);

        // Add attendance to participant
        $attendanceRp = new \ReflectionProperty($participant, 'attendances');
        $attendanceCollection = $attendanceRp->getValue($participant);
        $attendanceCollection->add($attendance);

        $users = $this->createMock(UserRepository::class);
        $users->method('findOneBy')->willReturn($user);

        $removedEntities = [];
        $em = $this->createMock(EntityManagerInterface::class);
        $em->method('remove')->willReturnCallback(static function (object $entity) use (&$removedEntities) {
            $removedEntities[] = $entity;
        });

        $bus = $this->createMock(MessageBusInterface::class);
        $bus->expects(self::once())->method('dispatch')->willReturn(new Envelope(new \stdClass()));

        $handler = new DeleteUserAccount($bus, $users, $em);
        $handler(new DeleteUserAccountCommand(Uuid::v7()->toRfc4122()));

        self::assertContains($attendance, $removedEntities);
        self::assertContains($participant, $removedEntities);
        self::assertContains($user, $removedEntities);
    }
}
