<?php

namespace App\Models\Goods;

use App\Base\BaseModel;
use Faker\Provider\Image;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Models\Goods\Category
 *
 * @property int                             $id
 * @property string                          $name
 * @property string                          $thumb
 * @property int                             $level 层级
 * @property int                             $parent_id 上级分类ID
 * @property int                             $weight 权重
 * @property int                             $index_weight 推荐位权重
 * @property int                             $category_weight 推荐位权重
 * @property int                             $search_weight 搜索页权重
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @method static \Illuminate\Database\Eloquent\Builder|Category newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Category newQuery()
 * @method static \Illuminate\Database\Query\Builder|Category onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|Category query()
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereCategoryWeight($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereIndexWeight($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereLevel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereParentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereSearchWeight($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereThumb($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereWeight($value)
 * @method static \Illuminate\Database\Query\Builder|Category withTrashed()
 * @method static \Illuminate\Database\Query\Builder|Category withoutTrashed()
 * @mixin \Eloquent
 * @property-read Category|null                                                                 $parent
 * @property-read \Illuminate\Database\Eloquent\Collection|Category[]                           $childS
 * @property-read int|null                                                                      $child_s_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Goods\GoodsCategoryMap[] $gcMap
 * @property-read int|null                                                                      $gc_map_count
 * @property string                                                                             $en_name
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereEnName($value)
 */
class Category extends BaseModel
{
    use SoftDeletes;

    protected $table = "category";
    protected $fillable = ["name", "thumb", "level", "parent_id", "weight", "index_weight", "category_weight", "search_weight"];

    public function format(...$options)
    {
        $return = [
            "id" => $this->id,
            "name" => $this->name,
            "en_name" => $this->en_name,
            "thumb" => $this->thumb,
            "level" => $this->level,
            "parent_id" => optional($this->parent)->id ?? 0,
            "parent_name" => optional($this->parent)->name ?? 0,
            "weight" => $this->weight,
            "index_weight" => $this->index_weight,
            "category_weight" => $this->category_weight,
            "search_weight" => $this->search_weight,
        ];

        return $return;
    }

    public function parent()
    {
        return $this->belongsTo(Category::class, "parent_id");
    }

    public function childS()
    {
        return $this->hasMany(Category::class, "parent_id", "id");
    }

    public function gcMap()
    {
        return $this->hasMany(GoodsCategoryMap::class, "category_id", "id");
    }
}