<?php

namespace App\Traits\Model;

use Spatie\Image\Manipulations;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\InteractsWithMedia;

trait Imageable
{
    use InteractsWithMedia;

    public function images()
    {
        return $this->hasMany(Media::class, 'model_id')->where('model_type', get_class($this));
    }

    public function image()
    {
        return $this->hasOne(Media::class, 'model_id')->where('model_type', get_class($this))->latest();
    }

    public function registerMediaConversions(Media $media = null) : void
    {
        $this->addMediaConversion('thumb')->fit(Manipulations::FIT_CROP, 32, 32);
        $this->addMediaConversion('mini')->fit(Manipulations::FIT_CROP, 96, 96);
        $this->addMediaConversion('preview')->fit(Manipulations::FIT_CROP, 160, 160);
        $this->addMediaConversion('normal')->fit(Manipulations::FIT_CROP, 480, 480);
        // $this->addMediaConversion('zoom')->fit(Manipulations::FIT_CROP, 1200, 1200)->withResponsiveImages();
    }
}
