<?php

namespace App\ApiResource;

use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
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 ApiPlatform\Doctrine\Orm\State\Options;
use App\Entity\UserTrip;
use App\Entity\UserTripEvent;
use App\Filter\CustomJsonFilter;
use App\Filter\CustomJsonOrderFilter;
use App\Filter\UserTripEventFilter;
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_USER")'
        ),
        new Patch(
            security: 'is_granted("ROLE_USER")'
        ),
        new Delete(
            security: 'is_granted("ROLE_USER")'
        )
    ],
    order: ['date' => 'ASC'],
    security: 'is_granted("ROLE_USER")',
    provider: EntityToDtoStateProvider::class,
    processor: EntityClassDtoStateProcessor::class,
    stateOptions: new Options(entityClass: UserTripEvent::class),
)]

#[ApiFilter(SearchFilter::class, properties: ['userTrip' => 'exact'])]
#[ApiFilter(UserTripEventFilter::class)]
#[ApiFilter(CustomJsonFilter::class)]
#[ApiFilter(CustomJsonOrderFilter::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;

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

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

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

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

    public ?string $note = null;

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