<?php


namespace App\Services\MediaLibrary;


use Illuminate\Support\Str;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\Support\PathGenerator\PathGenerator;

class MediaLibraryCustomPathGenerator implements PathGenerator {

    public function getPath(Media $media): string{
       // $key = $media->getKey(); // "1"
        $key = $media->model_id; // "1"
     //   $directory = $media->mediable->getMediaCollectionName(); // "projects"
        $directory = Str::plural($media->model_type); // "projects"
        return "$directory/$key/"; // "projects/1/"
    }

    /*
    * Get the path for conversions of the given media, relative to the root storage path.
    */
    public function getPathForConversions(Media $media): string{
        return $this->getBasePath($media).'/conversions/';
    }

    /*
     * Get the path for responsive images of the given media, relative to the root storage path.
     */
    public function getPathForResponsiveImages(Media $media): string{
        return $this->getBasePath($media).'/responsive-images/';
    }

    /*
    * Get a unique base path for the given media.
    */
    protected function getBasePath(Media $media): string{
        $prefix = config('media-library.prefix', '');
        $key = $media->getKey();
        if ($prefix !== '') {
            return  "$prefix/$key";
        }
        return $key;
    }

}
