<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

class AdminRedirect
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {

        if (!Auth::check() && $request->path() != 'admin/register') {
            return redirect()->route('admin.login');
        }
        if (Auth::check() && $request->path() == 'admin/login' && Auth::user()->can_login == 1) {
            return redirect()->route('admin.index');
        }

        // $user = Auth::user();
        // if ($user->email == null) {
        //     return redirect()->route('admin.profile');
        // }

        return $next($request);
    }
}
