<?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 Doctrine\Common\Collections\ArrayCollection;
use Ferienpass\CoreBundle\ConsentManager\ConsentManager;
use Ferienpass\CoreBundle\Entity\Offer\OfferInterface;
use Ferienpass\CoreBundle\Export\ParticipantList\XlsxExport;
use Ferienpass\CoreBundle\Repository\ConsentFormRepository;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Contracts\Translation\TranslatorInterface;

final class XlsxExportVisibilityTest extends TestCase
{
    public function testDateOfBirthIsShownWhenUserHasRole(): void
    {
        $security = $this->createMock(Security::class);
        $security->method('isGranted')->with('ROLE_PARTICIPANTS_ADMIN')->willReturn(true);

        $offer = $this->createOffer(false);
        $export = $this->createExport($security);

        $path = $export->generate($offer);
        $spreadsheet = IOFactory::load($path);
        $sheet = $spreadsheet->getActiveSheet();

        $headers = [];
        for ($i = 1; $i <= 10; ++$i) {
            $val = $sheet->getCell([$i, 3])->getValue();
            if ($val) {
                $headers[] = $val;
            }
        }

        self::assertContains('Geburtsdatum', $headers);
        unlink($path);
    }

    public function testDateOfBirthIsShownWhenOfferHasSetting(): void
    {
        $security = $this->createMock(Security::class);
        $security->method('isGranted')->with('ROLE_PARTICIPANTS_ADMIN')->willReturn(false);

        $offer = $this->createOffer(true);
        $export = $this->createExport($security);

        $path = $export->generate($offer);
        $spreadsheet = IOFactory::load($path);
        $sheet = $spreadsheet->getActiveSheet();

        $headers = [];
        for ($i = 1; $i <= 10; ++$i) {
            $val = $sheet->getCell([$i, 3])->getValue();
            if ($val) {
                $headers[] = $val;
            }
        }

        self::assertContains('Geburtsdatum', $headers);
        unlink($path);
    }

    public function testDateOfBirthIsHiddenWhenNeitherRoleNorSetting(): void
    {
        $security = $this->createMock(Security::class);
        $security->method('isGranted')->with('ROLE_PARTICIPANTS_ADMIN')->willReturn(false);

        $offer = $this->createOffer(false);
        $export = $this->createExport($security);

        $path = $export->generate($offer);
        $spreadsheet = IOFactory::load($path);
        $sheet = $spreadsheet->getActiveSheet();

        $headers = [];
        for ($i = 1; $i <= 10; ++$i) {
            $val = $sheet->getCell([$i, 3])->getValue();
            if ($val) {
                $headers[] = $val;
            }
        }

        self::assertNotContains('Geburtsdatum', $headers);
        unlink($path);
    }

    private function createExport(Security $security): XlsxExport
    {
        return new XlsxExport(
            $this->createStub(Filesystem::class),
            $security,
            $this->createStub(ConsentManager::class),
            $this->createStub(ConsentFormRepository::class),
            $this->createStub(TranslatorInterface::class),
            'name'
        );
    }

    private function createOffer(bool $collectDateOfBirth): OfferInterface
    {
        $offer = $this->createStub(OfferInterface::class);
        $offer->method('getName')->willReturn('Test Offer');
        $offer->method('isCollectDateOfBirth')->willReturn($collectDateOfBirth);
        $offer->method('getDates')->willReturn(new ArrayCollection([]));
        $offer->method('getApplicationExtra')->willReturn([]);
        $offer->method('getAttendancesConfirmedOrWaitlisted')->willReturn(new ArrayCollection([]));

        return $offer;
    }
}
