<?php

declare(strict_types=1);

namespace App\Entity\Organization;

use ApiPlatform\Metadata\ApiResource;
use App\Attribute\OrganizationDefaultValue;
use App\Entity\Core\AddressPostal;
use App\Entity\Traits\CreatedOnAndByTrait;
use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
use App\Repository\Organization\OrganizationAddressPostalRepository;
// use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Auditable;
use App\Validator\Organization as OpentalentAssert;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Fait le lien entre une adresse postal et une organisation.
 *
 * Security :
 *
 *   * @see App\Doctrine\Organization\CurrentOrganizationAddressPostalExtension
 */
#[ApiResource(
    operations: [],
    normalizationContext: ['groups' => ['address']],
    denormalizationContext: ['groups' => ['address']]
)]
// #[Auditable]
#[ORM\Entity(repositoryClass: OrganizationAddressPostalRepository::class)]
#[OrganizationDefaultValue(fieldName: 'organization')]
#[OpentalentAssert\OrganizationAddressPostal]
class OrganizationAddressPostal
{
    use CreatedOnAndByTrait;

    #[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'], orphanRemoval: true)]
    #[ORM\JoinColumn(nullable: false)]
    #[Assert\Valid]
    #[Groups('address')]
    private AddressPostal $addressPostal;

    #[ORM\Column(length: 50, enumType: AddressPostalOrganizationTypeEnum::class)]
    #[Groups('address')]
    private AddressPostalOrganizationTypeEnum $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(): AddressPostalOrganizationTypeEnum
    {
        return $this->type;
    }

    public function setType(AddressPostalOrganizationTypeEnum $type): self
    {
        $this->type = $type;

        return $this;
    }
}
