<?php

namespace AuthStack\Driver\AUDGeneric;

use AuthStack\Driver\Model\User;
use function Buckhill\config as cfg;
use AuthStack\Driver\Model\User as UserModel;
use AuthStack\Driver\Errors\NoAuthSourceError;
use AuthStack\Driver\Errors\InsufficientCredentialsError;

abstract class AbstractDriver
{
    /**
     * @var string
     */
    protected $cfg_key;


    /**
     * @var User
     */
    protected $user;

    /**
     * @var User
     */
    protected $provisioned;

    /**
     * @var null|int
     */
    protected $authsource_id = null;

    /**
     * @var null|\PDO
     */
    protected $pdo;

    /**
     * @var string
     */
    protected $last_error = '';

    /**
     * @param      $key
     * @param int|null $authsource_id
     *
     * @return static
     */
    public static function initialize($key, $authsource_id)
    {
        $me = new static();

        $me->cfg_key = $key;

        return $me->setAuthSourceId($authsource_id);
    }

    /**
     * We don't need to elevate privileges using MySQL usually, so we'll leave this method to return true
     *
     * @return bool
     */
    public function elevate()
    {
        return true;
    }

    /**
     * @param $id
     *
     * @return $this
     */
    public function setAuthSourceId($id)
    {
        $this->authsource_id = $id;

        return $this;
    }

    /**
     * @return int|null
     *
     * @throws NoAuthSourceError
     */
    public function getAuthSourceId()
    {
        if (is_null($this->authsource_id)) {
            throw new NoAuthSourceError('No authsource_id has been set. Unable to retrieve authsource_id.');
        }

        return $this->authsource_id;
    }

    /**
     * @return string
     */
    public function lastError()
    {
        return $this->last_error;
    }

    /**
     * @param $username
     *
     * @throws NoAuthSourceError
     */
    protected function provision($username)
    {
        $this->provisioned = UserModel::retrieve([
            'conn_id' => $this->getAuthSourceId(),
            'username' => $username
        ]);
    }

    /**
     * @return UserModel
     */
    protected function getProvisionedUser()
    {
        return $this->provisioned;
    }

    /**
     * @param array $credentials
     *
     * @throws InsufficientCredentialsError
     */
    protected function check(array $credentials)
    {
        if (!isset($credentials['username'])) {
            throw new InsufficientCredentialsError('Insufficient credentials: username is required');
        }

        if (!isset($credentials['password'])) {
            throw new InsufficientCredentialsError('Insufficient credentials: password is required');
        }
    }

    /**
     * @param $username
     *
     * @return string
     */
    protected function getUniqueId($username)
    {
        return sha1(sprintf("%d.%s", $this->getAuthSourceId(), $username), true);
    }

    /**
     * @param      $key
     * @param null $default
     *
     * @return string|int|float|array|null
     */
    protected function config($key, $default = null)
    {
        return cfg(sprintf('%s.%s', $this->cfg_key, $key), $default);
    }
}
