<?php

declare(strict_types=1);

namespace App\Scolarite\Application\Query\GetActiveReplacements;

use App\Scolarite\Domain\Model\TeacherReplacement\TeacherReplacement;
use DateTimeImmutable;

final readonly class ReplacementDto
{
    /** @param array<array{classId: string, subjectId: string}> $classes */
    public function __construct(
        public string $id,
        public string $replacedTeacherId,
        public string $replacementTeacherId,
        public DateTimeImmutable $startDate,
        public DateTimeImmutable $endDate,
        public string $status,
        public array $classes,
        public ?string $reason,
    ) {
    }

    public static function fromDomain(TeacherReplacement $replacement): self
    {
        return new self(
            id: (string) $replacement->id,
            replacedTeacherId: (string) $replacement->replacedTeacherId,
            replacementTeacherId: (string) $replacement->replacementTeacherId,
            startDate: $replacement->startDate,
            endDate: $replacement->endDate,
            status: $replacement->status->value,
            classes: array_map(
                static fn ($pair) => [
                    'classId' => (string) $pair->classId,
                    'subjectId' => (string) $pair->subjectId,
                ],
                $replacement->classes,
            ),
            reason: $replacement->reason,
        );
    }
}
