<?php

namespace App\Entity;

use App\Repository\PartyRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: PartyRepository::class)]
class Party
{
    #[ORM\Id]
    #[ORM\Column(type: UuidType::NAME, nullable: true)]
    #[ORM\GeneratedValue(strategy: 'CUSTOM')]
    #[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
    private ?Uuid $id = null;

    #[ORM\ManyToOne(inversedBy: 'parties')]
    private ?Gamemaster $gamemaster = null;

    #[ORM\ManyToOne(inversedBy: 'parties')]
    private ?Game $game = null;

    /**
     * @var Collection<int, Slot>
     */
    #[ORM\OneToMany(targetEntity: Slot::class, mappedBy: 'party')]
    #[ORM\OrderBy(["startOn" => "ASC"])]
    private Collection $slots;

    #[ORM\ManyToOne(inversedBy: 'parties')]
    #[ORM\JoinColumn(nullable: false)]
    private ?Event $event = null;

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

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

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

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

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

    #[ORM\Column(type: Types::TEXT, nullable: true)]
    private ?string $description = null;

    #[ORM\ManyToOne(inversedBy: 'submittedParties')]
    private ?User $submitter = null;

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

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

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

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

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

    public function setId(Uuid $id): static
    {
        $this->id = $id;

        return $this;
    }

    public function getGamemaster(): ?Gamemaster
    {
        return $this->gamemaster;
    }

    public function setGamemaster(?Gamemaster $gamemaster): static
    {
        $this->gamemaster = $gamemaster;

        return $this;
    }

    public function getGame(): ?Game
    {
        return $this->game;
    }

    public function setGame(?Game $game): static
    {
        $this->game = $game;

        return $this;
    }

    /**
     * @return Collection<int, Slot>
     */
    public function getSlots(): Collection
    {
        return $this->slots;
    }

    public function addSlot(Slot $slot): static
    {
        if (!$this->slots->contains($slot)) {
            $this->slots->add($slot);
            $slot->setParty($this);
        }

        return $this;
    }

    public function removeSlot(Slot $slot): static
    {
        if ($this->slots->removeElement($slot)) {
            // set the owning side to null (unless already changed)
            if ($slot->getParty() === $this) {
                $slot->setParty(null);
            }
        }

        return $this;
    }

    public function purgeSlots(): static
    {
        foreach ($this->getSlots() as $slot) {
            $this->removeSlot($slot);
        }
        return $this;
    }

    public function getEvent(): ?Event
    {
        return $this->event;
    }

    public function setEvent(?Event $event): static
    {
        $this->event = $event;

        return $this;
    }

    public function getStartOn(): ?\DateTime
    {
        return $this->startOn;
    }

    public function setStartOn(?\DateTime $startOn): static
    {
        $this->startOn = $startOn;

        return $this;
    }

    public function getEndOn(): ?\DateTime
    {
        return $this->endOn;
    }

    public function setEndOn(?\DateTime $endOn): static
    {
        $this->endOn = $endOn;

        return $this;
    }

    public function isGamemasterIsAuthor(): ?bool
    {
        return $this->gamemasterIsAuthor;
    }

    public function setGamemasterIsAuthor(?bool $gamemasterIsAuthor): static
    {
        $this->gamemasterIsAuthor = $gamemasterIsAuthor;

        return $this;
    }

    public function getMinParticipants(): ?int
    {
        return $this->minParticipants;
    }

    public function setMinParticipants(?int $minParticipants): static
    {
        $this->minParticipants = $minParticipants;

        return $this;
    }

    public function getMaxParticipants(): ?int
    {
        return $this->maxParticipants;
    }

    public function setMaxParticipants(?int $maxParticipants): static
    {
        $this->maxParticipants = $maxParticipants;

        return $this;
    }

    public function getSeatsOccuped(): ?int 
    {
        $seatsOccuped = count($this->getParticipations());
        return $seatsOccuped;
    }

    public function getSeatsLeft(): ?int
    {
        $seatsLeft = $this->maxParticipants - count($this->getParticipations());
        return $seatsLeft;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(?string $description): static
    {
        $this->description = $description;

        return $this;
    }

    public function getSubmitter(): ?User
    {
        return $this->submitter;
    }

    public function setSubmitter(?User $submitter): static
    {
        $this->submitter = $submitter;

        return $this;
    }

    public function getSubmittedDate(): ?\DateTime
    {
        return $this->submittedDate;
    }

    public function setSubmittedDate(?\DateTime $submittedDate): static
    {
        $this->submittedDate = $submittedDate;

        return $this;
    }

    public function isValidated(): ?bool
    {
        return $this->validated;
    }

    public function setValidated(?bool $validated): static
    {
        $this->validated = $validated;

        return $this;
    }

    /**
     * @return Collection<int, Participation>
     */
    public function getParticipations(): Collection
    {
        return $this->participations;
    }

    public function addParticipation(Participation $participation): static
    {
        if (!$this->participations->contains($participation)) {
            $this->participations->add($participation);
            $participation->setParty($this);
        }

        return $this;
    }

    public function removeParticipation(Participation $participation): static
    {
        if ($this->participations->removeElement($participation)) {
            // set the owning side to null (unless already changed)
            if ($participation->getParty() === $this) {
                $participation->setParty(null);
            }
        }

        return $this;
    }
}
