<?php

declare(strict_types=1);

namespace App\Entity\Booking;

use ApiPlatform\Metadata\ApiResource;
use App\Entity\Access\Access;
use App\Entity\AccessWish\EducationStudentWish;
use App\Entity\Education\Education;
use App\Entity\Education\EducationCurriculum;
use App\Entity\Organization\Organization;
use App\Entity\Place\Place;
use App\Entity\Place\Room;
use App\Repository\Booking\CourseRepository;
// use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Auditable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @todo : A la suite de la migration, il faut supprimer le nom de la table pour avoir une table Course, et supprimer l'attribut discr.
 * @todo : migration table tag_booking
 *
 * Classe Course qui permet de gérer les cours de la structure.
 *
 * Security :
 *
 *   * @see App\Doctrine\Booking\CurrentCoursesExtension
 */
#[ApiResource(operations: [])]
// #[Auditable]
#[ORM\Entity(repositoryClass: CourseRepository::class)]
class Course extends AbstractBooking
{
    /** @var Collection<int, CourseRecur> */
    #[ORM\OneToMany(targetEntity: CourseRecur::class, mappedBy: 'event', cascade: ['persist', 'remove'])]
    protected Collection $eventRecur;

    /** @var Collection<int, Course> */
    #[ORM\OneToMany(targetEntity: Course::class, mappedBy: 'parent', orphanRemoval: true)]
    protected Collection $timeline;

    #[ORM\ManyToOne(targetEntity: Course::class, cascade: ['persist'], inversedBy: 'timeline')]
    #[ORM\JoinColumn(nullable: true)]
    protected ?Course $parent;

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

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

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

    #[ORM\ManyToOne(inversedBy: 'courses')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    protected ?Education $education = null;

    /** @var Collection<int, EducationCurriculum> */
    #[ORM\ManyToMany(targetEntity: EducationCurriculum::class, inversedBy: 'courses', cascade: ['persist'])]
    #[ORM\JoinTable(name: 'course_education_curriculum')]
    #[ORM\JoinColumn(name: 'course_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    #[ORM\InverseJoinColumn(name: 'education_curriculum_id', referencedColumnName: 'id')]
    protected Collection $educationCurriculum;

    /** @var Collection<int, Access> */
    #[ORM\ManyToMany(targetEntity: Access::class, inversedBy: 'courses')]
    #[ORM\JoinTable(name: 'course_student')]
    #[ORM\JoinColumn(name: 'course_id', referencedColumnName: 'id')]
    #[ORM\InverseJoinColumn(name: 'student_id', referencedColumnName: 'id')]
    protected Collection $students;

    /** @var Collection<int, Work> */
    #[ORM\OneToMany(targetEntity: Work::class, mappedBy: 'course', cascade: ['persist', 'remove'])]
    protected Collection $work;

    /** @var Collection<int, AttendanceBooking> */
    #[ORM\OneToMany(targetEntity: AttendanceBooking::class, mappedBy: 'course', cascade: ['persist', 'remove'])]
    #[ORM\JoinColumn(nullable: false)]
    protected Collection $attendanceBooking;

    /** @var Collection<int, EducationStudentWish> */
    #[ORM\OneToMany(targetEntity: EducationStudentWish::class, mappedBy: 'course', cascade: [])]
    protected Collection $educationStudentWishes;

    public function __construct()
    {
        $this->eventRecur = new ArrayCollection();
        $this->timeline = new ArrayCollection();
        $this->organizer = new ArrayCollection();
        $this->educationCurriculum = new ArrayCollection();
        $this->students = new ArrayCollection();
        $this->work = new ArrayCollection();
        $this->equipments = new ArrayCollection();
        $this->attendanceBooking = new ArrayCollection();
        $this->educationStudentWishes = new ArrayCollection();
        parent::__construct();
    }

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

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

        return $this;
    }

    public function removeEventRecur(CourseRecur $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, Course>
     */
    public function getTimeline(): Collection
    {
        return $this->timeline;
    }

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

        return $this;
    }

    public function removeTimeline(Course $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 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;
    }

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

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

        return $this;
    }

    public function getEducation(): ?Education
    {
        return $this->education;
    }

    public function setEducation(?Education $education): self
    {
        $this->education = $education;

        return $this;
    }

    /**
     * @return Collection<int, EducationCurriculum>
     */
    public function getEducationCurriculum(): Collection
    {
        return $this->educationCurriculum;
    }

    public function addEducationCurriculum(EducationCurriculum $educationCurriculum): self
    {
        if (!$this->educationCurriculum->contains($educationCurriculum)) {
            $this->educationCurriculum[] = $educationCurriculum;
        }

        return $this;
    }

    public function removeEducationCurriculum(EducationCurriculum $educationCurriculum): self
    {
        $this->educationCurriculum->removeElement($educationCurriculum);

        return $this;
    }

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

    public function addStudent(Access $student): self
    {
        if (!$this->students->contains($student)) {
            $this->students[] = $student;
        }

        return $this;
    }

    public function removeStudent(Access $student): self
    {
        $this->students->removeElement($student);

        return $this;
    }

    /**
     * @return Collection<int, Work>
     */
    public function getWork(): Collection
    {
        return $this->work;
    }

    public function addWork(Work $work): self
    {
        if (!$this->work->contains($work)) {
            $this->work[] = $work;
            $work->setCourse($this);
        }

        return $this;
    }

    public function removeWork(Work $work): self
    {
        if ($this->work->removeElement($work)) {
            // set the owning side to null (unless already changed)
            if ($work->getCourse() === $this) {
                $work->setCourse(null);
            }
        }

        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->setCourse($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->getCourse() === $this) {
                $attendanceBooking->setCourse(null);
            }
        }

        return $this;
    }

    public function getEducationStudentWishes(): Collection
    {
        return $this->educationStudentWishes;
    }

    public function addEducationStudentWish(EducationStudentWish $educationStudentWish): self
    {
        if (!$this->educationStudentWishes->contains($educationStudentWish)) {
            $this->educationStudentWishes[] = $educationStudentWish;
        }

        return $this;
    }

    public function removeEducationStudentWish(EducationStudentWish $educationStudentWish): self
    {
        $this->educationStudentWishes->removeElement($educationStudentWish);

        return $this;
    }
}
