<?php
declare (strict_types=1);

namespace App\Entity\Organization;

use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\Organization\TypeOfPracticeRepository;

//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 JetBrains\PhpStorm\Pure;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Type des pratique d'une organisation
 */
#[ApiResource(
    operations: [
        new Get(),
        new GetCollection(
            normalizationContext: ['groups' => ['read']]
        )
    ],
    paginationEnabled: false
)]
//#[Auditable]
#[ORM\Entity(repositoryClass: TypeOfPracticeRepository::class)]
class TypeOfPractice
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    #[Groups(["read"])]
    private ?int $id = null;

    #[ORM\Column(length: 255, nullable: true)]
    #[Assert\Choice(callback: ['\\App\\Enum\\Cotisation\\TypeOfPracticeEnum', 'toArray'], message: 'invalid-name')]
    #[Groups(["read"])]
    private ?string $name = null;

    #[ORM\Column(length: 255, nullable: true)]
    #[Assert\Choice(callback: ['\\App\\Enum\\Cotisation\\CategoryTypeOfPracticeEnum', 'toArray'], message: 'invalid-category')]
    #[Groups(["read"])]
    private ?string $category = null;

    #[ORM\ManyToMany(targetEntity: Organization::class, inversedBy: 'typeOfPractices')]
    #[ORM\JoinTable(name: 'organization_type_of_practices')]
    #[ORM\JoinColumn(name: 'typeofpractice_id', referencedColumnName: 'id')]
    #[ORM\InverseJoinColumn(name: 'organization_id', referencedColumnName: 'id')]
    private Collection $organizations;

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

    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 getCategory(): ?string
    {
        return $this->category;
    }

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

    public function getOrganizations(): Collection
    {
        return $this->organizations;
    }

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

    public function removeOrganization(Organization $organization): self
    {
        if ($this->organizations->removeElement($organization)) {
            $organization->removeTypeOfPractice($this);
        }
        return $this;
    }
}