<?php
declare (strict_types=1);

namespace App\Entity\Core;

use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use App\Entity\Organization\OrganizationAddressPostal;
use App\Entity\Place\Place;
use App\Repository\Core\AddressPostalRepository;

//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 App\Entity\Person\PersonAddressPostal;
use Symfony\Component\Serializer\Annotation\Groups;

#[ApiResource(
    operations: [
        new Get(
            security: 'is_granted(\'ROLE_ORGANIZATION_VIEW\') and object.getOrganizationAddressPostal().getOrganization().getId() == user.getOrganization().getId()'
        ),
        new GetCollection()
    ]
)]
//#[Auditable]
#[ORM\Entity(repositoryClass: AddressPostalRepository::class)]
class AddressPostal
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    #[Groups(["address", "access_address"])]
    private ?int $id = null;

    #[ORM\ManyToOne]
    #[Groups("address")]
    private ?Country $addressCountry = null;

    #[ORM\Column(length: 100, nullable: true)]
    #[Groups("address")]
    private ?string $addressCity = null;

    #[ORM\Column(length: 100, nullable: true)]
    #[Groups("address")]
    private ?string $addressOwner = null;

    #[ORM\Column(length: 20, nullable: true)]
    #[Groups("address")]
    private ?string $postalCode = null;

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

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

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

    #[ORM\Column(nullable: true)]
    #[Groups("address")]
    private ?float $latitude = null;

    #[ORM\Column(nullable: true)]
    #[Groups("address")]
    private ?float $longitude = null;

    #[ORM\OneToOne(mappedBy: 'addressPostal')]
    private OrganizationAddressPostal $organizationAddressPostal;

    #[ORM\OneToOne(mappedBy: 'addressPostal')]
    private PersonAddressPostal $personAddressPostal;

    #[ORM\OneToMany(mappedBy: 'addressPostal', targetEntity: Place::class)]
    private Collection $places;

    public function __construct()
    {
        $this->places = new ArrayCollection();
    }

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

    public function getAddressCountry(): ?Country
    {
        return $this->addressCountry;
    }

    public function setAddressCountry(?Country $addressCountry): self
    {
        $this->addressCountry = $addressCountry;
        return $this;
    }

    public function getAddressCity(): ?string
    {
        return $this->addressCity;
    }

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

    public function getAddressOwner(): ?string
    {
        return $this->addressOwner;
    }

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

    public function getPostalCode(): ?string
    {
        return $this->postalCode;
    }

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

    public function getStreetAddress(): ?string
    {
        return $this->streetAddress;
    }

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

    public function getStreetAddressSecond(): ?string
    {
        return $this->streetAddressSecond;
    }

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

    public function getStreetAddressThird(): ?string
    {
        return $this->streetAddressThird;
    }

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

    public function getLatitude(): ?float
    {
        return $this->latitude;
    }

    public function setLatitude(?float $latitude): self
    {
        $this->latitude = $latitude;
        return $this;
    }

    public function getLongitude(): ?float
    {
        return $this->longitude;
    }

    public function setLongitude(?float $longitude): self
    {
        $this->longitude = $longitude;
        return $this;
    }

    public function getOrganizationAddressPostal(): ?OrganizationAddressPostal
    {
        return $this->organizationAddressPostal;
    }

    public function setOrganizationAddressPostal(OrganizationAddressPostal $organizationAddressPostal): self
    {
        $this->organizationAddressPostal = $organizationAddressPostal;
        return $this;
    }

    public function getPersonAddressPostal(): ?PersonAddressPostal
    {
        return $this->personAddressPostal;
    }

    public function setPersonAddressPostal(PersonAddressPostal $personAddressPostal): self
    {
        $this->personAddressPostal = $personAddressPostal;
        return $this;
    }

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

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

    public function removePlace(Place $place): self
    {
        if ($this->places->removeElement($place)) {
            // set the owning side to null (unless already changed)
            if ($place->getAddressPostal() === $this) {
                $place->setAddressPostal(null);
            }
        }
        return $this;
    }
}
