<?php

namespace App\Entity;

use App\Repository\EmailTrackingRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: EmailTrackingRepository::class)]
class EmailTracking
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 64, unique: true)]
    private ?string $messageId = null;

    #[ORM\Column(length: 255)]
    private ?string $recipient = null;

    #[ORM\Column(length: 255)]
    private ?string $subject = null;

    #[ORM\Column(length: 10)]
    private ?string $state = null;

    #[ORM\Column]
    private ?\DateTimeImmutable $sentAt = null;

    #[ORM\Column(nullable: true)]
    private ?\DateTimeImmutable $openedAt = null;

    public function __construct(string $messageId, string $recipient, string $subject)
    {
        $this->messageId = $messageId;
        $this->recipient = $recipient;
        $this->subject = $subject;
        $this->state = 'sent';
        $this->sentAt = new \DateTimeImmutable();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getMessageId(): ?string
    {
        return $this->messageId;
    }

    public function getRecipient(): ?string
    {
        return $this->recipient;
    }

    public function getSubject(): ?string
    {
        return $this->subject;
    }

    public function getState(): ?string
    {
        return $this->state;
    }

    public function getSentAt(): ?\DateTimeImmutable
    {
        return $this->sentAt;
    }

    public function getOpenedAt(): ?\DateTimeImmutable
    {
        return $this->openedAt;
    }

    public function markAsOpened(): void
    {
        if ('sent' === $this->state) {
            $this->state = 'opened';
            $this->openedAt = new \DateTimeImmutable();
        }
    }
}
