<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\PageBuilder\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;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

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

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

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

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

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

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

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

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

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

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

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

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

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

    public function getEyebrow(): ?string
    {
        return $this->eyebrow;
    }

    public function setEyebrow(?string $eyebrow): void
    {
        $this->eyebrow = $eyebrow;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(?string $title): void
    {
        $this->title = $title;
    }

    public function getBody(): ?string
    {
        return $this->body;
    }

    public function setBody(?string $body): void
    {
        $this->body = $body;
    }

    public function getPrimaryCtaLabel(): ?string
    {
        return $this->primaryCtaLabel;
    }

    public function setPrimaryCtaLabel(?string $primaryCtaLabel): void
    {
        $this->primaryCtaLabel = $primaryCtaLabel;
    }

    public function getPrimaryCtaUrl(): ?string
    {
        return $this->primaryCtaUrl;
    }

    public function setPrimaryCtaUrl(?string $primaryCtaUrl): void
    {
        $this->primaryCtaUrl = $primaryCtaUrl;
    }

    public function getSecondaryCtaLabel(): ?string
    {
        return $this->secondaryCtaLabel;
    }

    public function setSecondaryCtaLabel(?string $secondaryCtaLabel): void
    {
        $this->secondaryCtaLabel = $secondaryCtaLabel;
    }

    public function getSecondaryCtaUrl(): ?string
    {
        return $this->secondaryCtaUrl;
    }

    public function setSecondaryCtaUrl(?string $secondaryCtaUrl): void
    {
        $this->secondaryCtaUrl = $secondaryCtaUrl;
    }

    public function getQuoteSource(): ?string
    {
        return $this->quoteSource;
    }

    public function setQuoteSource(?string $quoteSource): void
    {
        $this->quoteSource = $quoteSource;
    }

    public function getQuoteRole(): ?string
    {
        return $this->quoteRole;
    }

    public function setQuoteRole(?string $quoteRole): void
    {
        $this->quoteRole = $quoteRole;
    }

    #[Assert\Callback]
    public function validatePrimaryCta(ExecutionContextInterface $context): void
    {
        $this->validateCtaPair('primaryCtaLabel', 'primaryCtaUrl', 'principal', $context);
    }

    #[Assert\Callback]
    public function validateSecondaryCta(ExecutionContextInterface $context): void
    {
        $this->validateCtaPair('secondaryCtaLabel', 'secondaryCtaUrl', 'secondaire', $context);
    }

    private function validateCtaPair(
        string $labelField,
        string $urlField,
        string $label,
        ExecutionContextInterface $context,
    ): void {
        $buttonLabel = trim((string) $this->{$labelField});
        $buttonUrl = trim((string) $this->{$urlField});

        if ('' === $buttonLabel && '' === $buttonUrl) {
            return;
        }

        if ('' === $buttonLabel) {
            $context->buildViolation(sprintf('Le libellé du CTA %s est obligatoire lorsqu’une URL est renseignée.', $label))
                ->atPath($labelField)
                ->addViolation();
        }

        if ('' === $buttonUrl) {
            $context->buildViolation(sprintf('L’URL du CTA %s est obligatoire lorsqu’un libellé est renseigné.', $label))
                ->atPath($urlField)
                ->addViolation();
        }
    }
}
