<?php

namespace Modules\Cms\Models;

use App\Models\Model;
use App\Traits\HasCovers;
use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Overtrue\LaravelVersionable\Versionable;

class Category extends Model
{

    use AdminBuilder,
        Cachable,
        HasCovers,
        ModelTree,
        Versionable,
        SoftDeletes;

    protected $table = 'cms_categories';

    protected $casts = [
        'pictures' => 'json',
    ];

    /**
     * 不参与版本记录的字段
     *
     * @var array|string[]
     */
    protected array $dontVersionable = ['updated_at'];

    public function children(): HasMany
    {
        return $this->hasMany(self::class, 'parent_id');
    }

    /**
     * Notes   : 分类下的文章
     *
     * @Date   : 2021/4/15 12:47 下午
     * @Author : < Jason.C >
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function articles(): BelongsToMany
    {
        return $this->belongsToMany(Article::class, (new ArticleCategory())->getTable())
            ->using(ArticleCategory::class)
            ->withTimestamps();
    }

    /**
     * Notes: 健康文章
     *
     * @Author: 玄尘
     * @Date: 2022/10/31 10:05
     */
    public function scopeHealth(Builder $query): Builder
    {
        return $query->where('slug', 'HEALTH');
    }

}