<?php
namespace PortalPeople\oauth2\controllers;

use Yii;
use yii\rest\Controller;

use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBasicAuth;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\auth\QueryParamAuth;

use backend\models\Settings;
use backend\models\Cartsbe;
use backend\models\Cartlines;
use backend\models\Productsbe;
use backend\models\Categoriesbe;
use backend\models\Reportsbe;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

use yii\db\query;
use yii\db\JsonExpression;
use yii\db\Expression;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

use OAuth2\Server;
use OAuth2\Storage\Pdo;
use OAuth2\GrantType\ClientCredentials;
use OAuth2\GrantType\AuthorizationCode;

/**
 * SiteController implements the CRUD actions for Visitors model.
 */
class SiteController extends Controller
{
    /**
     * 
     */
    public function actionToken()
    {
        Yii::$app->response->format = 'json';
        $request = \OAuth2\Request::createFromGlobals();
        $response = Yii::$app->getModule('oauth2')->getServer()->handleTokenRequest($request);
        return $response->getParameters();
    }
    public function actionRevoke()
    {
        Yii::$app->response->format = 'json';
        $module = Yii::$app->getModule('oauth2');
        $request = \OAuth2\Request::createFromGlobals();
        $response = $module->server->handleRevokeRequest($request);
        return $response->getParameters();
    }

    public function actionUserinfo()
    {
        Yii::$app->response->format = 'json';
        $module = Yii::$app->getModule('oauth2');
        $request = \OAuth2\Request::createFromGlobals();
        $response = new \OAuth2\Response();
        $scopeRequired = '*';
        yii::warning($request);
        if ($module->server->verifyResourceRequest($request, $response, $scopeRequired)) {
            $response = $module->server->handleUserInfoRequest($request);
            yii::warning($response);
        } 
        return $response->getParameters();
    }

    
    public function actionAuthenticate(){
        if((new \PortalPeople\oauth2\models\OauthUser())->checkUserCredentials(Yii::$app->request->post('username'), Yii::$app->request->post('password'))){
            return ['success' => true];
        }
        return ['success' => false];
    }


    
}