<?php

namespace App\Models\User;

use App\Exceptions\AlertException;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

/**
 * App\Models\User\AuthKey
 *
 * @property int $id 主键
 * @property int $uid 用户id
 * @property string $auth_key 认证key
 * @property string $auth_type 认证类型
 * @property \Illuminate\Support\Carbon $created_at 绑定时间
 * @property \Illuminate\Support\Carbon $updated_at 登录时间
 * @property int $login_at 上次登录时间
 * @property string|null $attach 附加信息
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey whereAttach($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey whereAuthKey($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey whereAuthType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey whereLoginAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class AuthKey extends Authenticatable implements JWTSubject
{
    use Notifiable;

    protected $table = 'kdgx_user_auth_key';

    protected $fillable = [
        'uid',
        'auth_key',
        'auth_type',
        'attach'
    ];

    protected $dateFormat = 'U';

    protected $primaryKey = 'id';

    /**
     * 获取用户的密码。
     *
     * @return string
     */
    public function getAuthPassword()
    {
        $hash = new \Illuminate\Hashing\BcryptHasher();
        return $hash->make($this->auth_key);
    }

    /**
     * 获取将存储在JWT主题声明中的标识符。
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->auth_key;
    }

    public function getAuthIdentifierName()
    {
        return 'auth_key';
    }

    /**
     * 返回一个键值数组，其中包含要添加到JWT的任何自定义声明。
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        $user = UserModel::find($this->uid);
        return [
            'nickname' => $user->nickname,
            'headimgurl' => $user->headimgurl,
            'phone' => $user->phone,
            'uid' => $this->uid
        ];
    }

    public function validateCredentials($auth_key)
    {
        $data = $this->where(['auth_key' => $auth_key])->first()->toArray();
        if (empty($data)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 获取某种登录方式的key
     * @param  int $uid 用户id
     * @param  string $auth_type 登陆方式
     * @return string auth_key
     * @throws AlertException
     */
    public function getKeyOfType(int $uid, string $auth_type)
    {
        $auth = $this->where(array(['uid', $uid], ['auth_type', $auth_type]))->first();
        if (collect($auth)->isEmpty()) {
            throw new AlertException("未找到登录方式", 404);
        } else {
            return $auth->auth_key;
        }
    }

    /**
     * 获取用户uid
     * @param  string  $auth_type
     * @return array
     * @throws AlertException
     */
    public function getUidByKey(string $auth_type)
    {
        $auth = $this->where('auth_key', $auth_type)->first();
        if (collect($auth)->isEmpty()) {
            throw new AlertException("未绑定", 404);
        } else {
            return $auth->uid;
        }
    }
}
