<?php

namespace App\Http\Middleware;

use App\Models\User;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;
use App\Models\AppSession;

class EnsureAdminRole
{
    public function handle(Request $request, Closure $next)
    {

        $sessionKey = Cookie::get(config('app.sessionKeyName'));

        if (!User::isLoggedIn($sessionKey)) return redirect(route('admin.dashboard'));
        $performer = AppSession::where('session_key', $sessionKey)->where('is_active', true)->first();
        if(!$performer->user->is_super_admin){
            dd('You are not an admin');
        }

        return $next($request);
    }
}
