<?php
declare (strict_types=1);

namespace App\Entity\Network;

use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\Network\NetworkRepository;

//use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Auditable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JetBrains\PhpStorm\Pure;
use Symfony\Component\Serializer\Annotation\Groups;
use Doctrine\Common\Collections\Collection;

/**
 * Enum des différents réseaux auxquels peut appartenir une Organization
 */
#[ApiResource(
    operations: [
        new Get(),
        new GetCollection()
    ],
    security: 'is_granted(\'ROLE_ORGANIZATION_VIEW\')'
)]
//#[Auditable]
#[ORM\Entity(repositoryClass: NetworkRepository::class)]
class Network
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    #[Groups("network")]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    #[Groups("network")]
    private string $name;

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

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

    #[ORM\OneToMany(mappedBy: 'network', targetEntity: NetworkOrganization::class, orphanRemoval: true)]
    private Collection $organizations;

    #[Pure]
    public function __construct()
    {
        $this->organizations = new ArrayCollection();
    }

    public function setId(int $id): self
    {
        $this->id = $id;
        return $this;
    }

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

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

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

    public function getLogo(): ?string
    {
        return $this->logo;
    }

    public function setLogo(?string $logo): self
    {
        $this->logo = $logo;
        return $this;
    }

    public function getUrl(): ?string
    {
        return $this->url;
    }

    public function setUrl(?string $url): self
    {
        $this->url = $url;
        return $this;
    }

    /**
     * @return Collection<int, NetworkOrganization>
     */
    public function getOrganizations(): Collection
    {
        return $this->organizations;
    }

    public function addOrganization(NetworkOrganization $organization): self
    {
        if (!$this->organizations->contains($organization)) {
            $this->organizations[] = $organization;
            $organization->setNetwork($this);
        }
        return $this;
    }

    public function removeOrganization(NetworkOrganization $organization): self
    {
        if ($this->organizations->removeElement($organization)) {
            // set the owning side to null (unless already changed)
            if ($organization->getNetwork() === $this) {
                $organization->setNetwork(null);
            }
        }
        return $this;
    }
}
