<?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\Filter\CustomJsonFilter;
use App\Filter\CustomJsonOrderFilter;
use App\State\EntityClassDtoStateProcessor;
use App\State\EntityToDtoStateProvider;
use App\State\UserTripStateProcessor;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Validator\Constraints\NotBlank;

#[ApiResource(
    shortName: 'UserTrip',
    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")'
        ),
        new Delete(
            security: 'is_granted("ROLE_ADMIN")'
        )
    ],
    security: 'is_granted("ROLE_USER")',
    provider: EntityToDtoStateProvider::class,
    processor: UserTripStateProcessor::class,
    stateOptions: new Options(entityClass: UserTrip::class),
)]
#[ApiFilter(SearchFilter::class, properties: [
    'trip' => 'exact',
    'user' => 'exact'
])]
#[ApiFilter(CustomJsonFilter::class)]
#[ApiFilter(CustomJsonOrderFilter::class)]
class UserTripApi
{
    #[ApiProperty(readable: false, writable: false, identifier: true)]
    public ?int $id = null;

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

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

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

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

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

    public ?string $captainName = null;

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

    #[Assert\NotNull]
    public bool $approved = false;

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

    #[ApiProperty(writable: true)]
    public ?MediaObjectApi $signatureIri = null;

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

    public ?\DateTimeImmutable $completedDate = null;

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