<?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\Marketing\Model;

use CpCreation\VitiCore\Behavior\HasTimestamp;
use CpCreation\VitiCore\Behavior\HasTranslatable;
use CpCreation\VitiCore\Behavior\Impl\Timestamp;
use CpCreation\VitiCore\Behavior\Impl\Translatable;
use CpCreation\VitiCore\Marketing\Model\Translation\AbandonedCartConfigurationTranslation;
use CpCreation\VitiCore\Marketing\Repository\AbandonedCartConfigurationRepository;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

#[ORM\Entity(repositoryClass: AbandonedCartConfigurationRepository::class)]
#[ORM\Table(name: 'marketing_abandoned_cart_configuration')]
#[ORM\HasLifecycleCallbacks]
class AbandonedCartConfiguration implements HasTimestamp, HasTranslatable
{
    use Timestamp;
    use Translatable;

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

    #[ORM\Column(type: 'boolean', options: ['default' => false])]
    private bool $enabled = false;

    #[ORM\Column(type: 'datetime_immutable', nullable: true)]
    private ?\DateTimeImmutable $enabledAt = null;

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

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

    public function isEnabled(): bool
    {
        return $this->enabled;
    }

    public function setEnabled(bool $enabled): void
    {
        if ($enabled && !$this->enabled) {
            $this->enabledAt = new \DateTimeImmutable();
        }

        $this->enabled = $enabled;
    }

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

    public function getEmailSubjectForLocale(string $locale): ?string
    {
        foreach ($this->getTranslationsForLocale($locale) as $translation) {
            if (null !== $subject = $translation->getEmailSubject()) {
                return $subject;
            }
        }

        return null;
    }

    public function getEmailTitleForLocale(string $locale): ?string
    {
        foreach ($this->getTranslationsForLocale($locale) as $translation) {
            if (null !== $title = $translation->getEmailTitle()) {
                return $title;
            }
        }

        return null;
    }

    public function getEmailPreheaderForLocale(string $locale): ?string
    {
        foreach ($this->getTranslationsForLocale($locale) as $translation) {
            if (null !== $preheader = $translation->getEmailPreheader()) {
                return $preheader;
            }
        }

        return null;
    }

    /**
     * @return \Generator<AbandonedCartConfigurationTranslation>
     */
    private function getTranslationsForLocale(string $locale): \Generator
    {
        $locale = mb_strtolower(str_replace('_', '-', mb_trim($locale)));
        $locales = array_values(array_unique(array_filter([
            $locale,
            explode('-', $locale, 2)[0] ?? null,
        ])));

        foreach ($locales as $candidate) {
            $translation = $this->translate($candidate, false);
            if ($translation instanceof AbandonedCartConfigurationTranslation) {
                yield $translation;
            }
        }
    }
}
