<?php

namespace twa\ecomauth\middleware;

use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode as Middleware;
use Closure;
use Illuminate\Support\Facades\Session;
use twa\apilibs\traits\APITrait;
use twa\ecomauth\repositories\AuthenticateRepository;
use twa\ecomauth\repositories\CommonRepository;

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

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

        $CommonRepository = (new CommonRepository);
        $AuthRepository = (new AuthenticateRepository);

        $user = Session::get('login');
        if(!isset($user['id'])){ return redirect()->route('account-auth'); }

        $user = $AuthRepository->getUserById($user['id']);
        if(!$user){ return redirect()->route('account-auth'); }

        $check_complete = $CommonRepository->checkProfileComplete($user);

        if(count($check_complete) > 0){
            return redirect()->route('complete-profile');
        }

        return $next($request);
    }
}
