<?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;

#[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;

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

    #[ApiProperty(writable: false)]
    public ?UserApi $createdBy = null;

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

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

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

    #[Assert\NotBlank]
    public ?\DateTimeImmutable $dueAt = null;

    public ?PartnerApi $partner = null;

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

    #[ApiProperty(writable: false)]
    public ?PartnerType $partnerType = null;

    public ?ContactApi $contact = null;

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

    #[Assert\NotBlank]
    public PrioType $prio;

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

    /**
     * @var $taskNotes array<int, TaskNoteApi>
     */
    #[ApiProperty(
        writable: false,
        readableLink: true,
        writableLink: true,
        builtinTypes: [
            new Type(
                'object',
                collection: true,
                collectionKeyType: [new Type('int')],
                collectionValueType: new Type('object', class: TaskNoteApi::class)
            )
        ]
    )]
    public array $taskNotes = [];

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

}
