<?php
declare (strict_types=1);

namespace App\Entity\Booking;

use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use App\Entity\Access\Access;
use App\Entity\Core\Categories;
use App\Entity\Core\File;
use App\Entity\Core\Tagg;
use App\Entity\Organization\Organization;
use App\Entity\Place\Place;
use App\Entity\Place\PlaceSystem;
use App\Entity\Place\Room;
//use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Auditable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @todo : A la suite de la migration, il faut supprimer le nom de la table pour avoir une table Event, et supprimer l'attribut discr.
 * @todo : migration table tag_booking
 */
#[ApiResource(
    operations: [
        new Get(
            security: 'is_granted(\'ROLE_ADMIN\') and object.getOrganization().getId() == user.getOrganization().getId()'
        )
    ]
)]
//#[Auditable]
#[ORM\Entity]
#[ORM\Table(name: 'Booking')]
class Event extends AbstractBooking
{
    #[ORM\Column(length: 255, nullable: false)]
    private string $discr = 'event';

    #[ORM\ManyToOne(inversedBy: 'events')]
    #[ORM\JoinColumn(nullable: false)]
    protected Organization $organization;

    #[ORM\ManyToOne(inversedBy: 'events')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    protected Place $place;

    #[ORM\ManyToOne(inversedBy: 'events')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    protected Room $room;

    #[ORM\OneToMany(mappedBy: 'event', targetEntity: EventRecur::class, cascade: ['persist'], orphanRemoval: true)]
    private Collection $eventRecur;

    #[ORM\OneToMany(mappedBy: 'parent', targetEntity: Event::class, orphanRemoval: true)]
    private Collection $timeline;

    #[ORM\ManyToOne(inversedBy: 'timeline')]
    private Event $parent;

    #[Assert\Valid]
    #[ORM\ManyToOne(cascade: ['persist'], inversedBy: 'events')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    private File $image;

    #[ORM\ManyToOne]
    private EventGender $gender;

    #[Assert\Valid]
    #[ORM\OneToMany(mappedBy: 'event', targetEntity: EventUser::class, cascade: ['persist'], orphanRemoval: true)]
    private Collection $eventUser;

    #[ORM\ManyToMany(targetEntity: Categories::class, cascade: ['persist'])]
    private Collection $categories;

    #[ORM\OneToMany(mappedBy: 'event', targetEntity: EventReport::class, orphanRemoval: true)]
    private Collection $eventReports;

    #[ORM\ManyToMany(targetEntity: File::class, cascade: ['persist'], orphanRemoval: true)]
    #[ORM\JoinTable(name: 'event_files')]
    #[ORM\JoinColumn(name: 'event_id', referencedColumnName: 'id', onDelete: 'cascade')]
    #[ORM\InverseJoinColumn(name: 'file_id', referencedColumnName: 'id', onDelete: 'cascade')]
    private Collection $files;

    #[ORM\ManyToOne]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    private PlaceSystem $placeSystem;

    #[ORM\OneToMany(mappedBy: 'event', targetEntity: AttendanceBooking::class, cascade: ['persist'], orphanRemoval: true)]
    #[ORM\JoinColumn(nullable: false)]
    private Collection $attendanceBooking;

    #[ORM\ManyToMany(targetEntity: Access::class, inversedBy: 'eventOrganizers')]
    #[ORM\JoinTable(name: 'booking_organizer')]
    #[ORM\JoinColumn(name: 'booking_id', referencedColumnName: 'id')]
    #[ORM\InverseJoinColumn(name: 'organizer_id', referencedColumnName: 'id')]
    private Collection $organizer;

    #[ORM\ManyToMany(targetEntity: Tagg::class, inversedBy: 'events', cascade: ['persist'])]
    #[ORM\JoinTable(name: 'tag_booking')]
    #[ORM\JoinColumn(name: 'booking_id', referencedColumnName: 'id')]
    #[ORM\InverseJoinColumn(name: 'tag_id', referencedColumnName: 'id')]
    private Collection $tags;

    public function __construct()
    {
        $this->eventRecur = new ArrayCollection();
        $this->timeline = new ArrayCollection();
        $this->eventUser = new ArrayCollection();
        $this->categories = new ArrayCollection();
        $this->eventReports = new ArrayCollection();
        $this->files = new ArrayCollection();
        $this->attendanceBooking = new ArrayCollection();
        $this->organizer = new ArrayCollection();
        $this->tags = new ArrayCollection();
    }

    public function getDiscr(): ?string
    {
        return $this->discr;
    }

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

    public function getOrganization(): ?Organization
    {
        return $this->organization;
    }

    public function setOrganization(?Organization $organization): self
    {
        $this->organization = $organization;
        return $this;
    }

    public function getPlace(): ?Place
    {
        return $this->place;
    }

    public function setPlace(?Place $place): self
    {
        $this->place = $place;
        return $this;
    }

    public function getRoom(): ?Room
    {
        return $this->room;
    }

    public function setRoom(?Room $room): self
    {
        $this->room = $room;
        return $this;
    }

    /**
     * @return Collection<int, EventRecur>
     */
    public function getEventRecur(): Collection
    {
        return $this->eventRecur;
    }

    public function addEventRecur(EventRecur $eventRecur): self
    {
        if (!$this->eventRecur->contains($eventRecur)) {
            $this->eventRecur[] = $eventRecur;
            $eventRecur->setEvent($this);
        }
        return $this;
    }

    public function removeEventRecur(EventRecur $eventRecur): self
    {
        if ($this->eventRecur->removeElement($eventRecur)) {
            // set the owning side to null (unless already changed)
            if ($eventRecur->getEvent() === $this) {
                $eventRecur->setEvent(null);
            }
        }
        return $this;
    }

    /**
     * @return Collection<int, Event>
     */
    public function getTimeline(): Collection
    {
        return $this->timeline;
    }

    public function addTimeline(Event $timeline): self
    {
        if (!$this->timeline->contains($timeline)) {
            $this->timeline[] = $timeline;
            $timeline->setParent($this);
        }
        return $this;
    }

    public function removeTimeline(Event $timeline): self
    {
        if ($this->timeline->removeElement($timeline)) {
            // set the owning side to null (unless already changed)
            if ($timeline->getParent() === $this) {
                $timeline->setParent(null);
            }
        }
        return $this;
    }

    public function getParent(): ?self
    {
        return $this->parent;
    }

    public function setParent(?self $parent): self
    {
        $this->parent = $parent;
        return $this;
    }

    public function getImage(): ?File
    {
        return $this->image;
    }

    public function setImage(?File $image): self
    {
        $this->image = $image;
        return $this;
    }

    public function getGender(): ?EventGender
    {
        return $this->gender;
    }

    public function setGender(?EventGender $gender): self
    {
        $this->gender = $gender;
        return $this;
    }

    /**
     * @return Collection<int, EventUser>
     */
    public function getEventUser(): Collection
    {
        return $this->eventUser;
    }

    public function addEventUser(EventUser $eventUser): self
    {
        if (!$this->eventUser->contains($eventUser)) {
            $this->eventUser[] = $eventUser;
            $eventUser->setEvent($this);
        }
        return $this;
    }

    public function removeEventUser(EventUser $eventUser): self
    {
        if ($this->eventUser->removeElement($eventUser)) {
            // set the owning side to null (unless already changed)
            if ($eventUser->getEvent() === $this) {
                $eventUser->setEvent(null);
            }
        }
        return $this;
    }

    /**
     * @return Collection<int, Categories>
     */
    public function getCategories(): Collection
    {
        return $this->categories;
    }

    public function addCategory(Categories $category): self
    {
        if (!$this->categories->contains($category)) {
            $this->categories[] = $category;
        }
        return $this;
    }

    public function removeCategory(Categories $category): self
    {
        $this->categories->removeElement($category);
        return $this;
    }

    /**
     * @return Collection<int, EventReport>
     */
    public function getEventReports(): Collection
    {
        return $this->eventReports;
    }

    public function addEventReport(EventReport $eventReport): self
    {
        if (!$this->eventReports->contains($eventReport)) {
            $this->eventReports[] = $eventReport;
            $eventReport->setEvent($this);
        }
        return $this;
    }

    public function removeEventReport(EventReport $eventReport): self
    {
        if ($this->eventReports->removeElement($eventReport)) {
            // set the owning side to null (unless already changed)
            if ($eventReport->getEvent() === $this) {
                $eventReport->setEvent(null);
            }
        }
        return $this;
    }

    /**
     * @return Collection<int, File>
     */
    public function getFiles(): Collection
    {
        return $this->files;
    }

    public function addFile(File $file): self
    {
        if (!$this->files->contains($file)) {
            $this->files[] = $file;
        }
        return $this;
    }

    public function removeFile(File $file): self
    {
        $this->files->removeElement($file);
        return $this;
    }

    public function getPlaceSystem(): ?PlaceSystem
    {
        return $this->placeSystem;
    }

    public function setPlaceSystem(?PlaceSystem $placeSystem): self
    {
        $this->placeSystem = $placeSystem;
        return $this;
    }

    /**
     * @return Collection<int, AttendanceBooking>
     */
    public function getAttendanceBooking(): Collection
    {
        return $this->attendanceBooking;
    }

    public function addAttendanceBooking(AttendanceBooking $attendanceBooking): self
    {
        if (!$this->attendanceBooking->contains($attendanceBooking)) {
            $this->attendanceBooking[] = $attendanceBooking;
            $attendanceBooking->setEvent($this);
        }
        return $this;
    }

    public function removeAttendanceBooking(AttendanceBooking $attendanceBooking): self
    {
        if ($this->attendanceBooking->removeElement($attendanceBooking)) {
            // set the owning side to null (unless already changed)
            if ($attendanceBooking->getEvent() === $this) {
                $attendanceBooking->setEvent(null);
            }
        }
        return $this;
    }

    /**
     * @return Collection<int, Access>
     */
    public function getOrganizer(): Collection
    {
        return $this->organizer;
    }

    public function addOrganizer(Access $organizer): self
    {
        if (!$this->organizer->contains($organizer)) {
            $this->organizer[] = $organizer;
        }
        return $this;
    }

    public function removeOrganizer(Access $organizer): self
    {
        $this->organizer->removeElement($organizer);
        return $this;
    }

    /**
     * @return Collection<int, Tagg>
     */
    public function getTags(): Collection
    {
        return $this->tags;
    }

    public function addTag(Tagg $tag): self
    {
        if (!$this->tags->contains($tag)) {
            $this->tags[] = $tag;
        }
        return $this;
    }

    public function removeTag(Tagg $tag): self
    {
        $this->tags->removeElement($tag);
        return $this;
    }
}
