<?php

namespace App\Tests\Entity;

use App\Entity\EmailTracking;
use PHPUnit\Framework\TestCase;

class EmailTrackingTest extends TestCase
{
    public function testConstructor(): void
    {
        $t = new EmailTracking('msg-123', 'to@test.com', 'Subject', '<h1>Hi</h1>', [['path' => '/f.pdf', 'name' => 'f.pdf']]);
        $this->assertNull($t->getId());
        $this->assertSame('msg-123', $t->getMessageId());
        $this->assertSame('to@test.com', $t->getRecipient());
        $this->assertSame('Subject', $t->getSubject());
        $this->assertSame('<h1>Hi</h1>', $t->getHtmlBody());
        $this->assertCount(1, $t->getAttachments());
        $this->assertSame('sent', $t->getState());
        $this->assertInstanceOf(\DateTimeImmutable::class, $t->getSentAt());
        $this->assertNull($t->getOpenedAt());
    }

    public function testConstructorWithoutOptionals(): void
    {
        $t = new EmailTracking('msg-456', 'to@test.com', 'Test');
        $this->assertNull($t->getHtmlBody());
        $this->assertNull($t->getAttachments());
    }

    public function testMarkAsOpened(): void
    {
        $t = new EmailTracking('msg-789', 'to@test.com', 'Test');
        $this->assertNull($t->getOpenedAt());

        $t->markAsOpened();
        $this->assertSame('opened', $t->getState());
        $this->assertInstanceOf(\DateTimeImmutable::class, $t->getOpenedAt());
    }

    public function testMarkAsOpenedOnlyOnce(): void
    {
        $t = new EmailTracking('msg-abc', 'to@test.com', 'Test');
        $t->markAsOpened();
        $firstOpenedAt = $t->getOpenedAt();

        $t->markAsOpened();
        $this->assertSame($firstOpenedAt, $t->getOpenedAt());
    }
}
