<?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\Export\ParticipantList;

use Ferienpass\CoreBundle\ConsentManager\ConsentManager;
use Ferienpass\CoreBundle\Entity\Attendance;
use Ferienpass\CoreBundle\Entity\ConsentForm;
use Ferienpass\CoreBundle\Entity\Offer\OfferInterface;
use Ferienpass\CoreBundle\Entity\Participant;
use Ferienpass\CoreBundle\Export\ParticipantList\PdfExport;
use Ferienpass\CoreBundle\Repository\ConsentFormRepository;
use Ferienpass\CoreBundle\Settings\SystemSettingsManager;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Uid\Uuid;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Twig\Environment;

class PdfExportTest extends TestCase
{
    public function testCreatesPdf(): void
    {
        $offer = $this->createStub(OfferInterface::class);
        $offer->method('getName')->willReturn('My Offer');

        $filesystem = new Filesystem();
        $twig = $this->createMock(Environment::class);
        $twig
            ->expects($this->once())
            ->method('render')
            ->willReturn('<h1>Participant List: My Offer</h1>')
        ;

        $dispatcher = $this->createStub(EventDispatcherInterface::class);
        $consentManager = $this->createStub(ConsentManager::class);
        $consentFormRepository = $this->createStub(ConsentFormRepository::class);

        $pdfExport = new PdfExport($filesystem, $twig, $dispatcher, $consentManager, $consentFormRepository, $this->sortSetting(), sys_get_temp_dir());

        $path = $pdfExport->generate($offer);

        self::assertStringEndsWith('.pdf', $path);
        if (file_exists($path)) {
            unlink($path);
        }
    }

    public function testCreatesExpandedPdf(): void
    {
        $offer = $this->createStub(OfferInterface::class);
        $offer->method('getName')->willReturn('My Offer');

        $filesystem = new Filesystem();
        $twig = $this->createMock(Environment::class);

        $dispatcher = $this->createStub(EventDispatcherInterface::class);
        $consentManager = $this->createStub(ConsentManager::class);
        $consentFormRepository = $this->createStub(ConsentFormRepository::class);

        $consentForm = $this->createStub(ConsentForm::class);
        $consentForm->method('getId')->willReturn(Uuid::v6());
        $consentForm->method('getSubject')->willReturn('participant');

        $participant = $this->createStub(Participant::class);

        $attendance = $this->createStub(Attendance::class);
        $attendance->method('getId')->willReturn(1);
        $attendance->method('getParticipant')->willReturn($participant);

        $consentFormRepository->method('findForOffer')->willReturn([$consentForm]);
        $offer->method('getAttendancesConfirmedOrWaitlisted')->willReturn(new \Doctrine\Common\Collections\ArrayCollection([$attendance]));

        $twig
            ->expects($this->once())
            ->method('render')
            ->with($this->anything(), $this->callback(static function (array $args) {
                return isset($args['expanded']) && true === $args['expanded']
                    && isset($args['attendanceConsents']) && 1 === \count($args['attendanceConsents']);
            }))
            ->willReturn('<h1>Expanded Participant List: My Offer</h1>')
        ;

        $pdfExport = new PdfExport($filesystem, $twig, $dispatcher, $consentManager, $consentFormRepository, $this->sortSetting(), sys_get_temp_dir());

        $path = $pdfExport->generate($offer, expanded: true);

        self::assertStringEndsWith('.pdf', $path);
        if (file_exists($path)) {
            unlink($path);
        }
    }

    private function sortSetting(string $sort = 'name'): SystemSettingsManager
    {
        $manager = $this->createStub(SystemSettingsManager::class);
        $manager->method('getParticipantListSort')->willReturn($sort);

        return $manager;
    }
}
