<?php

declare(strict_types=1);

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\TransactionalEmail\Model;

use CpCreation\VitiCore\Behavior\HasDisable;
use CpCreation\VitiCore\Behavior\HasTimestamp;
use CpCreation\VitiCore\Behavior\Impl\Disable;
use CpCreation\VitiCore\Behavior\Impl\Timestamp;
use CpCreation\VitiCore\TransactionalEmail\Repository\TransactionalEmailRepository;
use CpCreation\VitiCore\TransactionalEmail\TransactionalEmailRecipient;
use CpCreation\VitiCore\TransactionalEmail\TransactionalEmailTrigger;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity(repositoryClass: TransactionalEmailRepository::class)]
#[ORM\Table(name: 'transactional_email')]
#[ORM\HasLifecycleCallbacks]
class TransactionalEmail implements HasTimestamp, HasDisable
{
    use Disable;
    use Timestamp;

    #[ORM\Id]
    #[ORM\Column(type: 'uuid')]
    #[ORM\GeneratedValue(strategy: 'NONE')]
    private UuidInterface $id;

    #[ORM\Column(length: 255)]
    #[Assert\NotBlank]
    private string $name = '';

    #[ORM\Column(length: 64)]
    #[Assert\NotBlank]
    private string $triggerEvent = TransactionalEmailTrigger::ORDER_PAID;

    #[ORM\Column(length: 32)]
    #[Assert\NotBlank]
    private string $recipientType = TransactionalEmailRecipient::CUSTOMER;

    #[ORM\Column(type: 'integer', options: ['default' => 0])]
    #[Assert\GreaterThanOrEqual(0)]
    private int $delayDays = 0;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $templateCode = null;

    #[ORM\Column(type: 'text', nullable: true)]
    private ?string $description = null;

    public function __construct()
    {
        $this->id = Uuid::uuid4();
        $this->disabled = false;
    }

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

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): void
    {
        $this->name = $name;
    }

    public function getTriggerEvent(): string
    {
        return $this->triggerEvent;
    }

    public function setTriggerEvent(string $triggerEvent): void
    {
        $this->triggerEvent = $triggerEvent;
    }

    public function getTriggerLabel(): string
    {
        return TransactionalEmailTrigger::label($this->triggerEvent);
    }

    public function getRecipientType(): string
    {
        return $this->recipientType;
    }

    public function setRecipientType(string $recipientType): void
    {
        $this->recipientType = $recipientType;
    }

    public function getRecipientLabel(): string
    {
        return TransactionalEmailRecipient::label($this->recipientType);
    }

    public function getDelayDays(): int
    {
        return $this->delayDays;
    }

    public function setDelayDays(int $delayDays): void
    {
        $this->delayDays = max(0, $delayDays);
    }

    public function getTemplateCode(): ?string
    {
        return $this->templateCode;
    }

    public function setTemplateCode(?string $templateCode): void
    {
        $templateCode = null !== $templateCode ? mb_trim($templateCode) : null;
        $this->templateCode = '' === $templateCode ? null : $templateCode;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(?string $description): void
    {
        $description = null !== $description ? mb_trim($description) : null;
        $this->description = '' === $description ? null : $description;
    }
}
