<?php

declare(strict_types=1);

namespace PhpList\Core\Domain\Messaging\Model;

use DateTime;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
use PhpList\Core\Domain\Common\Model\Interfaces\DomainModel;
use PhpList\Core\Domain\Common\Model\Interfaces\Identity;
use PhpList\Core\Domain\Common\Model\Interfaces\ModificationDate;
use PhpList\Core\Domain\Messaging\Repository\ListMessageRepository;
use PhpList\Core\Domain\Subscription\Model\SubscriberList;

#[ORM\Entity(repositoryClass: ListMessageRepository::class)]
#[ORM\Table(name: 'listmessage')]
#[ORM\UniqueConstraint(name: 'phplist_listmessage_messageid', columns: ['messageid', 'listid'])]
#[ORM\Index(name: 'phplist_listmessage_listmessageidx', columns: ['listid', 'messageid'])]
#[ORM\HasLifecycleCallbacks]
class ListMessage implements DomainModel, Identity, ModificationDate
{
    #[ORM\Id]
    #[ORM\Column(type: 'integer')]
    #[ORM\GeneratedValue]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: Message::class, inversedBy: 'listMessages')]
    #[ORM\JoinColumn(name: 'messageid', referencedColumnName: 'id', nullable: false)]
    private Message $message;

    #[ORM\ManyToOne(targetEntity: SubscriberList::class, inversedBy: 'listMessages')]
    #[ORM\JoinColumn(name: 'listid', referencedColumnName: 'id', nullable: false)]
    private SubscriberList $subscriberList;

    #[ORM\Column(name: 'entered', type: 'datetime', nullable: true)]
    private ?DateTimeInterface $entered = null;

    #[ORM\Column(name: 'modified', type: 'datetime')]
    private ?DateTime $updatedAt = null;

    public function __construct(Message $message, SubscriberList $subscriberList)
    {
        $this->message = $message;
        $this->subscriberList = $subscriberList;
        $this->updatedAt = new DateTime();
        $this->entered = new DateTime();
    }

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

    public function getMessage(): Message
    {
        return $this->message;
    }

    public function getList(): SubscriberList
    {
        return $this->subscriberList;
    }

    public function getEntered(): ?DateTimeInterface
    {
        return $this->entered;
    }

    public function getUpdatedAt(): ?DateTime
    {
        return $this->updatedAt;
    }

    #[ORM\PreUpdate]
    public function updateUpdatedAt(): void
    {
        $this->updatedAt = new DateTime();
    }
}
