<?php

declare(strict_types=1);

namespace App\Entity\Organization;

use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
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") and object.getOrganization().getId() == user.getOrganization().getId()'
        ),
        new Patch(
            security: 'is_granted("ROLE_ORGANIZATION") and object.getOrganization().getId() == user.getOrganization().getId()'
        ),
        new GetCollection(
            security: 'is_granted("ROLE_ORGANIZATION_VIEW")'
        ),
        new Post(
            security: 'is_granted("ROLE_ORGANIZATION")'
        ),
    ],
    processor: SubdomainProcessor::class
)]
#[Auditable]
#[ORM\Entity(repositoryClass: SubdomainRepository::class)]
#[OrganizationDefaultValue(fieldName: 'organization')]
#[UniqueEntity('subdomain')]
#[ApiFilter(filterClass: SearchFilter::class, properties: ['subdomain' => 'exact'])]
#[ORM\Table]
#[ORM\Index(name: 'subdomain_idx', columns: ['subdomain'])]
class Subdomain
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    private ?int $id = null;

    #[ORM\ManyToOne(inversedBy: 'subdomains')]
    #[ORM\JoinColumn(nullable: false)]
    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;

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

    public function setId(?int $id): void
    {
        $this->id = $id;
    }

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

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

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

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

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

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