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

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\File;
use CpCreation\VitiCore\Behavior\Impl\Timestamp;
use CpCreation\VitiCore\Behavior\Impl\Translatable;
use CpCreation\VitiCore\Behavior\Impl\Video;
use CpCreation\VitiCore\Gite\Repository\RoomRepository;
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: RoomRepository::class)]
#[ORM\Table(name: 'gite_room')]
#[ORM\HasLifecycleCallbacks]
class Room implements HasTimestamp, HasVideo, HasFile, HasTranslatable
{
    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;

    #[ORM\ManyToOne(targetEntity: Gite::class, inversedBy: 'rooms')]
    #[ORM\JoinColumn(name: 'gite_id', referencedColumnName: 'id', onDelete: 'cascade')]
    private ?Gite $gite = null;

    /**
     * @var Collection<int, RoomImage>
     */
    #[ORM\OneToMany(targetEntity: RoomImage::class, cascade: ['persist', 'remove'], mappedBy: 'room', orphanRemoval: true)]
    #[ORM\OrderBy(['position' => 'ASC'])]
    #[Assert\Count(min: 1, minMessage: 'Vous devez ajouter au moins une image')]
    #[Assert\Valid]
    private Collection $images;

    public function __construct()
    {
        $this->id = Uuid::uuid4();
        $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 getGite(): ?Gite
    {
        return $this->gite;
    }

    public function setGite(?Gite $gite): void
    {
        $this->gite = $gite;
    }

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

        return $this;
    }

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

            if ($image->getRoom() === $this) {
                $image->setRoom(null);
            }
        }

        return $this;
    }

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