<?php

declare(strict_types=1);

namespace App\Scolarite\Infrastructure\Api\Resource;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Link;
use App\Scolarite\Domain\Model\Schedule\ResolvedScheduleSlot;
use App\Scolarite\Infrastructure\Api\Provider\ResolvedScheduleWeekProvider;

/**
 * API Resource pour l'emploi du temps résolu (récurrences + exceptions + calendrier).
 *
 * @see Story 4.2 - Récurrences Hebdomadaires
 * @see FR27 - Définir une semaine type qui se répète automatiquement
 */
#[ApiResource(
    shortName: 'ResolvedScheduleSlot',
    operations: [
        new GetCollection(
            uriTemplate: '/schedule/week/{date}',
            uriVariables: [
                'date' => new Link(
                    fromClass: self::class,
                    identifiers: ['date'],
                ),
            ],
            provider: ResolvedScheduleWeekProvider::class,
            name: 'get_resolved_schedule_week',
        ),
    ],
)]
final class ResolvedScheduleSlotResource
{
    #[ApiProperty(identifier: true)]
    public ?string $id = null;

    public ?string $slotId = null;

    public ?string $classId = null;

    public ?string $subjectId = null;

    public ?string $teacherId = null;

    public ?int $dayOfWeek = null;

    public ?string $startTime = null;

    public ?string $endTime = null;

    public ?string $room = null;

    public ?string $date = null;

    public ?bool $isModified = null;

    public ?string $exceptionId = null;

    public static function fromDomain(ResolvedScheduleSlot $resolved): self
    {
        $resource = new self();
        $resource->id = (string) $resolved->slotId . '_' . $resolved->date->format('Y-m-d');
        $resource->slotId = (string) $resolved->slotId;
        $resource->classId = (string) $resolved->classId;
        $resource->subjectId = (string) $resolved->subjectId;
        $resource->teacherId = (string) $resolved->teacherId;
        $resource->dayOfWeek = $resolved->dayOfWeek->value;
        $resource->startTime = $resolved->timeSlot->startTime;
        $resource->endTime = $resolved->timeSlot->endTime;
        $resource->room = $resolved->room;
        $resource->date = $resolved->date->format('Y-m-d');
        $resource->isModified = $resolved->isModified;
        $resource->exceptionId = $resolved->exceptionId !== null ? (string) $resolved->exceptionId : null;

        return $resource;
    }
}
