<?php

namespace Modules\Mall\Models;

use App\Models\Model;
use App\Traits\HasCovers;
use App\Traits\OrderByOrderAsc;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Modules\Mall\Models\Traits\HasRegion;
use Modules\Mall\Models\Traits\ShopActions;
use Modules\User\Models\User;
use Modules\User\Traits\BelongsToUser;
use Overtrue\LaravelVersionable\Versionable;

class Shop extends Model
{

    use BelongsToUser,
        Cachable,
        HasCovers,
        HasRegion,
        OrderByOrderAsc,
        Versionable,
        ShopActions,
        SoftDeletes;

    const STATUS_APPLYING = 0;
    const STATUS_NORMAL   = 1;
    const STATUS_REJECT   = 2;
    const STATUS_CLOSE    = 3;

    const STATUS_MAP = [
        self::STATUS_APPLYING => '待审核',
        self::STATUS_NORMAL   => '正常',
        self::STATUS_REJECT   => '驳回申请',
        self::STATUS_CLOSE    => '关闭',
    ];

    const LABEL_MAP = [
        self::STATUS_APPLYING => 'default',
        self::STATUS_NORMAL   => 'success',
        self::STATUS_REJECT   => 'warning',
        self::STATUS_CLOSE    => 'danger',
    ];

    protected $table = 'mall_shops';

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

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

    /**
     * Notes   : 店铺支持的物流
     * @Date   : 2021/5/6 5:20 下午
     * @Author : < Jason.C >
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function expresses(): BelongsToMany
    {
        return $this->belongsToMany(Express::class, 'mall_shop_express')
                    ->withTimestamps();
    }

    /**
     * Notes   : 店铺的商品
     * @Date   : 2021/5/6 5:32 下午
     * @Author : < Jason.C >
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function goods(): HasMany
    {
        return $this->hasMany(Goods::class);
    }

    /**
     * Notes   : 分类
     * @Date   : 2021/5/6 5:32 下午
     * @Author : < Jason.C >
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function categories(): HasMany
    {
        return $this->hasMany(Category::class);
    }

    /**
     * Notes   : 店铺设置的标签
     * @Date   : 2021/5/13 10:34 上午
     * @Author : < Jason.C >
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function tags(): HasMany
    {
        return $this->hasMany(Tag::class);
    }

    /**
     * Notes   : 店铺的品牌
     * @Date   : 2021/5/13 10:35 上午
     * @Author : < Jason.C >
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function brands(): HasMany
    {
        return $this->hasMany(Brand::class);
    }

    /**
     * Notes   : 轮播图
     * @Date   : 2021/5/13 11:34 上午
     * @Author : < Jason.C >
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function banners(): HasMany
    {
        return $this->hasMany(Banner::class);
    }

    /**
     * Notes   : 订单
     * @Date   : 2021/5/6 5:33 下午
     * @Author : < Jason.C >
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function orders(): HasMany
    {
        return $this->hasMany(Order::class);
    }

    /**
     * Notes   : 退款订单
     * @Date   : 2021/5/7 9:16 上午
     * @Author : < Jason.C >
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function refunds(): HasMany
    {
        return $this->hasMany(Refund::class);
    }

    /**
     * Notes   : 获取状态文本
     * @Date   : 2021/5/7 10:57 上午
     * @Author : < Jason.C >
     * @return string
     */
    public function getStatusTextAttribute(): string
    {
        return self::STATUS_MAP[$this->status] ?? '未知状态';
    }

    /**
     * Notes: 退货理由
     * @Author: 玄尘
     * @Date  : 2021/5/18 15:31
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function reasons(): BelongsToMany
    {
        return $this->belongsToMany(Reason::class, 'mall_shop_reasons')
                    ->withTimestamps();
    }

    /**
     * Notes   : 店铺的员工
     * @Date   : 2021/7/1 4:25 下午
     * @Author : < Jason.C >
     */
    public function staffers(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'mall_shop_staffers')
                    ->using(ShopStaffer::class)
                    ->withPivot('job_id')
                    ->withTimestamps();
    }

}
