<?php

namespace App\ApiResource;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Doctrine\Orm\State\Options;
use App\Entity\UserTrip;
use App\Entity\UserTripEvent;
use App\State\EntityClassDtoStateProcessor;
use App\State\EntityToDtoStateProvider;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Validator\Constraints\NotBlank;

#[ApiResource(
    shortName: 'UserTripEvent',
    operations: [
        new Get(
            security: 'is_granted("ROLE_USER")'
        ),
        new GetCollection(
            security: 'is_granted("ROLE_USER")'
        ),
        new Post(
            security: 'is_granted("ROLE_ADMIN")'
        ),
        new Patch(
            security: 'is_granted("ROLE_ADMIN")'
        ),
    ],
    security: 'is_granted("ROLE_USER")',
    provider: EntityToDtoStateProvider::class,
    processor: EntityClassDtoStateProcessor::class,
    stateOptions: new Options(entityClass: UserTripEvent::class),
)]
class UserTripEventApi
{
    #[ApiProperty(readable: false, writable: false, identifier: true)]
    public ?int $id = null;

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

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

    #[NotBlank]
    #[ApiProperty(writable: true)]
    public ?UserTripApi $userTripIri = null;

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

    #[NotBlank]
    #[ApiProperty(writable: true)]
    public ?EventApi $eventIri = null;

    #[Assert\NotBlank]
    public \DateTimeImmutable $date;

    public ?string $note;

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