<?php
declare (strict_types=1);

namespace App\Entity\Organization;

use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use App\Attribute\OrganizationDefaultValue;
use App\Entity\Core\AddressPostal;
use App\Repository\Organization\OrganizationAddressPostalRepository;

//use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Auditable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\Groups;
use App\Validator\Organization as OpentalentAssert;

#[ApiResource(
    operations: [
        new Get(
            security: 'is_granted(\'ROLE_ORGANIZATION_VIEW\') and object.getOrganization().getId() == user.getOrganization().getId()'
        ),
        new Put(
            security: 'object.getOrganization().getId() == user.getOrganization().getId()'
        ),
        new Delete(
            security: 'object.getOrganization().getId() == user.getOrganization().getId()'
        ),
        new GetCollection(
            security: 'is_granted(\'ROLE_ORGANIZATION_VIEW\')'
        ),
        new Post()
    ],
    normalizationContext: ['groups' => ['address']],
    denormalizationContext: ['groups' => ['address']],
    security: 'is_granted(\'ROLE_ORGANIZATION\')'
)]
//#[Auditable]
#[ORM\Entity(repositoryClass: OrganizationAddressPostalRepository::class)]
#[OrganizationDefaultValue(fieldName: "organization")]
#[OpentalentAssert\OrganizationAddressPostal]
class OrganizationAddressPostal
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    #[Groups("address")]
    private ?int $id = null;

    #[ORM\ManyToOne(inversedBy: 'organizationAddressPostals')]
    #[ORM\JoinColumn(nullable: false)]
    private Organization $organization;

    #[ORM\OneToOne(inversedBy: 'organizationAddressPostal', cascade: ['persist', 'remove'])]
    #[ORM\JoinColumn(nullable: false)]
    #[Assert\Valid]
    #[Groups("address")]
    private AddressPostal $addressPostal;

    #[ORM\Column(length: 255)]
    #[Assert\Choice(callback: ['\\App\\Enum\\Organization\\AddressPostalOrganizationTypeEnum', 'toArray'], message: 'invalid-address-postal-type')]
    #[Groups("address")]
    private string $type;

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

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

    public function setOrganization(?Organization $organization): self
    {
        $this->organization = $organization;
        return $this;
    }

    public function getAddressPostal(): ?AddressPostal
    {
        return $this->addressPostal;
    }

    public function setAddressPostal(AddressPostal $addressPostal): self
    {
        $this->addressPostal = $addressPostal;
        return $this;
    }

    public function getType(): string
    {
        return $this->type;
    }

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