<?php

namespace App\Models\Goods;

use App\Base\BaseModel;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Arr;

/**
 * App\Models\Goods\Spec
 *
 * @property int                                                                        $id
 * @property string                                                                     $name
 * @property array                                                                      $extra
 * @property int                                                                        $index_weight
 * @property int                                                                        $category_weight
 * @property int                                                                        $search_weight
 * @property int                                                                        $is_custom 是否自定义
 * @property \Illuminate\Support\Carbon|null                                            $created_at
 * @property \Illuminate\Support\Carbon|null                                            $updated_at
 * @property \Illuminate\Support\Carbon|null                                            $deleted_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Goods\SpecAttr[] $attrs
 * @property-read int|null                                                              $attrs_count
 * @method static \Illuminate\Database\Eloquent\Builder|Spec newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Spec newQuery()
 * @method static \Illuminate\Database\Query\Builder|Spec onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|Spec query()
 * @method static \Illuminate\Database\Eloquent\Builder|Spec whereCategoryWeight($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Spec whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Spec whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Spec whereExtra($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Spec whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Spec whereIndexWeight($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Spec whereIsCustom($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Spec whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Spec whereSearchWeight($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Spec whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|Spec withTrashed()
 * @method static \Illuminate\Database\Query\Builder|Spec withoutTrashed()
 * @mixin \Eloquent
 * @property string                                                                     $en_name
 * @method static \Illuminate\Database\Eloquent\Builder|Spec whereEnName($value)
 */
class Spec extends BaseModel
{
    use SoftDeletes;

    const FORMAT_ATTR = "attr";
    protected $table = "spec";

    public function attrs()
    {
        return $this->hasMany(SpecAttr::class, "spec_id", "id");
    }

    public function format(...$options)
    {
        $return = [
            "id" => $this->id,
            "name" => $this->name,
            "en_name" => $this->en_name,
            "is_custom" => (bool)$this->is_custom,
            "index_weight" => $this->index_weight,
            "category_weight" => $this->category_weight,
            "search_weight" => $this->search_weight,
            "created_at" => $this->created_at->format("Y-m-d"),
        ];

        if (in_array(self::FORMAT_ATTR, $options)) {
            $return['attr'] = $this->attrs->map(function (SpecAttr $m) {
                return [
                    "id" => $m->id,
                    "name" => $m->name,
                ];
            });
        }
        return $return;
    }
}