<?php
declare (strict_types=1);

namespace App\Entity\Core;

use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use App\Entity\Organization\Organization;
use App\Entity\Person\Person;
use App\Entity\Place\Place;

//use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Auditable;
use JetBrains\PhpStorm\Pure;
use libphonenumber\PhoneNumber;
use App\Repository\Core\ContactPointRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use App\Validator\Core as OpentalentAssert;

/**
 * Données de contact d'une Person ou d'une Organization ou d'un lieu
 */
#[ApiResource(
    operations: [
        new Get(
            security: 'is_granted("CONTACT_POINT_READ", object)'
        ),
        new Put(
            security: 'is_granted("CONTACT_POINT_EDIT", object)'
        ),
        new Delete(
            security: 'is_granted("CONTACT_POINT_DELETE", object)'
        ),
        new Post(),
        new GetCollection()
    ]
)]
//#[Auditable]
#[ORM\Entity(repositoryClass: ContactPointRepository::class)]
#[OpentalentAssert\ContactPoint]
class ContactPoint
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    #[Assert\Choice(callback: ['\\App\\Enum\\Core\\ContactPointTypeEnum', 'toArray'], message: 'invalid-contact-type')]
    private string $contactType;

    #[ORM\Column(length: 255, nullable: true)]
    #[Assert\Email(message: 'invalid-email-format', mode: 'strict')]
    private ?string $email = null;

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

    #[ORM\Column(type: 'phone_number', nullable: true)]
    private ?PhoneNumber $faxNumber = null;

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

    #[ORM\Column(type: 'phone_number', nullable: true)]
    private ?PhoneNumber $telphone = null;

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

    #[ORM\Column(type: 'phone_number', nullable: true)]
    private ?PhoneNumber $mobilPhone = null;

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

    #[ORM\ManyToMany(targetEntity: Organization::class, inversedBy: 'contactPoints')]
    #[ORM\JoinTable(name: 'organization_contactpoint')]
    #[ORM\JoinColumn(name: 'contactPoint_id', referencedColumnName: 'id', unique: true)]
    #[ORM\InverseJoinColumn(name: 'organization_id', referencedColumnName: 'id')]
    private Collection $organization;

    #[ORM\ManyToMany(targetEntity: Person::class, inversedBy: 'contactPoints')]
    #[ORM\JoinTable(name: 'person_contactpoint')]
    #[ORM\JoinColumn(name: 'contactPoint_id', referencedColumnName: 'id', unique: true)]
    #[ORM\InverseJoinColumn(name: 'person_id', referencedColumnName: 'id')]
    private Collection $person;

    #[ORM\ManyToMany(targetEntity: Place::class, mappedBy: 'contactpoint')]
    #[ORM\JoinTable(name: 'place_contactpoint')]
    #[ORM\JoinColumn(name: 'contactPoint_id', referencedColumnName: 'id', unique: true)]
    #[ORM\InverseJoinColumn(name: 'place_id', referencedColumnName: 'id')]
    private Collection $place;

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

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

    public function getContactType(): string
    {
        return $this->contactType;
    }

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

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(?string $email): self
    {
        $this->email = $email;
        if (!is_null($this->email && !is_null($this->getEmailInvalid()))) {
            $this->setEmailInvalid(null);
        }
        return $this;
    }

    public function getEmailInvalid(): ?string
    {
        return $this->emailInvalid;
    }

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

    public function getFaxNumber(): ?PhoneNumber
    {
        return $this->faxNumber;
    }

    public function setFaxNumber(?PhoneNumber $faxNumber): self
    {
        $this->faxNumber = $faxNumber;
        if (!is_null($this->faxNumber) && !is_null($this->getFaxNumberInvalid())) {
            $this->setFaxNumberInvalid(null);
        }
        return $this;
    }

    public function getFaxNumberInvalid(): ?string
    {
        return $this->faxNumberInvalid;
    }

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

    public function getTelphone(): ?PhoneNumber
    {
        return $this->telphone;
    }

    public function setTelphone(?PhoneNumber $telphone): self
    {
        $this->telphone = $telphone;
        if (!is_null($this->telphone && !is_null($this->getTelphoneInvalid()))) {
            $this->setTelphoneInvalid(null);
        }
        return $this;
    }

    public function getTelphoneInvalid(): ?string
    {
        return $this->telphoneInvalid;
    }

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

    public function getMobilPhone(): ?PhoneNumber
    {
        return $this->mobilPhone;
    }

    public function setMobilPhone(?PhoneNumber $mobilPhone): self
    {
        $this->mobilPhone = $mobilPhone;
        if (!is_null($this->mobilPhone && !is_null($this->getMobilPhoneInvalid()))) {
            $this->setMobilPhoneInvalid(null);
        }
        return $this;
    }

    public function getMobilPhoneInvalid(): ?string
    {
        return $this->mobilPhoneInvalid;
    }

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

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

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

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

    public function getPerson(): Collection
    {
        return $this->person;
    }

    public function addPerson(Person $person): self
    {
        if (!$this->person->contains($person)) {
            $this->person[] = $person;
            $person->addContactPoint($this);
        }
        return $this;
    }

    public function removePerson(Person $person): self
    {
        if ($this->person->removeElement($person)) {
            $person->removeContactPoint($this);
        }
        return $this;
    }

    /**
     * @return Collection<int, Place>
     */
    public function getPlace(): Collection
    {
        return $this->place;
    }

    public function addPlace(Place $place): self
    {
        if (!$this->place->contains($place)) {
            $this->place[] = $place;
            $place->addContactpoint($this);
        }
        return $this;
    }

    public function removePlace(Place $place): self
    {
        if ($this->place->removeElement($place)) {
            $place->removeContactpoint($this);
        }
        return $this;
    }
}
