<?php

namespace App\Models\Shop;

use Rinvex\Tags\Models\Tag as parentTag;

use App\Models\Botanic\Specie;
use App\Models\Botanic\Variety;

class Tag extends parentTag
{

    protected $guarded = ['id'];
    protected $fillable = [
        'slug',
        'name',
        'description',
        'sort_order',
        'group',
        'tag_group_id',
    ];
    public $translatable = [];

    /*
    public function taggable()
    {
        return $this->hasMany('App\Models\Shop\Taggable');
    }
    */

    // TODO
    public function offers()
    {
        return $this->articles();
    }

    public function articles()
    {
        return $this->morphedByMany(Article::class, 'taggable');
    }

    public function categories()
    {
        return $this->morphedByMany(Category::class, 'taggable');
    }

    public function tag_group()
    {
        return $this->belongsTo(TagGroup::class);
    }

    public function shelves()
    {
        return $this->morphedByMany(Shelve::class, 'taggable');
    }

    public function species()
    {
        return $this->morphedByMany(Specie::class, 'taggable');
    }

    public function varieties()
    {
        return $this->morphedByMany(Variety::class, 'taggable');
    }

    public function merchandises()
    {
        return $this->morphedByMany(Merchandise::class, 'taggable');
    }

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

    public function scopeWithCountArticlesByCategory($query, $category_id)
    {
        return $query->withCount(['articles' => function($query) use ($category_id) {
            $query->byCategoryParent($category_id);
        }]);
    }
}
