<?php

declare(strict_types=1);

namespace App\Scolarite\Application\Query\GetStudentHomework;

use App\Scolarite\Domain\Model\Homework\Homework;
use App\Scolarite\Domain\Model\Homework\HomeworkAttachment;

use function array_map;

final readonly class StudentHomeworkDetailDto
{
    /**
     * @param array<AttachmentDto> $attachments
     */
    public function __construct(
        public string $id,
        public string $subjectId,
        public string $subjectName,
        public ?string $subjectColor,
        public string $teacherId,
        public string $teacherName,
        public string $title,
        public ?string $description,
        public string $dueDate,
        public string $createdAt,
        public array $attachments,
    ) {
    }

    /**
     * @param array<HomeworkAttachment> $attachments
     */
    public static function fromDomain(
        Homework $homework,
        string $subjectName,
        ?string $subjectColor,
        string $teacherName,
        array $attachments,
    ): self {
        return new self(
            id: (string) $homework->id,
            subjectId: (string) $homework->subjectId,
            subjectName: $subjectName,
            subjectColor: $subjectColor,
            teacherId: (string) $homework->teacherId,
            teacherName: $teacherName,
            title: $homework->title,
            description: $homework->description,
            dueDate: $homework->dueDate->format('Y-m-d'),
            createdAt: $homework->createdAt->format('Y-m-d\TH:i:sP'),
            attachments: array_map(
                static fn (HomeworkAttachment $a): AttachmentDto => new AttachmentDto(
                    id: (string) $a->id,
                    filename: $a->filename,
                    fileSize: $a->fileSize,
                    mimeType: $a->mimeType,
                ),
                $attachments,
            ),
        );
    }
}
