<?php

/*
 * This file is part of the VitiCore 2.
 *
 * (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\History\Model;

use CpCreation\VitiCore\Behavior\HasDisable;
use CpCreation\VitiCore\Behavior\HasFile;
use CpCreation\VitiCore\Behavior\HasTimestamp;
use CpCreation\VitiCore\Behavior\HasTranslatable;
use CpCreation\VitiCore\Behavior\HasVideo;
use CpCreation\VitiCore\Behavior\Impl\Disable;
use CpCreation\VitiCore\Behavior\Impl\File;
use CpCreation\VitiCore\Behavior\Impl\Timestamp;
use CpCreation\VitiCore\Behavior\Impl\Translatable;
use CpCreation\VitiCore\Behavior\Impl\Video;
use CpCreation\VitiCore\History\Repository\HistoryRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity(repositoryClass: HistoryRepository::class)]
#[ORM\Table(name: 'history_history')]
#[ORM\HasLifecycleCallbacks]
class History implements HasTimestamp, HasDisable, HasVideo, HasFile, HasTranslatable
{
    use Disable;
    use File;
    use Timestamp;
    use Translatable;
    use Video;

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

    #[ORM\Column(type: 'integer')]
    #[Gedmo\SortablePosition]
    private int $position;

    /**
     * @var Collection<int, HistoryImage>
     */
    #[ORM\OneToMany(targetEntity: HistoryImage::class, cascade: ['persist', 'remove'], mappedBy: 'history', orphanRemoval: true)]
    #[ORM\OrderBy(['position' => 'ASC'])]
    #[Assert\Valid]
    private Collection $images;

    public function __construct()
    {
        $this->id = Uuid::uuid4();
        $this->disabled = 0;
        $this->images = new ArrayCollection();
    }

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

    public function getPosition(): int
    {
        return $this->position;
    }

    public function setPosition(int $position): void
    {
        $this->position = $position;
    }

    public function addImage(HistoryImage $image): self
    {
        if (!$this->images->contains($image)) {
            $this->images[] = $image;
            $image->setHistory($this);
        }

        return $this;
    }

    public function removeImage(HistoryImage $image): self
    {
        if ($this->images->contains($image)) {
            $this->images->removeElement($image);

            if ($image->getHistory() === $this) {
                $image->setHistory(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, HistoryImage>
     */
    public function getImages(): Collection
    {
        return $this->images;
    }

    #[ORM\PrePersist]
    #[ORM\PreUpdate]
    public function imagesUpdate(): void
    {
        foreach ($this->getImages() as $image) {
            $image->setHistory($this);
        }
    }
}
