<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Marketing\Model\Translation;

use CpCreation\VitiCore\Behavior\Impl\Translation;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity]
#[ORM\Table(name: 'marketing_abandoned_cart_config_translation')]
class AbandonedCartConfigurationTranslation implements TranslationInterface
{
    use Translation;

    public const int TITLE_MAX_LENGTH = 150;
    public const int SUBJECT_MAX_LENGTH = 150;
    public const int PREHEADER_MAX_LENGTH = 200;

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

    #[Assert\Length(max: self::TITLE_MAX_LENGTH)]
    #[ORM\Column(type: 'string', length: self::TITLE_MAX_LENGTH, nullable: true)]
    private ?string $emailTitle = null;

    #[Assert\Length(max: self::SUBJECT_MAX_LENGTH)]
    #[ORM\Column(type: 'string', length: self::SUBJECT_MAX_LENGTH, nullable: true)]
    private ?string $emailSubject = null;

    #[Assert\Length(max: self::PREHEADER_MAX_LENGTH)]
    #[ORM\Column(type: 'string', length: self::PREHEADER_MAX_LENGTH, nullable: true)]
    private ?string $emailPreheader = null;

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

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

    public function getEmailSubject(): ?string
    {
        return $this->emailSubject;
    }

    public function getEmailTitle(): ?string
    {
        return $this->emailTitle;
    }

    public function setEmailTitle(?string $emailTitle): void
    {
        $this->emailTitle = $this->normalizeSingleLine($emailTitle);
    }

    public function setEmailSubject(?string $emailSubject): void
    {
        $this->emailSubject = $this->normalizeSingleLine($emailSubject);
    }

    public function getEmailPreheader(): ?string
    {
        return $this->emailPreheader;
    }

    public function setEmailPreheader(?string $emailPreheader): void
    {
        $this->emailPreheader = $this->normalizeSingleLine($emailPreheader);
    }

    private function normalizeSingleLine(?string $value): ?string
    {
        if (null === $value) {
            return null;
        }

        $value = preg_replace('/\s+/u', ' ', mb_trim($value)) ?? '';

        return '' !== $value ? $value : null;
    }
}
