<?php

namespace App\Providers;

use App\Models\User;
use Illuminate\Auth\DatabaseUserProvider;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
use Illuminate\Support\Facades\Auth;

class AppUserProvider extends DatabaseUserProvider
{
    /**
     * Retrieve a user by their unique identifier.
     *
     * @param  mixed $identifier
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveById($identifier)
    {
        $user = $this->conn->table($this->table)->where([
            'sys_user_id' => $identifier,
        ])->where('status', '<>', 2)->first();
        $user->api_token = Auth::getPayload()->get('api_token');
        return $this->getGenericUser($user);
    }

    /**
     * Retrieve a user by their unique identifier and "remember me" token.
     *
     * @param  mixed $identifier
     * @param  string $token
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveByToken($identifier, $token)
    {
        return null;
    }

    /**
     * Retrieve a user by the given credentials.
     *
     * @param  array $credentials
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveByCredentials(array $credentials)
    {
        if ($credentials['sys_user_id']) {
            // 用户中心已验证好、直接授权
            $user = [
                'id'=>$credentials['sys_user_id'],
                'sys_user_id'=>$credentials['sys_user_id'],
                'user_name'=>$credentials['user_name'],
                'nick_name'=>$credentials['nick_name'],
                'api_token'=>$credentials['api_token'],
                'password'=>'',
                'phone'=>$credentials['phone'],
                'email'=>$credentials['email'],
                'create_time'=>$credentials['create_time'],
                'update_time'=>$credentials['update_time']
            ];
        } else {
            // 查数据库授权
            $query = $this->conn->table($this->table);
            $user = $query->where('username', $credentials['username'])
                ->where('status', '<>', 2)->first();
        }
        return $this->getGenericUser($user);
    }

    /**
     * Get the generic user.
     *
     * @param  mixed $user
     * @return \Illuminate\Auth\GenericUser|null
     */
    protected function getGenericUser($user)
    {
        if (!is_null($user)) {
            return new User((array)$user);
        }
    }

    protected $model = User::class;

    /**
     * Gets the name of the Eloquent user model.
     *
     * @return string
     */
    public function getModel()
    {
        return $this->model;
    }

    /**
     * Sets the name of the Eloquent user model.
     *
     * @param  string $model
     * @return $this
     */
    public function setModel($model)
    {
        $this->model = $model;

        return $this;
    }

    public function validateCredentials(UserContract $user, array $credentials)
    {
        return true;
    }

}
