<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Session;
use App\Models\UserStore;
use App\Models\Store;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    function __construct()
    {
        $this->middleware(function ($request, $next) {
            $logged = Auth::user();
            if ($logged) {
                //kalo cache ini gaada logout aja
                if (!Cache::has('select_stores') or !Cache::has('menus') or !Cache::has('global_params')) {
                    Session::flush();
                    Auth::logout();
                    return redirect('login')->with('noready', 'Session expired, please login again.')->with('username', $logged->username);
                }
                //set variable auth====================
                //buat di kontroler
                $this->logged_user_id = $logged->id;
                $this->logged_username = $logged->username;
                $this->logged_name = $logged->fullname;
                $this->logged_email = $logged->email;
                $this->f_need_change_password = $logged->f_need_change_password; //flagging need change password
                $this->logged_role_id = 0;
                $this->logged_role_name = '';
                //buat di view
                View::share('logged_user_id', $this->logged_user_id);
                View::share('logged_username', $this->logged_username);
                View::share('logged_name', $this->logged_name);
                View::share('logged_email', $this->logged_email);
                View::share('f_need_change_password', $this->f_need_change_password);

                if ($logged->roles->pluck('name')->toArray()) {
                    $this->logged_role_id = ucwords($logged->roles->pluck('id')->toArray()[0]);
                    $this->logged_role_name = ucwords($logged->roles->pluck('name')->toArray()[0]);
                    View::share('logged_role_id', $this->logged_role_id);
                    View::share('logged_role_name', $this->logged_role_name);
                } else {
                    //kalo tidak punya peran/roles dipaksa logout
                    Session::flush();
                    Auth::logout();
                    return redirect('login')->with('noready', 'Peran (roles) pengguna belum ditentukan! silahkan hubungi administrator')->with('username', $logged->username);
                }
                //====================set variable auth

                //deteksi pernah di reset password====================
                if ($this->f_need_change_password && $this->f_need_change_password != '' && !in_array(request()->route()->getName(), array('logout.perform', 'users.change-password', 'users.store'))) {
                    //kalo abis di reset password paksa buat bikin password baru
                    return redirect()->route('users.change-password', $this->logged_user_id);
                }
                //====================deteksi pernah di reset password

                //label saldo==================== 
                $store_name = '';
                if (session('store_selected')) {
                    $store = Store::select('name')
                        ->where('id', session('store_selected'))
                        ->first();

                    if ($store) {
                        $store_name = $store->name;
                    }
                }
                View::share('store_name', $store_name);
                //====================label saldo 
            }

            return $next($request);
        });
    }
}
