<?php

declare(strict_types=1);

namespace App\Scolarite\Domain\Model\Homework;

use App\Scolarite\Domain\Exception\PieceJointeInvalideException;
use DateTimeImmutable;

use function in_array;

final class HomeworkAttachment
{
    private const int MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 Mo

    private const array ALLOWED_MIME_TYPES = [
        'application/pdf',
        'image/jpeg',
        'image/png',
    ];

    public function __construct(
        public private(set) HomeworkAttachmentId $id,
        public private(set) string $filename,
        public private(set) string $filePath,
        public private(set) int $fileSize {
            set(int $fileSize) {
                if ($fileSize > self::MAX_FILE_SIZE) {
                    throw PieceJointeInvalideException::fichierTropGros($fileSize, self::MAX_FILE_SIZE);
                }
                $this->fileSize = $fileSize;
            }
        },
        public private(set) string $mimeType {
            set(string $mimeType) {
                if (!in_array($mimeType, self::ALLOWED_MIME_TYPES, true)) {
                    throw PieceJointeInvalideException::typeFichierNonAutorise($mimeType);
                }
                $this->mimeType = $mimeType;
            }
        },
        public private(set) DateTimeImmutable $uploadedAt,
    ) {
    }
}
