<?php

declare(strict_types=1);

namespace App\Administration\Infrastructure\Api\Resource;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use App\Administration\Application\Query\GetStudentsImageRights\StudentImageRightsDto;
use App\Administration\Infrastructure\Api\Processor\UpdateImageRightsProcessor;
use App\Administration\Infrastructure\Api\Provider\ImageRightsCollectionProvider;
use App\Administration\Infrastructure\Api\Provider\ImageRightsExportProvider;
use App\Administration\Infrastructure\Api\Provider\ImageRightsItemProvider;
use DateTimeImmutable;
use Symfony\Component\Validator\Constraints as Assert;

#[ApiResource(
    shortName: 'ImageRights',
    operations: [
        new GetCollection(
            uriTemplate: '/students/image-rights',
            provider: ImageRightsCollectionProvider::class,
            name: 'get_students_image_rights',
        ),
        new Get(
            uriTemplate: '/students/image-rights/export',
            provider: ImageRightsExportProvider::class,
            name: 'export_students_image_rights',
        ),
        new Patch(
            uriTemplate: '/students/{id}/image-rights',
            provider: ImageRightsItemProvider::class,
            processor: UpdateImageRightsProcessor::class,
            name: 'update_student_image_rights',
        ),
    ],
)]
final class ImageRightsResource
{
    #[ApiProperty(identifier: true)]
    public ?string $id = null;

    public ?string $firstName = null;

    public ?string $lastName = null;

    public ?string $email = null;

    #[Assert\NotBlank(message: 'Le statut est requis.')]
    #[Assert\Choice(choices: ['authorized', 'refused', 'not_specified'], message: 'Statut invalide.')]
    public ?string $imageRightsStatus = null;

    public ?string $imageRightsStatusLabel = null;

    public ?DateTimeImmutable $imageRightsUpdatedAt = null;

    public ?string $className = null;

    public static function fromDto(StudentImageRightsDto $dto): self
    {
        $resource = new self();
        $resource->id = $dto->id;
        $resource->firstName = $dto->firstName;
        $resource->lastName = $dto->lastName;
        $resource->email = $dto->email;
        $resource->imageRightsStatus = $dto->imageRightsStatus;
        $resource->imageRightsStatusLabel = $dto->imageRightsStatusLabel;
        $resource->imageRightsUpdatedAt = $dto->imageRightsUpdatedAt;
        $resource->className = $dto->className;

        return $resource;
    }
}
