<?php

namespace App\Http\Middleware;

use App\Models\AppSession;
use Closure;

class EnsureAdminPro
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $sessionKey = $request->cookie('sessionKey');
        $appSession = AppSession::where('session_key', $sessionKey)->where('is_active', true)->first();
        $authenticated = $sessionKey && $appSession && $appSession->pro && $appSession->pro->pro_type == 'ADMIN';
       
        if (!$authenticated) {
            return redirect('/');
        }

        return $next($request);
    }
}
