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

use Ferienpass\CoreBundle\Entity\MessengerLog;
use Ferienpass\CoreBundle\Entity\SentMessage;
use Ferienpass\CoreBundle\Entity\SentMessageEngagement;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;

final class SentMessageTest extends TestCase
{
    public function testGetIdIsNullByDefault(): void
    {
        $msg = new SentMessage();

        self::assertNull($msg->getId());
    }

    public function testGetCreatedAtIsSetOnConstruct(): void
    {
        $msg = new SentMessage();

        self::assertInstanceOf(\DateTimeInterface::class, $msg->getCreatedAt());
    }

    public function testGetEngagementsIsEmptyByDefault(): void
    {
        $msg = new SentMessage();

        self::assertCount(0, $msg->getEngagements());
    }

    public function testGetLogEntryIsNullByDefault(): void
    {
        $msg = new SentMessage();

        self::assertNull($msg->getLogEntry());
    }

    public function testIsEmailReturnsFalse(): void
    {
        $msg = new SentMessage();

        self::assertFalse($msg->isEmail());
    }

    public function testIsSmsReturnsFalse(): void
    {
        $msg = new SentMessage();

        self::assertFalse($msg->isSms());
    }

    public function testIsOpenedReturnsFalseWhenNoEngagements(): void
    {
        $msg = new SentMessage();

        self::assertFalse($msg->isOpened());
    }

    public function testIsClickedReturnsFalseWhenNoEngagements(): void
    {
        $msg = new SentMessage();

        self::assertFalse($msg->isClicked());
    }

    public function testIsBouncedReturnsFalseWhenNoEngagements(): void
    {
        $msg = new SentMessage();

        self::assertFalse($msg->isBounced());
    }

    public function testIsDeliveredReturnsFalseWhenNoEngagements(): void
    {
        $msg = new SentMessage();

        self::assertFalse($msg->isDelivered());
    }

    public function testIsDroppedReturnsFalseWhenNoEngagements(): void
    {
        $msg = new SentMessage();

        self::assertFalse($msg->isDropped());
    }

    public function testIsUnsubscribedReturnsFalseWhenNoEngagements(): void
    {
        $msg = new SentMessage();

        self::assertFalse($msg->isUnsubscribed());
    }

    public function testIsSpamReturnsFalseWhenNoEngagements(): void
    {
        $msg = new SentMessage();

        self::assertFalse($msg->isSpam());
    }

    public function testIsOpenedReturnsTrueWhenOpenEngagement(): void
    {
        $msg = new SentMessage();
        $engagement = new SentMessageEngagement($msg, MailerEngagementEvent::OPEN);

        // Inject engagement via reflection (collection is private)
        $prop = new \ReflectionProperty(SentMessage::class, 'engagements');
        $collection = $prop->getValue($msg);
        $collection->add($engagement);

        self::assertTrue($msg->isOpened());
    }

    public function testIsDeliveredReturnsTrueWhenDeliveredEngagement(): void
    {
        $msg = new SentMessage();
        $engagement = new SentMessageEngagement($msg, MailerDeliveryEvent::DELIVERED);

        $prop = new \ReflectionProperty(SentMessage::class, 'engagements');
        $collection = $prop->getValue($msg);
        $collection->add($engagement);

        self::assertTrue($msg->isDelivered());
    }

    public function testSetLogEntrySetsAndGetsValue(): void
    {
        $msg = new SentMessage();
        $log = new MessengerLog('TestMessage');
        $msg->setLogEntry($log);

        self::assertSame($log, $msg->getLogEntry());
    }

    /**
     * @param string[] $events
     */
    #[DataProvider('deliveryStatusProvider')]
    public function testDeliveryStatusForReducesEventsToTheFurthestStage(array $events, string $expected): void
    {
        self::assertSame($expected, SentMessage::deliveryStatusFor($events));
    }

    /**
     * @return iterable<string, array{string[], string}>
     */
    public static function deliveryStatusProvider(): iterable
    {
        yield 'no events falls back to sent' => [[], 'gesendet'];
        yield 'delivered' => [[MailerDeliveryEvent::DELIVERED], 'zugestellt'];
        yield 'open outranks delivered' => [[MailerDeliveryEvent::DELIVERED, MailerEngagementEvent::OPEN], 'geoeffnet'];
        yield 'click outranks open' => [[MailerEngagementEvent::OPEN, MailerEngagementEvent::CLICK], 'geklickt'];
        yield 'bounce outranks everything' => [[MailerEngagementEvent::CLICK, MailerDeliveryEvent::BOUNCE], 'bounce'];
        yield 'dropped counts as bounce' => [[MailerDeliveryEvent::DROPPED], 'bounce'];
    }

    /**
     * The bulk path the outbox list uses must agree with the entity's own reduction —
     * they are two readings of the same engagements.
     */
    public function testDeliveryStatusForMatchesTheEntityReduction(): void
    {
        $msg = new SentMessage();
        $prop = new \ReflectionProperty(SentMessage::class, 'engagements');
        $prop->getValue($msg)->add(new SentMessageEngagement($msg, MailerEngagementEvent::CLICK));

        self::assertSame($msg->getDeliveryStatus(), SentMessage::deliveryStatusFor([MailerEngagementEvent::CLICK]));
        self::assertSame('geklickt', $msg->getDeliveryStatus());
    }
}
