<?php
declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use App\Enum\CelestialBodySizeEnum;
use App\Enum\StarTypeEnum;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;


#[ORM\Entity]
#[ApiResource]
class System
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 100, nullable: false)]
    private string $name;

    /** @var Collection<int, Planet> */
    #[ORM\OneToMany(targetEntity: Planet::class, mappedBy: 'system', orphanRemoval: true)]
    private Collection $planets;

    #[ORM\ManyToOne(targetEntity: Sector::class, inversedBy: 'systems')]
    #[ORM\JoinColumn(nullable: true)]
    private ?Sector $sector = null;

    #[ORM\Column(length: 20, enumType: StarTypeEnum::class, options: ['default' => StarTypeEnum::RED_DWARF])]
    private StarTypeEnum $starType = StarTypeEnum::RED_DWARF;

    #[ORM\Column(length: 3, enumType: CelestialBodySizeEnum::class, options: ['default' => CelestialBodySizeEnum::M])]
    private CelestialBodySizeEnum $starSize = CelestialBodySizeEnum::M;

    #[ORM\Column(nullable: true)]
    #[Assert\Range(notInRangeMessage: 'between_{{ min }}_and_{{ max }}', min: 0, max: 7)]
    private int $asteroidBeltPosition = 0;

    public function __construct()
    {
        $this->planets = new ArrayCollection();
    }

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

    public function setId(?int $id): self
    {
        $this->id = $id;
        return $this;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;
        return $this;
    }

    public function getPlanets(): Collection
    {
        return $this->planets;
    }

    public function setPlanets(Collection $planets): self
    {
        $this->planets = $planets;
        return $this;
    }

    public function addPlanet(Planet $planet): self
    {
        if (!$this->planets->contains($planet)) {
            $this->planets->add($planet);
            $planet->setSystem($this);
        }
        return $this;
    }

    public function removePlanet(Planet $planet): self
    {
        if ($this->planets->removeElement($planet)) {
            if ($planet->getSystem() === $this) {
                $planet->setSystem(null);
            }
        }
        return $this;
    }

    public function getSector(): ?Sector
    {
        return $this->sector;
    }

    public function setSector(?Sector $sector): self
    {
        $this->sector = $sector;
        if ($sector && !$sector->getSystems()->contains($this)) {
            $sector->addSystem($this);
        }
        return $this;
    }

    public function getStarType(): StarTypeEnum
    {
        return $this->starType;
    }

    public function setStarType(StarTypeEnum $starType): self
    {
        $this->starType = $starType;
        return $this;
    }

    public function getStarSize(): CelestialBodySizeEnum
    {
        return $this->starSize;
    }

    public function setStarSize(CelestialBodySizeEnum $starSize): self
    {
        $this->starSize = $starSize;
        return $this;
    }

    public function getAsteroidBeltPosition(): int
    {
        return $this->asteroidBeltPosition;
    }

    public function setAsteroidBeltPosition(int $asteroidBeltPosition): self
    {
        $this->asteroidBeltPosition = $asteroidBeltPosition;
        return $this;
    }
}