<?php

namespace App\Entity;

use App\Repository\JoueurRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: JoueurRepository::class)]
class Joueur
{
    #[ORM\Id]
    #[ORM\Column(type: 'guid', length: 36)]
    private string $id;

    #[ORM\Column(length: 50)]
    private ?string $pseudo = null;

    /**
     * @var Collection<int, Session>
     */
    #[ORM\OneToMany(targetEntity: Session::class, mappedBy: 'joueur')]
    private Collection $sessions;

    /**
     * @var Collection<int, Territoire>
     */
    #[ORM\ManyToMany(targetEntity: Territoire::class, mappedBy: 'joueurs')]
    private Collection $territoires;

    #[ORM\OneToOne(mappedBy: 'joueur', cascade: ['persist', 'remove'])]
    private ?User $owner = null;

    public function __construct(string $uuid)
    {
        $this->id = $uuid;
        $this->sessions = new ArrayCollection();
        $this->territoires = new ArrayCollection();
    }

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

    public function getPseudo(): ?string
    {
        return $this->pseudo;
    }

    public function setPseudo(string $pseudo): static
    {
        $this->pseudo = $pseudo;

        return $this;
    }

    /**
     * @return Collection<int, Session>
     */
    public function getSessions(): Collection
    {
        return $this->sessions;
    }

    public function addSession(Session $session): static
    {
        if (!$this->sessions->contains($session)) {
            $this->sessions->add($session);
            $session->setJoueur($this);
        }

        return $this;
    }

    public function removeSession(Session $session): static
    {
        if ($this->sessions->removeElement($session)) {
            // set the owning side to null (unless already changed)
            if ($session->getJoueur() === $this) {
                $session->setJoueur(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, Territoire>
     */
    public function getTerritoires(): Collection
    {
        return $this->territoires;
    }

    public function addTerritoire(Territoire $territoire): static
    {
        if (!$this->territoires->contains($territoire)) {
            $this->territoires->add($territoire);
            $territoire->addJoueur($this);
        }

        return $this;
    }

    public function removeTerritoire(Territoire $territoire): static
    {
        if ($this->territoires->removeElement($territoire)) {
            $territoire->removeJoueur($this);
        }

        return $this;
    }

    public function getOwner(): ?User
    {
        return $this->owner;
    }

    public function setOwner(?User $owner): static
    {
        // unset the owning side of the relation if necessary
        if ($owner === null && $this->owner !== null) {
            $this->owner->setJoueur(null);
        }

        // set the owning side of the relation if necessary
        if ($owner !== null && $owner->getJoueur() !== $this) {
            $owner->setJoueur($this);
        }

        $this->owner = $owner;

        return $this;
    }
}
