<?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 ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use App\Entity\Task;
use App\Enum\PartnerType;
use App\Enum\PrioType;
use App\State\EntityClassDtoStateProcessor;
use App\State\EntityToDtoStateProvider;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints\NotBlank;

#[ApiResource(
    shortName: 'Task',
    operations: [
        new Get(
            security: 'is_granted("ROLE_USER")'
        ),
        new GetCollection(
            security: 'is_granted("ROLE_USER")'
        ),
        new Post(
            security: 'is_granted("ROLE_USER")',
            validationContext: ['groups' => ['Default', 'postValidation']]
        ),
        new Patch(
            security: 'is_granted("ROLE_USER")'
        ),
//        new Delete(
//            security: 'is_granted("ROLE_USER")',
//        )
    ],
    order: ['dueAt' => 'DESC'],
    security: 'is_granted("ROLE_USER")',
    provider: EntityToDtoStateProvider::class,
    processor: EntityClassDtoStateProcessor::class,
    stateOptions: new Options(entityClass: Task::class),
)]
#[ApiFilter(SearchFilter::class, properties: [
    'assignedTo' => 'exact',
    'partner' => 'exact',
    'contact' => 'exact'
])]
class TaskApi
{
    #[ApiProperty(readable: false, writable: false, identifier: true)]
    public ?int $id = null;

    #[Assert\NotBlank]
    public ?string $headline = null;

    public ?string $description = null;

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

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

    #[Assert\NotBlank]
    public ?UserApi $assignedToIri = null;

    #[Assert\NotBlank]
    public ?\DateTimeImmutable $dueAt = 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;

    #[NotBlank]
    public ?ProductApi $productIri = null;

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

    public ?ContactApi $contactIri = null;

    #[Assert\NotBlank]
    public PrioType $prio;

    #[Assert\NotNull]
    public ?bool $completed = null;

    #[ApiProperty(writable: false)]
    public ?int $numTaskNotes = null;

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

}
