<?php
declare(strict_types=1);
namespace App\Entity\ClinicalManager;
use App\Entity\Customer\Customer;
use App\Entity\Doctor\Doctor;
use App\Entity\Order\Order;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Sylius\Component\Resource\Model\ResourceInterface;
use function array_unique;
use function array_values;
use function in_array;
#[ORM\Table(name: 'app_clinical_manager')]
#[ORM\Entity]
class ClinicalManager implements ResourceInterface
{
use TimestampableEntity;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private int|null $id = null;
#[ORM\Column(name: 'requires_validation_by_clinical_manager', type: 'boolean', options: ['default' => false])]
private bool $requiresValidationByClinicalManager = false;
#[ORM\OneToOne(mappedBy: 'clinicalManager', targetEntity: Customer::class)]
private Customer $customer;
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'attachedClinicalManagers')]
#[ORM\JoinColumn(name: 'attached_clinical_manager_id', nullable: true, onDelete: 'SET NULL')]
private ClinicalManager|null $attachedClinicalManager = null;
#[ORM\OneToMany(mappedBy: 'attachedClinicalManager', targetEntity: self::class, cascade: ['all'], orphanRemoval: true)]
private Collection $attachedClinicalManagers;
#[ORM\OneToMany(mappedBy: 'clinicalManagerWhoValidated', targetEntity: Order::class, cascade: ['all'], orphanRemoval: true)]
private Collection $validatedOrders;
#[ORM\OneToMany(mappedBy: 'attachedClinicalManager', targetEntity: Doctor::class, cascade: ['all'], orphanRemoval: true)]
private Collection $attachedDoctors;
public function __construct()
{
$this->validatedOrders = new ArrayCollection();
$this->attachedDoctors = new ArrayCollection();
$this->attachedClinicalManagers = new ArrayCollection();
}
public function getId(): int|null
{
return $this->id;
}
public function getCustomer(): Customer
{
return $this->customer;
}
public function setCustomer(Customer $customer): void
{
$this->customer = $customer;
}
public function getAttachedClinicalManager(): ClinicalManager|null
{
return $this->attachedClinicalManager;
}
public function setAttachedClinicalManager(ClinicalManager|null $attachedClinicalManager): void
{
$this->attachedClinicalManager = $attachedClinicalManager;
}
/** @return Collection<array-key, Order> */
public function getValidatedOrders(): Collection
{
return $this->validatedOrders;
}
/** @param Collection<array-key, Order> $validatedOrders */
public function setValidatedOrders(Collection $validatedOrders): void
{
$this->validatedOrders = $validatedOrders;
}
public function addValidatedOrder(Order $order): self
{
if (! $this->validatedOrders->contains($order)) {
$this->validatedOrders[] = $order;
}
return $this;
}
public function removeValidatedOrder(Order $order): self
{
$this->validatedOrders->removeElement($order);
return $this;
}
public function isRequiresValidationByClinicalManager(): bool
{
return $this->requiresValidationByClinicalManager;
}
public function setRequiresValidationByClinicalManager(bool $requiresValidationByClinicalManager): void
{
$this->requiresValidationByClinicalManager = $requiresValidationByClinicalManager;
}
/** @return Collection<array-key, Doctor> */
public function getAttachedDoctors(): Collection
{
return $this->attachedDoctors;
}
/** @param Collection<array-key, Doctor> $attachedDoctors */
public function setAttachedDoctors(Collection $attachedDoctors): void
{
$this->attachedDoctors = $attachedDoctors;
}
public function addAttachedDoctor(Doctor $doctor): self
{
if (! $this->attachedDoctors->contains($doctor)) {
$this->attachedDoctors[] = $doctor;
$doctor->setAttachedClinicalManager($this);
}
return $this;
}
public function removeAttachedDoctor(Doctor $doctor): self
{
$this->attachedDoctors->removeElement($doctor);
return $this;
}
/** @return Collection<array-key, self> */
public function getAttachedClinicalManagers(): Collection
{
return $this->attachedClinicalManagers;
}
/** @param Collection<array-key, self> $clinicalManagers */
public function setAttachedClinicalManagers(Collection $clinicalManagers): void
{
$this->attachedClinicalManagers = $clinicalManagers;
}
public function addAttachedClinicalManager(self $clinicalManager): self
{
if (! $this->attachedClinicalManagers->contains($clinicalManager)) {
$this->attachedClinicalManagers[] = $clinicalManager;
$clinicalManager->setAttachedClinicalManager($this);
}
return $this;
}
public function removeAttachedClinicalManager(self $clinicalManager): self
{
$this->attachedClinicalManagers->removeElement($clinicalManager);
return $this;
}
/** @return array<int|null> */
public function getAttachedClinicalManagerIdsRecursively(array $ids = []): array
{
// This method also adds the ID of the current clinical manager
$ids[] = $this->getId();
// Return the id of the clinical manager in the case where it is attached to itself to not having an infinite loop
if ($this->attachedClinicalManager?->getId() === $this->getId()) {
return $ids;
}
$ids[] = $this->getId();
foreach ($this->getAttachedClinicalManagers() as $attachedClinicalManager) {
if (in_array($attachedClinicalManager->getId(), $ids, true)) {
continue;
}
$ids[] = $attachedClinicalManager->getId();
$ids = $attachedClinicalManager->getAttachedClinicalManagerIdsRecursively($ids);
}
return array_values(array_unique($ids));
}
}