<?php


namespace App\Models\Traits;

use App\Models\SettingManagement\Image;
use App\Traits\ImageUploadTrait;
use Illuminate\Database\Eloquent\Relations\MorphMany;

trait HasMedias{
  use ImageUploadTrait;

    /**
     * @return MorphMany
     */
    public function media(){
        return $this->morphMany(Image::class, 'mediable');
    }

    public function firstMedia(){
        return $this->morphOne(Image::class, 'mediable')
            ->orderBy('file_sort', 'asc');
    }

    /**
     * @return MorphMany
     */
    public function medias(){
        return $this->morphMany(Image::class, 'mediable');
    }

    public function images(){

    }
    public function videos(){

    }

    public function documents(){

    }

    public function pdfs(){

    }

    public function createMedia($files , $i = 1,$width = null, $height = null){
        foreach ($files as $file) {
            $this->medias()->create([
                'file_name' => $this->uploadImages($this->name, $file, $i, $this->getMorphClass(), $width, $height),
                'file_size' => $file->getSize(),
                'file_type' => $file->getMimeType(),
                'file_status' => true,
                'file_sort' => $i
            ]);
            $i++;
        }
    }
}
