<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Slowlyo\OwlAdmin\Models\BaseModel as Model;
use \Laravel\Sanctum\HasApiTokens;

/**
 * 用户
 */
class MemberUser extends Model
{
	use HasApiTokens, SoftDeletes;

	protected $table = 'member_users';
	protected $hidden = ['password'];
	protected $fillable = ['mobile', 'state', 'register_area'];

	protected static function booted()
	{
		static::created(function ($user) {
			$user->wallet()->create([
				'owner_type' => MemberUser::class,
				'owner_id' => $user->id
			]);
		});
	}

    /**
     * @Author FelixYin
     * @description 用户关联社交账户
     */
    public function socialAccounts()
    {
        return $this->hasMany('App\Models\MemberSocialAccount', 'user_id', 'id');
    }

    /**
     * @Author FelixYin
     * @description 用户关联地址
     */
    public function addresses()
    {
        return $this->hasMany('App\Models\MemberAddress', 'user_id', 'id');
    }

    /**
     * @Author FelixYin
     * @description 用户关联技师身份
     */
    public function coach()
    {
        return $this->hasOne('App\Models\CoachUser', 'user_id', 'id');
    }

    /**
     * @Author FelixYin
     * @description 用户关联订单
     */
    public function orders()
    {
        return $this->hasMany('App\Models\OrderInfo', 'user_id', 'id');
    }

    /**
     * @Author FelixYin
     * @description 用户关联评论
     */
    public function comments()
    {
        return $this->hasMany('App\Models\OrderComment', 'user_id', 'id');
    }

    /**
     * @Author FelixYin
     * @description 用户关联钱包
     */
    public function wallet()
    {
        return $this->MORPH_ONE('App\Models\Wallet', 'undefined', 'id');
    }

    /**
     * @Author FelixYin
     * @description 用户关联店铺身份
     */
    public function shop()
    {
        return $this->hasOne('App\Models\ShopInfo', 'user_id', 'id');
    }

    /**
     * @Author FelixYin
     * @description 用户关联代理商身份
     */
    public function agent()
    {
        return $this->hasOne('App\Models\AgentInfo', 'user_id', 'id');
    }

    /**
     * @Author FelixYin
     * @description 会员所属行政区划
     */
    public function region()
    {
        return $this->belongsTo('App\Models\SysRegion', 'register_area');
    }
}
