<?php

declare(strict_types=1);

namespace Ferienpass\CoreBundle\Tests\Entity;

use Ferienpass\CoreBundle\Entity\MessengerLog;
use Ferienpass\CoreBundle\Entity\SentMessage;
use Ferienpass\CoreBundle\Entity\SentMessageEngagement;
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());
    }
}
