<?php

declare(strict_types=1);

namespace Ferienpass\AdminBundle\Tests\EventListener;

use Ferienpass\AdminBundle\Event\ParticipantListEvent;
use Ferienpass\AdminBundle\EventListener\ParticipantListListener;
use Ferienpass\CoreBundle\Entity\Offer\OfferInterface;
use PHPUnit\Framework\TestCase;

final class ParticipantListListenerTest extends TestCase
{
    public function testDoesNothingWhenApplicationExtraIsNull(): void
    {
        $offer = $this->createMock(OfferInterface::class);
        $offer->method('getApplicationExtra')->willReturn(null);

        $event = new ParticipantListEvent($offer, ['col1' => 'template1']);

        $listener = new ParticipantListListener();
        $listener($event);

        self::assertSame(['col1' => 'template1'], $event->getColumns());
    }

    public function testAddsTextColumnForNonYnExtra(): void
    {
        $extra = [
            ['uuid' => 'abc-123', 'type' => 'text', 'question' => 'What is your name?'],
        ];

        $offer = $this->createMock(OfferInterface::class);
        $offer->method('getApplicationExtra')->willReturn($extra);

        $event = new ParticipantListEvent($offer, []);

        $listener = new ParticipantListListener();
        $listener($event);

        self::assertArrayHasKey('What is your name?', $event->getColumns());
        self::assertSame("{{ item.extra('abc-123') }}", $event->getColumns()['What is your name?']);
    }

    public function testAddsYesNoColumnForYnExtra(): void
    {
        $extra = [
            ['uuid' => 'def-456', 'type' => 'yn', 'question' => 'Vegetarian?'],
        ];

        $offer = $this->createMock(OfferInterface::class);
        $offer->method('getApplicationExtra')->willReturn($extra);

        $event = new ParticipantListEvent($offer, []);

        $listener = new ParticipantListListener();
        $listener($event);

        self::assertArrayHasKey('Vegetarian?', $event->getColumns());
        self::assertSame("{{ 'y' == item.extra('def-456') ? 'Ja' : 'Nein' }}", $event->getColumns()['Vegetarian?']);
    }

    public function testPreservesExistingColumns(): void
    {
        $extra = [
            ['uuid' => 'xyz', 'type' => 'text', 'question' => 'Extra Q'],
        ];

        $offer = $this->createMock(OfferInterface::class);
        $offer->method('getApplicationExtra')->willReturn($extra);

        $event = new ParticipantListEvent($offer, ['existing' => 'template']);

        $listener = new ParticipantListListener();
        $listener($event);

        self::assertArrayHasKey('existing', $event->getColumns());
        self::assertArrayHasKey('Extra Q', $event->getColumns());
    }

    public function testAddsBirthdateColumnWhenOfferCollectsDateOfBirth(): void
    {
        $offer = $this->createMock(OfferInterface::class);
        $offer->method('isCollectDateOfBirth')->willReturn(true);
        $offer->method('getApplicationExtra')->willReturn(null);

        $event = new ParticipantListEvent($offer, []);

        $listener = new ParticipantListListener();
        $listener($event);

        self::assertArrayHasKey('dateOfBirth', $event->getColumns());
        self::assertStringContainsString('item.participant.dateOfBirth', $event->getColumns()['dateOfBirth']);
    }

    public function testDoesNotAddBirthdateColumnWhenFlagIsFalse(): void
    {
        $offer = $this->createMock(OfferInterface::class);
        $offer->method('isCollectDateOfBirth')->willReturn(false);
        $offer->method('getApplicationExtra')->willReturn(null);

        $event = new ParticipantListEvent($offer, []);

        $listener = new ParticipantListListener();
        $listener($event);

        self::assertArrayNotHasKey('dateOfBirth', $event->getColumns());
    }
}
