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

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

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

    #[Gedmo\SortablePosition]
    #[ORM\Column(type: 'integer', nullable: true)]
    private ?int $position = null;

    /**
     * @var Collection<int, SeasonImage>
     */
    #[ORM\OneToMany(
        targetEntity: SeasonImage::class,
        mappedBy: 'season',
        cascade: ['persist', 'remove'],
        orphanRemoval: true
    )]
    #[ORM\OrderBy(['position' => 'ASC'])]
    #[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): self
    {
        $this->position = $position;

        return $this;
    }

    public function addImage(SeasonImage $image): self
    {
        if (!$this->images->contains($image)) {
            $this->images->add($image);
            $image->setSeason($this);
        }

        return $this;
    }

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

            if ($image->getSeason() === $this) {
                $image->setSeason(null);
            }
        }

        return $this;
    }

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