<?php

namespace App\Middleware;

use App\Enumerations\UserRole;
use App\Providers\AuthCheckProvider;
use Closure;

class AdminRoleMiddleware
{
    public function handle($request, Closure $next)
    {
        if (!auth()->check()) {
            return redirect('/login')->with('message', 'Du musst eingeloggt sein.');
        }

        $authCheck = new AuthCheckProvider();
        $role = $authCheck->getUserRole();

        if (!in_array($role, [UserRole::USER_ROLE_ADMIN, UserRole::USER_ROLE_GROUP_LEADER], true)) {
            return redirect('/')->with('message', 'Du bist dazu nicht berechtigt.');
        }

        return $next($request);
    }
}
