<?php
declare (strict_types=1);

namespace App\Entity\Organization;

use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use App\Attribute\OrganizationDefaultValue;
use App\Repository\Organization\SubdomainRepository;
use App\State\Processor\Organization\SubdomainProcessor;

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

/**
 * Sous-domaine enregistré par une organisation
 */
#[ApiResource(
    operations: [
        new Get(
            security: '(is_granted("ROLE_ORGANIZATION_VIEW") or is_granted("ROLE_ORGANIZATION")) and object.getOrganization().getId() == user.getOrganization().getId()'
        ),
        new Put(
            security: 'is_granted("ROLE_ORGANIZATION") and object.getOrganization().getId() == user.getOrganization().getId()'
        ),
        new GetCollection(),
        new Post()
    ],
    processor: SubdomainProcessor::class
)]
//#[Auditable]
#[ORM\Entity(repositoryClass: SubdomainRepository::class)]
#[OrganizationDefaultValue(fieldName: "organization")]
#[UniqueEntity('subdomain')]
#[ApiFilter(filterClass: SearchFilter::class, properties: ['subdomain' => 'exact'])]
class Subdomain
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    private ?int $id = null;

    #[ORM\ManyToOne(inversedBy: 'subdomains')]
    private Organization $organization;

    #[ORM\Column(type: 'string', length: 60, unique: true, nullable: false)]
    #[Groups("subdomain")]
    #[Assert\Regex('/^[\\w\\-]+$/', 'Subdomains can not contains whitespaces or special characters')]
    private string $subdomain;

    #[ORM\Column(options: ['default' => false])]
    #[Groups("subdomain")]
    private bool $active = false;

    /**
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @param int|null $id
     */
    public function setId(?int $id): void
    {
        $this->id = $id;
    }

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

    /**
     * @param Organization $organization
     */
    public function setOrganization(Organization $organization): void
    {
        $this->organization = $organization;
    }

    /**
     * @return string
     */
    public function getSubdomain(): string
    {
        return $this->subdomain;
    }

    /**
     * @param string $subdomain
     */
    public function setSubdomain(string $subdomain): void
    {
        $this->subdomain = $subdomain;
    }

    /**
     * @return bool
     */
    public function isActive(): bool
    {
        return $this->active;
    }

    /**
     * @param bool $active
     */
    public function setActive(bool $active): void
    {
        $this->active = $active;
    }
}
