<?php

namespace App\Http\Middleware;

use App\Models\User;
use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

class ConfirmUserMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // if (Auth::check() && Route::current()->uri == 'loginkarbar') {
        //     return true;
        // }
        // dd(Auth::user());


        // for migrate

        $usernames_list = [
            "witel",
            "amiriis",
            "drdanesh",
            "khuzestanoffice",
            "alborzoffice",
            "ardabiloffice",
            "azarbayjangharbioffice",
            "azarbayjansharghioffice",
            "bushehroffice",
            "chaharmahalvabakhtiarioffice",
            "farsoffice",
            "ghazvinoffice",
            "gilanoffice",
            "golestanoffice",
            "hamedanoffice",
            "hormozganoffice",
            "ilamoffice",
            "iranshahroffice",
            "isfahanoffice",
            "kermanjonuboffice",
            "kermanoffice",
            "kermanshahoffice",
            "khorasanjonubioffice",
            "khorasanrazavioffice",
            "khorasanshomalioffice",
            "khuzestanoffice",
            "kohgiluyehvaboyerahmadoffice",
            "kordestanoffice",
            "larestanoffice",
            "lorestanoffice",
            "markazioffice",
            "mazandaranoffice",
            "qomoffice",
            "semnangharboffice",
            "semnansharghoffice",
            "sistanvabaluchestanoffice",
            "tehranoffice",
            "yazdoffice",
            "zanjanoffice",

            "pardis",
            "ahvaz",
            "birjand",
        ];
        // if (Auth::check() && !in_array(Auth::user()->username, $usernames_list)) {
        //     return abort(503);
        // }

        // for migrate

        if (Auth::check() && !Auth::user()->enabled) {
            return abort(455);
        }

        if (Auth::check() && Auth::user()->confirmed == 0 && !preg_match('/^valid(.*)/m', Route::current()->uri) && !preg_match('/^logout(.*)/m', Route::current()->uri)) {
            return redirect('/valid/loginkarbar');
        }

        return $next($request);
    }
}
