<?php
namespace App\Services\Upload;

use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;

/**
* Class  Helper to upload
*/
Class UploadService
{
    public static $disk = 'public';
    public static $root = 'main';
    public static function setRoot()
    {
        // self::$root= 'tenant_'.request()->user()->code;
    }

    /**
     * Helper to upload
     * @param mixed $file File to upload
     * @param string $oldFile
     * @return string
     */
    public static function upload( $file, $oldFile= null, $folder= ''): ?string
    {
        if(!$file) {
            return null;
        }
        self::setRoot();
        // check mime
        $mime= $file->getMimeType();
        // dump($mime, config('lfm.folder_categories.file.valid_mime'));
        if(!in_array($mime, config('lfm.folder_categories.file.valid_mime'))){
            abort(400, ' INVALID FILE ');
            return NULL;
        }
        // dd('upload disk=>'.self::$disk);
        $path= pathinfo($file->getClientOriginalName());
        $ext = $path['extension']; //$file->extension();
        // $name= rand().'-'.str_slug($path['filename']).'.' . $ext;
        $name= str_slug($path['filename']).'-'.time().rand().'.' . $ext;
        // dd($name);
        $folder= self::$root.'/'.$folder;
        // dd($folder);
        // $uploadedFile= $file->storeAs('/'.$folder, $name,['disk' => 'public_uploads']);
        $uploadedFile= $file->storeAs('/'.$folder, $name,['disk' => self::$disk]);
        //
        // delete old file
        if ($oldFile){
            if(Storage::disk(self::$disk)->exists($oldFile)) {
                Storage::disk(self::$disk)->delete($oldFile);
            }
            if(is_file(storage_path(self::$disk."/".$oldFile))){
                File::delete(storage_path(self::$disk."/".$oldFile));
            }
        }
        // dd($uploadedFile);
        return $uploadedFile;
    }


    /**
     * Check file Exists
     */
    public static function exists(string $file): bool
    {
        if ($file && Storage::disk(self::$disk)->exists($file)) {
            return true;
        }
        return false;
    }

    /**
     * Check file type
     */
    public static function type(string $file)//: string
    {
        $fileExists= false;
        if(self::exists($file)){
            $file= self::path($file);
            $fileExists= true;
        }elseif($file = self::path($file)){
            $fileExists= true;
        }
        if ($fileExists) {
            $mimetype = explode('/', File::mimeType($file))[0] ?? 'file';
            $mimetype = 'image' == $mimetype ? 'image' : ('audio' == $mimetype ? 'audio' : ('video' == $mimetype ? 'video' : pathinfo($file)['extension']));
            return $mimetype;
        }
        return null;
    }
    /**
     * Delete file
     */
    public static function delete(string $file): ?bool
    {
        if (self::exists($file)) {
            return Storage::disk(self::$disk)->delete($file);
        }
        return false;
    }

    /**
     * Url of the file
     */
    public static function url($file): ?string
    {
        // dump($file , Storage::disk(self::$disk)->exists($file), Storage::disk(self::$disk)->url($file));
        if ($file && Storage::disk(self::$disk)->exists($file)) {
            return Storage::disk(self::$disk)->url($file);
        }
        return null;
    }
    
    /**
     *  File Path to download it
     * @param string|null $filepath
     */
    public static function path(string $file) :?string
    {
        if ($file && Storage::disk(self::$disk)->exists($file)) {
            // return response()->download(Storage::disk(self::$disk)->path($file), $file->getClientOriginalName());
            return Storage::disk(self::$disk)->path($file);
        }
        return null;
    }
}
?>