<?php

namespace AuthStack\AuthProfile\Provider;

use AuthStack\AuthProfile\Model\AuthProfile;
use Illuminate\Support\ServiceProvider;
use AuthStack\AuthProfile\Facade\ProfileManager;
use AuthStack\Driver\Connection\Manager as ConnectionManager;

class AuthStackUserProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton('AuthStackUser', function($app)
        {
            /** @var  $profile AuthProfile */
            $profile = ProfileManager::getActiveProfile();

            $provisioned = $profile->getProvisionedUser();

            $connection = ConnectionManager::initialize($profile->getConnectionId());

            $connection->elevate();

            $user = $connection->getUserByUsername($provisioned->username);

            return $user;
        });
    }
}
