<?php

namespace AutoAdmin\Bundle\Media\Library;

use Symfony\Component\HttpFoundation\File\File;

class MediaFileValidator
{
    public function __construct(
        protected MediaTypeResolver $mediaTypeResolver,
    ) {
    }

    public function validate(string $typeSlug, object $file): array
    {
        $errors = [];
        $typeConfig = $this->mediaTypeResolver->getTypeConfig($typeSlug);
        if (empty($typeConfig)) {
            $errors[] = sprintf('Aucune configuration pour le type "%s".', $typeSlug);

            return [false, $errors];
        }
        if (!$file instanceof File) {
            $errors[] = sprintf('Argument fichier incorrect.');
        }

        if (!in_array($file->getMimeType(), $typeConfig['mime_types'])) {
            $errors[] = sprintf('Type de fichier incorrect.');
        }

        if ($errors) {
            return [false, $errors];
        }

        return [true, []];
    }
}