<?php

namespace DMD\LaLigaApi\Entity;

use DMD\LaLigaApi\Repository\TeamRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: TeamRepository::class)]
#[ORM\HasLifecycleCallbacks]
class Team
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $name = null;

    #[ORM\Column(nullable: true)]
    private ?bool $active = null;

    #[ORM\ManyToMany(targetEntity: Season::class, mappedBy: 'teams')]
    private Collection $seasons;

    #[ORM\OneToMany(mappedBy: 'team', targetEntity: SeasonData::class)]
    private Collection $seasonData;

    #[ORM\OneToMany(mappedBy: 'team', targetEntity: Player::class)]
    private Collection $players;

    #[ORM\Column(nullable: true)]
    private ?\DateTimeImmutable $createdAt = null;

    #[ORM\OneToOne(inversedBy: 'team', cascade: ['persist', 'remove'])]
    private ?User $captain = null;

    #[ORM\ManyToOne(inversedBy: 'teams')]
    private ?Facility $homeFacility = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $dayOfTheWeekForHomeGame = null;

    #[ORM\Column(nullable: true)]
    private ?int $leagueId = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $teamLogo = null;

    public function __construct()
    {
        $this->seasons = new ArrayCollection();
        $this->seasonData = new ArrayCollection();
        $this->players = new ArrayCollection();
    }

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

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

    public function setName(?string $name): static
    {
        $this->name = $name;

        return $this;
    }

    public function isActive(): ?bool
    {
        return $this->active;
    }

    public function setActive(?bool $active): static
    {
        $this->active = $active;

        return $this;
    }

    public function getCreatedAt(): ?\DateTimeImmutable
    {
        return $this->createdAt;
    }

    #[ORM\PrePersist]
    public function setCreatedAt(): void
    {
        $this->createdAt = new \DateTimeImmutable();
    }

    /**
     * @return Collection<int, Season>
     */
    public function getSeasons(): Collection
    {
        return $this->seasons;
    }

    public function addSeason(Season $season): static
    {
        if (!$this->seasons->contains($season)) {
            $this->seasons->add($season);
            $season->addTeam($this);
        }

        return $this;
    }

    public function removeSeason(Season $season): static
    {
        if ($this->seasons->removeElement($season)) {
            $season->removeTeam($this);
        }

        return $this;
    }


    /**
     * @return Collection<int, SeasonData>
     */
    public function getSeasonData(): Collection
    {
        return $this->seasonData;
    }

    public function addSeasonData(SeasonData $seasonData): static
    {
        if (!$this->seasonData->contains($seasonData)) {
            $this->seasonData->add($seasonData);
            $seasonData->setTeam($this);
        }

        return $this;
    }

    public function removeSeasonData(SeasonData $seasonData): static
    {
        if ($this->seasonData->removeElement($seasonData)) {
            // set the owning side to null (unless already changed)
            if ($seasonData->getTeam() === $this) {
                $seasonData->setTeam(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, Player>
     */
    public function getPlayers(): Collection
    {
        return $this->players;
    }

    public function addPlayer(Player $player): static
    {
        if (!$this->players->contains($player)) {
            $this->players->add($player);
            $player->setTeam($this);
        }

        return $this;
    }

    public function removePlayer(Player $player): static
    {
        if ($this->players->removeElement($player)) {
            // set the owning side to null (unless already changed)
            if ($player->getTeam() === $this) {
                $player->setTeam(null);
            }
        }

        return $this;
    }

    public function getCaptain(): ?User
    {
        return $this->captain;
    }

    public function setCaptain(?User $captain): static
    {
        $this->captain = $captain;

        return $this;
    }

    public function getHomeFacility(): ?Facility
    {
        return $this->homeFacility;
    }

    public function setHomeFacility(?Facility $homeFacility): static
    {
        $this->homeFacility = $homeFacility;

        return $this;
    }

    public function getDayOfTheWeekForHomeGame(): ?string
    {
        return $this->dayOfTheWeekForHomeGame;
    }

    public function setDayOfTheWeekForHomeGame(?string $dayOfTheWeekForHomeGame): static
    {
        $this->dayOfTheWeekForHomeGame = $dayOfTheWeekForHomeGame;

        return $this;
    }

    public function getLeagueId(): ?int
    {
        return $this->leagueId;
    }

    public function setLeagueId(?int $leagueId): static
    {
        $this->leagueId = $leagueId;

        return $this;
    }

    public function getTeamLogo(): ?string
    {
        return $this->teamLogo;
    }

    public function setTeamLogo(?string $teamLogo): static
    {
        $this->teamLogo = $teamLogo;

        return $this;
    }

}
