<?php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;

#[ApiResource(
    operations: [
        new GetCollection(
            uriTemplate: '/user_site_roles',
            normalizationContext: ['groups' => ['user_site_role:read', 'site:read', 'user:read']],
            security: "is_granted('ROLE_ADMIN')"
        ),
        new Post(
            uriTemplate: '/user_site_roles',
            denormalizationContext: ['groups' => ['user_site_role:write']],
            security: "is_granted('ROLE_ADMIN')"
        ),
        new Delete(
            uriTemplate: '/user_site_roles/{id}',
            security: "is_granted('ROLE_ADMIN')"
        ),
    ],
    paginationEnabled: false,
)]
#[ORM\Entity()]
#[ORM\Table(name: 'user_site_roles')]
#[ORM\UniqueConstraint(name: 'uniq_user_site_role', fields: ['user', 'site', 'role'])]
class UserSiteRole
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    #[Groups(['user_site_role:read'])]
    private ?int $id = null;

    #[ApiProperty(readableLink: true)]
    #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'siteRoles')]
    #[ORM\JoinColumn(nullable: false)]
    #[Groups(['user_site_role:read', 'user_site_role:write'])]
    private ?User $user = null;

    #[ApiProperty(readableLink: true)]
    #[ORM\ManyToOne(targetEntity: Site::class)]
    #[ORM\JoinColumn(nullable: false)]
    #[Groups(['user_site_role:read', 'user_site_role:write'])]
    private ?Site $site = null;

    #[ORM\Column(type: 'string', length: 50)]
    #[Groups(['user_site_role:read', 'user_site_role:write'])]
    private string $role = '';

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

    public function getUser(): ?User
    {
        return $this->user;
    }

    public function setUser(?User $user): self
    {
        $this->user = $user;

        return $this;
    }

    public function getSite(): ?Site
    {
        return $this->site;
    }

    public function setSite(?Site $site): self
    {
        $this->site = $site;

        return $this;
    }

    public function getRole(): string
    {
        return $this->role;
    }

    public function setRole(string $role): self
    {
        $this->role = $role;

        return $this;
    }
}
