<?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\Participant;

use Doctrine\ORM\EntityManagerInterface;
use Ferienpass\CoreBundle\Entity\Participant\ParticipantInterface;

class ParticipantMerger
{
    public function __construct(private readonly EntityManagerInterface $entityManager)
    {
    }

    public function merge(ParticipantInterface $merged, ParticipantInterface $base): void
    {
        foreach ($merged->getAttendances() as $attendance) {
            $attendance->setParticipant($base);
        }

        foreach ($merged->getConsents() as $consent) {
            $consent->setParticipant($base);
        }

        foreach ($merged->getActivity() as $log) {
            $log->setParticipant($base);
        }

        foreach ($merged->getFriendCodes() as $friendCode) {
            $base->addFriendCode($friendCode);
        }

        foreach ($merged->getAccessCodes() as $accessCode) {
            $base->addAccessCode($accessCode);
        }

        $this->entityManager->remove($merged);
        $this->entityManager->flush();
    }
}
