<?php

namespace App\Models;

use Spatie\MediaLibrary\HasMedia;
use Spatie\Image\Enums\CropPosition;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

class WhyBest extends Model implements HasMedia
{
    use InteractsWithMedia;

    protected $table = "why_bests";
    protected $fillable = ['title', 'description', 'status', 'sort'];
    protected $casts = [
        'id'          => 'integer',
        'title'       => 'string',
        'description' => 'string',
        'status'      => 'integer',
        'sort'        => 'integer'
    ];

    public function getImageAttribute(): string
    {
        if (!empty($this->getFirstMediaUrl('why-best'))) {
            $image = $this->getMedia('why-best')->last();
            return $image->getUrl('cover');
        }
        return asset('images/default/why-best.png');
    }

    public function registerMediaConversions(Media $media = null): void
    {
        $this->addMediaConversion('cover')->crop(80, 80, CropPosition::Center)->keepOriginalImageFormat()->sharpen(5);
    }
}
