<?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\Trip;
use App\Filter\CustomJsonFilter;
use App\Filter\CustomJsonOrderFilter;
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: 'Trip',
    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: EntityClassDtoStateProcessor::class,
    stateOptions: new Options(entityClass: Trip::class),
)]
#[ApiFilter(SearchFilter::class, properties: [
    'id' => 'ipartial',
])]
#[ApiFilter(CustomJsonFilter::class)]
#[ApiFilter(CustomJsonOrderFilter::class)]
class TripApi
{
    #[ApiProperty(readable: false, writable: false, identifier: true)]
    public ?int $id = null;

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

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

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

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

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

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

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

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

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

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

    public ?string $note;

    public bool $completed = false;

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