<?php

declare(strict_types=1);

namespace App\Entity\Custom\Search;

use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use App\Filter\ApiPlatform\Utils\InFilter;
use App\Repository\Custom\Search\UserSearchItemRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * Données réduites d'identification d'un utilisateur (ids, noms)
 * Utilisées entre autres pour les listes déroulantes de recherche.
 *
 * Fichier source de la view : ./sql/schema-extensions/003-view_search_user.sql
 *
 * @see App\Doctrine\Custom\Search\RestrictToOrganizationIdExtension.php
 */
#[ApiResource(
    operations: [
        new Get(
            uriTemplate: '/search/users/{id}',
            security: 'object.getOrganizationId() == user.getOrganization().getId()'
        ),
        new GetCollection(
            uriTemplate: '/search/users',
            paginationMaximumItemsPerPage: 10,
            paginationClientItemsPerPage: true,
            order: ['fullName' => 'ASC']
        ),
    ]
)]
#[ORM\Table(name: 'view_search_user')]
#[ORM\Entity(repositoryClass: UserSearchItemRepository::class, readOnly: true)]
#[ApiFilter(filterClass: InFilter::class, properties: ['id'])]
#[ApiFilter(filterClass: SearchFilter::class, properties: ['fullName' => 'ipartial'])]
class UserSearchItem
{
    #[ORM\Id]
    #[ORM\Column]
    private int $id;

    #[ORM\Column(type: 'integer', nullable: true)]
    private ?int $organizationId = null;

    #[ORM\Column(type: 'integer', nullable: true)]
    private ?int $personId = null;

    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $username = null;

    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $name = null;

    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $givenName = null;

    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $fullName = null;

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

    public function setId(int $id): self
    {
        $this->id = $id;

        return $this;
    }

    public function getOrganizationId(): ?int
    {
        return $this->organizationId;
    }

    public function setOrganizationId(?int $organizationId): self
    {
        $this->organizationId = $organizationId;

        return $this;
    }

    public function getPersonId(): ?int
    {
        return $this->personId;
    }

    public function setPersonId(?int $personId): self
    {
        $this->personId = $personId;

        return $this;
    }

    public function getUsername(): ?string
    {
        return $this->username;
    }

    public function setUsername(?string $username): self
    {
        $this->username = $username;

        return $this;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(?string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getGivenName(): ?string
    {
        return $this->givenName;
    }

    public function setGivenName(?string $givenName): self
    {
        $this->givenName = $givenName;

        return $this;
    }

    public function getFullName(): ?string
    {
        return $this->fullName;
    }

    public function setFullName(?string $fullName): self
    {
        $this->fullName = $fullName;

        return $this;
    }
}
