<?php

namespace App\Http\Middleware;

use App\Models\AppSession;
use Closure;

class EnsureSuperAdmin
{
    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 abort(403);
        }

        return $next($request);
    }
}
