<?php

declare(strict_types=1);

namespace App\Scolarite\Infrastructure\Api\Resource;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use App\Scolarite\Application\Query\GetReplacedClassesForTeacher\ReplacedClassDto;
use App\Scolarite\Infrastructure\Api\Provider\ReplacedClassesProvider;

/**
 * Classes/matières pour lesquelles l'enseignant connecté est remplaçant.
 */
#[ApiResource(
    shortName: 'ReplacedClass',
    operations: [
        new GetCollection(
            uriTemplate: '/me/replaced-classes',
            provider: ReplacedClassesProvider::class,
            name: 'get_replaced_classes',
        ),
    ],
)]
final class ReplacedClassResource
{
    #[ApiProperty(identifier: true)]
    public string $id;

    public string $replacementId;

    public string $replacedTeacherId;

    public string $classId;

    public string $subjectId;

    public string $className;

    public string $subjectName;

    public string $startDate;

    public string $endDate;

    public static function fromDto(ReplacedClassDto $dto): self
    {
        $resource = new self();
        $resource->id = $dto->replacementId . '_' . $dto->classId . '_' . $dto->subjectId;
        $resource->replacementId = $dto->replacementId;
        $resource->replacedTeacherId = $dto->replacedTeacherId;
        $resource->classId = $dto->classId;
        $resource->subjectId = $dto->subjectId;
        $resource->className = $dto->className;
        $resource->subjectName = $dto->subjectName;
        $resource->startDate = $dto->startDate->format('Y-m-d');
        $resource->endDate = $dto->endDate->format('Y-m-d');

        return $resource;
    }
}
