<?php

namespace app\controllers;

use app\models\Bidding;
use Yii;
use app\models\Notification;
use yii\rest\ActiveController;
use yii\data\ActiveDataProvider;

class NotificationController extends ActiveController
{

    public $modelClass = 'app\models\Notification';

    public function actions()
    {
        return [
            'view' => [
                'class' => 'yii\rest\ViewAction',
                'modelClass' => $this->modelClass,
                'checkAccess' => [$this, 'checkAccess'],
            ],
            'create' => [
                //'class' => 'yii\rest\CreateAction',
                'modelClass' => $this->modelClass,
                'checkAccess' => [$this, 'checkAccess'],
                'scenario' => $this->createScenario,
            ],
            'update' => [
                //'class' => 'yii\rest\UpdateAction',
                'class' => 'app\controllers\UpdateAction',
                'modelClass' => $this->modelClass,
                'checkAccess' => [$this, 'checkAccess'],
                'scenario' => $this->updateScenario,
            ],
            'delete' => [
                //'class' => 'yii\rest\DeleteAction',
                'modelClass' => $this->modelClass,
                'checkAccess' => [$this, 'checkAccess'],
            ],
            'options' => [
                'class' => 'yii\rest\OptionsAction',
            ],
        ];
    }
    public function checkAccess($action, $model = null, $params = [])
    {
        // check if the user can access $action and $model
        // throw ForbiddenHttpException if access should be denied
    }

    public function actionIndex(){
        return [
            'count_messages' => Notification::find()->where(['user_id' => Yii::$app->user->id, 'status' => 0])->count(),
            'count_bids' => Bidding::find()->where([(Yii::$app->user->can('member') ? 'user_id' : 'org_id') => Yii::$app->user->id, 'readed' => 0])->count(),
        ];
    }

}
