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


namespace App\ApiResource;

use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
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\Comment;
use App\Entity\Document;
use App\Entity\DocumentObject;
use App\Entity\Partner;
use App\Interface\OwnerInterface;
use App\State\EntityClassDtoStateProcessor;
use App\State\EntityToDtoStateProvider;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use App\Validator\IsValidOwner;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Validator\Constraints\NotBlank;

#[ApiResource(
    shortName: 'Document',
    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")',
//        )
    ],
    security: 'is_granted("ROLE_USER")',
    provider: EntityToDtoStateProvider::class,
    processor: EntityClassDtoStateProcessor::class,
    stateOptions: new Options(entityClass: Document::class),
)]
#[ApiFilter(SearchFilter::class, properties: [
    'name' => 'ipartial',
    'partner' => 'exact',
    'product' => 'exact',
])]
#[ApiFilter(OrderFilter::class, properties: [
    'name',
    'partner.name',
    'product.name',
], arguments: ['orderParameterName' => 'order'])]
class DocumentApi
{
    #[ApiProperty(readable: false, writable: false, identifier: true)]
    public ?int $id = null;

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

    public ?string $description = null;

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

    public ?PartnerApi $partnerIri = null;

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

    public ?ProductApi $productIri = null;

    #[NotBlank]
    public ?DocumentObject $documentObject = null;

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

    /**
     * @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;
}