<?php

namespace AutoAdmin\Bundle\Media\Namer;

use AutoAdmin\Bundle\Core\Slugger\Slugger;
use AutoAdmin\Bundle\Media\Helper\FileHelper;

class MediaNamer implements NamerInterface
{
    private Slugger $slugger;

    public function __construct(Slugger $slugger)
    {
        $this->slugger = $slugger;
    }

    public function generateName(string $originalName): string
    {
        $extension = FileHelper::extractExtension($originalName);
        $fileName = FileHelper::extractName($originalName);
        $fileName = $this->slugger->slugify($fileName);

        $hash = substr(md5(uniqid('', true)), 0, 4);

        return $fileName.'-'.$hash.'.'.$extension;
    }
}
