<?php
/**
 * @author Daniel Knudsen <d.knudsen@spawntree.de>
 * @date 12.12.23
 */


namespace App\ApiResource;

use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use App\Entity\Contact;
use App\Entity\MediaObject;
use App\Filter\ContactNameFilter;
use App\State\EntityClassDtoStateProcessor;
use App\State\EntityToDtoStateProvider;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;

#[ApiResource(
    shortName: 'Contact',
    operations: [
        new Get(
            security: 'is_granted("ROLE_USER")',
        ),
        new GetCollection(
            security: 'is_granted("ROLE_USER")',
        ),
        new Post(
            security: 'is_granted("ROLE_USER")',
        ),
        new Patch(
            security: 'is_granted("ROLE_USER")',
        ),
//        new Delete(
//            security: 'is_granted("ROLE_ADMIN")',
//        )
    ],
    order: ['lastName' => 'ASC'],
    security: 'is_granted("ROLE_USER")',
    provider: EntityToDtoStateProvider::class,
    processor: EntityClassDtoStateProcessor::class,
    stateOptions: new Options(entityClass: Contact::class),
)]
#[ApiFilter(SearchFilter::class, properties: ['partner' => 'exact'])]
#[ApiFilter(ContactNameFilter::class)]
class ContactApi
{
    #[ApiProperty(readable: false, writable: false, identifier: true)]
    public ?int $id = null;

    #[NotBlank]
    public ?string $firstName = null;

    #[NotBlank]
    public ?string $lastName = null;

    public ?string $fullName = null;

    /**
     * @var $partner ?PartnerApi
     */
    #[ApiProperty(
        writable: false,
        readableLink: true,
        writableLink: false,
        builtinTypes: [
            new Type(
                'object',
                class: PartnerApi::class,
            )
        ]
    )]
    public ?PartnerApi $partner = null;

    #[NotBlank]
    public ?PartnerApi $partnerIri = null;

    public ?\DateTimeImmutable $birthday = null;

    public ?MediaObject $image = null;

    #[ApiProperty(writable: false)]
    public ?string $imageUrl = null;

    public ?string $position = null;

    public ?string $phone = null;

    #[Email]
    public ?string $email = null;

    /**
     * @var array<int, PostingApi>
     */
    #[ApiProperty(writable: false)]
    public array $posts = [];

    /**
     * @var $createdBy ?UserApi
     */
    #[ApiProperty(
        writable: false,
        readableLink: true,
        writableLink: true,
        builtinTypes: [
            new Type(
                'object',
                class: UserApi::class,
            )
        ]
    )]
    public ?UserApi $createdBy = null;

    #[ApiProperty(writable: false)]
    public ?\DateTimeImmutable $createdAt = null;
}