<?php

namespace twa\ecomauth\middleware;

use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode as Middleware;
use Closure;
use twa\ecomauth\controllers\v1\ProfileController;
use Illuminate\Support\Facades\Session;

class AuthCheckProfileComplete extends Middleware
{
    /**
     * The URIs that should be reachable while maintenance mode is enabled.
     *
     * @var array
     */
    protected $except = [
        //
    ];

    public function handle($request, Closure $next)
    {

        if((new ProfileController)->requiresCompleteProfile()){
            if(request()->input('complete_profile') != 1){
                return redirect('/'.request()->segment(1).'/'.request()->segment(2).'/login?complete_profile=1');
            }
        }elseif(Session::get('login') && request()->segment(3) == "login"){
            return redirect('/'.request()->segment(1).'/'.request()->segment(2));
        }

        return $next($request);
    }
}
