<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\data\ActiveDataProvider;
use app\models\TrainingRequests;
use app\models\Trainings;
use app\models\Branches;
use app\models\Interviews;
use yii\filters\AccessControl;

class DashboardController extends Controller
{
    public function behaviors(){
        return [
            'access' => [
                'class' => AccessControl::class,
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@']
                    ]
                ],
                'denyCallback' => function(){
                    if(Yii::$app->user->isGuest){
                        return Yii::$app->response->redirect(['/users/login']);
                    }
                    else{
                        return Yii::$app->response->redirect(['index']);
                    }
                }
            ]
        ];
    }

    public function actionIndex(){
        $authenticated_role = Yii::$app->user->identity->role->item_name;
        if($authenticated_role == 'Admin'){
            return $this->render('admin', [
                'pendingRequestsDataProvider' => $this->getPendingRequestsDataProvider(),
                'todayInterviewsDataProvider' => $this->getTodayInterviewsDataProvider()
            ]);
        }
        elseif($authenticated_role == 'Branch Admin'){
            $branches_admin = Branches::find()->where(['branch_user_id' => Yii::$app->user->getId()])->all();
            $branches_IDs = [];
            if($branches_admin != null){
                foreach($branches_admin as $branch){
                    $branches_IDs[] = $branch->id;
                }
                return $this->render('branch-admin', [
                    'requestsToEvaluateDataProvider' => $this->getRequestsToEvaluateDataProvider($branches_IDs)
                ]);
            }
            else{
                return $this->render('branch-admin', [
                    'requestsToEvaluateDataProvider' => null
                ]);
            }
            
        }
        else{
            $request = TrainingRequests::find()->where(['trainee_user_id' => Yii::$app->user->getId()])->one();
            if(! $request){
                return $this->redirect(['/training-requests/create']);
            }
            return $this->render('user', [
                'requests' => $this->getUserRequests()
            ]);
        }
    }

    public function getPendingRequestsDataProvider(){
        $pendingRequestsDataProvider = new ActiveDataProvider();
        $pendingRequestsDataProvider->query = TrainingRequests::find()
            ->where(['training_request_status' => 'Pending'])
            ->orWhere(['training_request_status' => 'Reactivated']);
        $pendingRequestsDataProvider->pagination->pageSize = 10;
        $pendingRequestsDataProvider->setSort([
            'defaultOrder' => [
                'create_date' => SORT_DESC
            ]
        ]);

        return $pendingRequestsDataProvider;
    }

    public function getTodayInterviewsDataProvider(){
        $todayInterviewsDataProvider = new ActiveDataProvider();
        $todayInterviewsDataProvider->query = Interviews::find()
            ->innerJoin('training_requests', 'training_requests.id = interviews.training_request_id')
            ->where(['between', 'date', date('Y-m-d 00:00:00'), date('Y-m-d 23:59:58')])
            ->andWhere(['training_requests.training_request_status' => 'Interview']);
        $todayInterviewsDataProvider->pagination->pageSize = 10;
        $todayInterviewsDataProvider->setSort([
            'attributes' => [
                'serial_number' => [
                    'asc'  => ['training_requests.serial_number' => SORT_ASC],
                    'desc' => ['training_requests.serial_number' => SORT_DESC]
                ],
                'trainee_full_name' => [
                    'asc'  => ['training_requests.full_name' => SORT_ASC],
                    'desc' => ['training_requests.full_name' => SORT_DESC]
                ],
                'trainee_mother_name' => [
                    'asc'  => ['training_requests.mother_name' => SORT_ASC],
                    'desc' => ['training_requests.mother_name' => SORT_DESC]
                ],
                'create_date' => [
                    'asc'  => ['training_requests.create_date' => SORT_ASC],
                    'desc' => ['training_requests.create_date' => SORT_DESC]
                ],
                'date'
            ]
        ]);

        return $todayInterviewsDataProvider;
    }

    public function getRequestsToEvaluateDataProvider($branches_IDs){
        $requestsDataProvider = new ActiveDataProvider();
        $requestsDataProvider->query = Trainings::find()
            ->innerJoin('training_requests', 'training_requests.id = trainings.training_request_id')
            ->where(['training_request_status' => 'Approved'])
            ->andWhere(['in', 'trainings.branch_id', $branches_IDs])
            ->andWhere(['<=', 'trainings.end_date', date('Y-m-d')]);
        $requestsDataProvider->pagination->pageSize = 10;
        $requestsDataProvider->setSort([
            'attributes' => [
                'serial_number' => [
                    'asc'  => ['training_requests.serial_number' => SORT_ASC],
                    'desc' => ['training_requests.serial_number' => SORT_DESC]
                ],
                'trainee_full_name' => [
                    'asc'  => ['training_requests.full_name' => SORT_ASC],
                    'desc' => ['training_requests.full_name' => SORT_DESC]
                ],
                'trainee_mother_name' => [
                    'asc'  => ['training_requests.mother_name' => SORT_ASC],
                    'desc' => ['training_requests.mother_name' => SORT_DESC]
                ],
                'end_date'
            ]
        ]);

        return $requestsDataProvider;
    }

    public function getUserRequests(){
        return TrainingRequests::find()->where(['trainee_user_id' => Yii::$app->user->getId()])->all();
    }
}