<?php

/*
 * 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\Impl\Timestamp;
use CpCreation\VitiCore\Marketing\Repository\EmailCampaignRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

#[ORM\Entity(repositoryClass: EmailCampaignRepository::class)]
#[ORM\Table(name: 'marketing_email_campaign')]
#[ORM\Index(name: 'marketing_email_campaign_created_idx', columns: ['created_at'])]
#[ORM\HasLifecycleCallbacks]
class EmailCampaign implements HasTimestamp
{
    use Timestamp;

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

    #[ORM\Column(length: 255)]
    private string $title;

    #[ORM\Column(length: 255)]
    private string $subject;

    #[ORM\Column(type: Types::TEXT)]
    private string $textContent;

    #[ORM\Column]
    private int $senderId;

    #[ORM\Column(type: Types::JSON)]
    private array $recipientListIds;

    #[ORM\Column(length: 255)]
    private string $utmSource;

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

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

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

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

    #[ORM\Column(nullable: true)]
    private ?int $brevoCampaignId = null;

    #[ORM\Column(length: 20, options: ['default' => 'draft'])]
    private string $status = 'draft';

    #[ORM\Column(type: Types::TEXT, nullable: true)]
    private ?string $lastError = null;

    #[ORM\Column(type: Types::DATETIMETZ_IMMUTABLE, nullable: true)]
    private ?\DateTimeImmutable $sentAt = null;

    /** @param list<int> $recipientListIds */
    public function __construct(
        string $title,
        string $subject,
        string $textContent,
        int $senderId,
        array $recipientListIds,
        string $utmSource,
        ?string $utmMedium,
        ?string $utmCampaign,
        ?string $utmTerm,
        ?string $utmContent,
    ) {
        $this->id = Uuid::uuid7();
        $this->title = mb_trim($title);
        $this->subject = mb_trim($subject);
        $this->textContent = mb_trim($textContent);
        $this->senderId = $senderId;
        $this->recipientListIds = array_values(array_unique(array_map('intval', $recipientListIds)));
        $this->utmSource = mb_trim($utmSource);
        $this->utmMedium = self::trimToNull($utmMedium);
        $this->utmCampaign = self::trimToNull($utmCampaign);
        $this->utmTerm = self::trimToNull($utmTerm);
        $this->utmContent = self::trimToNull($utmContent);
    }

    public function markCreated(int $brevoCampaignId): void
    {
        $this->brevoCampaignId = $brevoCampaignId;
        $this->status = 'ready';
        $this->lastError = null;
    }

    public function markSent(): void
    {
        $this->status = 'sent';
        $this->sentAt = new \DateTimeImmutable('now', new \DateTimeZone('UTC'));
        $this->lastError = null;
    }

    public function markFailure(string $message): void
    {
        $this->status = 'failed';
        $this->lastError = mb_substr(mb_trim($message), 0, 2000);
    }

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

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

    public function getSubject(): string
    {
        return $this->subject;
    }

    public function getTextContent(): string
    {
        return $this->textContent;
    }

    public function getSenderId(): int
    {
        return $this->senderId;
    }

    /** @return list<int> */
    public function getRecipientListIds(): array
    {
        return $this->recipientListIds;
    }

    public function getUtmSource(): string
    {
        return $this->utmSource;
    }

    public function getUtmMedium(): ?string
    {
        return $this->utmMedium;
    }

    public function getUtmCampaign(): ?string
    {
        return $this->utmCampaign;
    }

    public function getUtmTerm(): ?string
    {
        return $this->utmTerm;
    }

    public function getUtmContent(): ?string
    {
        return $this->utmContent;
    }

    public function getBrevoCampaignId(): ?int
    {
        return $this->brevoCampaignId;
    }

    public function getStatus(): string
    {
        return $this->status;
    }

    public function getLastError(): ?string
    {
        return $this->lastError;
    }

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

    private static function trimToNull(?string $value): ?string
    {
        $value = null === $value ? null : mb_trim($value);

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