<?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\ContactPartnerProduct;
use App\Entity\PartnerProduct;
use App\State\EntityClassDtoStateProcessor;
use App\State\EntityToDtoStateProvider;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Validator\Constraints\NotBlank;

#[ApiResource(
    shortName: 'ContactPartnerProduct',
    operations: [
        new Get(
            security: 'is_granted("ROLE_USER")'
        ),
        new GetCollection(
            security: 'is_granted("ROLE_USER")',
        ),
        new Post(
            security: 'is_granted("ROLE_USER")',
        ),
        new Delete(
            security: 'is_granted("ROLE_USER")',
        )
    ],
    security: 'is_granted("ROLE_USER")',
    provider: EntityToDtoStateProvider::class,
    processor: EntityClassDtoStateProcessor::class,
    stateOptions: new Options(entityClass: ContactPartnerProduct::class),
)]
#[ApiFilter(SearchFilter::class, properties: [
    'contact' => 'exact',
    'partnerProduct.product.name' => 'ipartial',
])]
#[ApiFilter(OrderFilter::class, properties: ['partnerProduct.product.name'], arguments: ['orderParameterName' => 'order'])]
class ContactPartnerProductApi
{
    #[ApiProperty(readable: false, writable: false, identifier: true)]
    public ?int $id = null;

    #[NotBlank]
    public ?ContactApi $contactIri = null;

    #[NotBlank]
    public ?PartnerProductApi $partnerProductIri = null;

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

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

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