<?php

namespace App\Models\Shop;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Image\Manipulations;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
use Venturecraft\Revisionable\RevisionableTrait;
use Wildside\Userstamps\Userstamps;

class ArticleNature extends Model implements HasMedia
{
    use HasRelationships, InteractsWithMedia, RevisionableTrait, SoftDeletes, UserStamps;

    protected $guarded = ['id'];

    protected $table = 'shop_article_natures';

    protected $revisionEnabled = true;

    protected $keepRevisionOf = ['product_type', 'name', 'description'];

    public function articles(): HasMany
    {
        return $this->hasMany(Article::class);
    }

    public function scopeByArticleNature($query, $id)
    {
        return $query->where($this->table.'.id', $id);
    }

    public function scopeBySlug($query, $slug)
    {
        return $query->where($this->table.'.slug', $slug);
    }

    public function scopeByBotanic($query)
    {
        return $query->ByProductType(1);
    }

    public function scopeByMerchandise($query)
    {
        return $query->ByProductType(2);
    }

    public function scopeByProductType($query, $type)
    {
        return $query->where($this->table.'.product_type', $type);
    }

    public function scopeByIds($query, $ids)
    {
        return $query->whereIn($this->table.'.id', $ids);
    }

    public function registerMediaConversions(?Media $media = null): void
    {
        $this->addMediaConversion('thumb')->fit(Manipulations::FIT_MAX, 60, 32)->keepOriginalImageFormat()->nonQueued();
        $this->addMediaConversion('normal')->fit(Manipulations::FIT_MAX, 360, 192)->keepOriginalImageFormat()->nonQueued();
    }
}
