<?php

namespace App\Http\Controllers;

use App\Http\Requests\LoginRequest;
use App\Models\CreditreportProvider;
use App\Models\Permission;
use App\Models\Support;
use App\Models\User;
use App\Models\VideoSetting;
use Illuminate\Support\Facades\Auth;

class AuthController extends Controller
{
    private $message;
    private $code;
    private $data;
    public function __construct()
    {
        $this->middleware('guest')->except([
            'userLogout',
            'showRegisterForm',
            'register',
            'authRedirect'
        ]);
        $this->message = 'Fail';
        $this->code = config('constant.API_FAILED_CODE');
        $this->data=[];
    }
    public function authRedirect(){
        if(auth()->check()){

            $user_type = auth()->user()->user_type;

            if($user_type == 0){
                return redirect(route('show.manage.clients'));
            }elseif ($user_type == 2){
                $permissions_route_list = getSession('permissions_route_list');
                return redirect(route($permissions_route_list[0]));
            }else {
                return redirect(route('get.clients'));
            }
        }

        return redirect(route('login'));
    }
    public function showLoginForm(){
        return view('auth.login');
    }
    public function login(LoginRequest $request){
        $inputData = $request->only(['email','password']);
        $action='USER_LOGIN';
        $userObjData = (new User())->getAuthenticateUser($inputData['email'],$inputData['password']);

        if(!empty($userObjData)){
            Auth::loginUsingId($userObjData->id);

            if($userObjData->user_type == 0)
            {
                $days = getDateDifference($userObjData->last_import_date,'day');
                setSession('login',1);
                if($days >= config('constant.PACKAGE_INTERVAL_DAY')){
                    setSession('eligible_for_import',1);
                    $userObjData->is_import = config('constant.IMPORT_REPORT_STATUS.ELIGIBLE');
//                    $userObjData->wave = ($userObjData->wave +1);
//                    $userObjData->last_import_date = getCurrentDateTime();
                    $userObjData->save();
                }
                getUserImagesAndDoc($userObjData->id);
                $this->setTrainingVideoLink();
                $this->setReportProviderLink();
                $inputData['id'] = $userObjData->id;
                $inputData['user_type'] = 0;
                $this->setSupport($inputData);
                return redirect(route('show.manage.clients'));
            }else if($userObjData->user_type == 2){
                $permissions_route_list = (new Permission())->getRouteNameListByUserId($userObjData->id);
                setSession('permissions_route_list',$permissions_route_list);
                $inputData['id'] = $userObjData->id;
                $inputData['user_type'] = 1;
                $this->setSupport($inputData);
                return redirect(route($permissions_route_list[0]));
            }
            else{
                $inputData['id'] = $userObjData->id;
                $inputData['user_type'] = 1;
                $this->setSupport($inputData);
                return redirect(route('get.clients'));
            }
        }
        return back()->withErrors(['message'=>'Invalid email or password']);
    }
    public function userLogout(){
        auth()->logout();
        return redirect(route('login'));
    }
    private function setTrainingVideoLink(){

        $key = 'training_video_link';
        $inputData['is_welcome_video'] = 1 ;
        unsetCache($key);
        if(empty(getCache($key))){
            $training_video = (new VideoSetting())->getVideos($inputData)->pluck('link','id');

            if(!empty($training_video)) {
                setCache($key, $training_video);
            }
        }
    }
    private function setReportProviderLink(){
        $key = 'credit_report_provider_link';
        unsetCache($key);
        if(empty(getCache($key))){
            $report_provider = (new CreditreportProvider())->getReportProviders();
            if(!empty($report_provider)) {
                setCache($key, $report_provider);
            }
        }

    }

    private function setSupport($inputData)
    {
//        $support_data = (new Support())->getSupport($inputData);
//        setSession('new_ticket',$support_data->count());
        $support_data = (new Support())->getNotifcation($inputData['id'],$inputData['user_type'])->toArray();
        setSession('new_ticket',$support_data);
    }

}
