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

use Ferienpass\CoreBundle\Entity\Attendance;
use Ferienpass\CoreBundle\Entity\ConversationItem;
use Ferienpass\CoreBundle\Entity\Offer\Base;
use Ferienpass\CoreBundle\Entity\Participant\BaseParticipant;
use Ferienpass\CoreBundle\Notification\CommentNotification;
use PHPUnit\Framework\TestCase;

/**
 * The application summary in a conversation e-mail: the block only appears when
 * the author asked for it, because it is the parent's whole route into the
 * application — naming a registration without handing over a usable link would
 * describe something the recipient cannot reach.
 */
class CommentNotificationAttendanceTest extends TestCase
{
    public function testTheSummaryIsAbsentUntilALinkIsSet(): void
    {
        $notification = $this->notification($this->attendance());

        self::assertNotNull($notification->attendance(), 'the comment is about an application');
        self::assertNull($notification->attendanceSummary(), 'but no link came along, so nothing to summarise');
    }

    public function testTheSummaryCarriesTheApplicationOnceLinked(): void
    {
        $attendance = $this->attendance();

        $notification = $this->notification($attendance)->attendanceUrl('https://example.org/anmeldung/abc?valid=x');

        self::assertSame($attendance, $notification->attendanceSummary());
    }

    public function testAConversationAboutSomethingElseNeverGetsTheSummary(): void
    {
        // A comment on an offer has no application behind it; a link set anyway
        // must not make the template render a summary of nothing.
        $comment = new ConversationItem(new Base(), null, 'Hallo');

        $notification = $this->notification(null, $comment)->attendanceUrl('https://example.org/x');

        self::assertNull($notification->attendanceSummary());
    }

    private function notification(?Attendance $attendance, ?ConversationItem $comment = null): CommentNotification
    {
        $comment ??= new ConversationItem($attendance, null, 'Bitte laden Sie noch das Lichtbild hoch.');

        return new CommentNotification()->comment($comment);
    }

    private function attendance(): Attendance
    {
        $offer = new Base();
        $offer->setName('Kinderstadt NOHhausen');

        return new Attendance($offer, new BaseParticipant());
    }
}
