<?php
#app/Middleware/RoleMiddleware.php

namespace Core\Middleware;

use Core\Auth;
use Core\Response;

class RoleMiddleware implements MiddlewareInterface
{
    protected array $allowedRoles;

    public function __construct(array $allowedRoles)
    {
        $this->allowedRoles = $allowedRoles;
    }

    public function handle(string $uri, string $method, array $params): void
    {
        if (!Auth::check()) {
            Response::redirect('/login');
        }

        $user = Auth::user();

        // Check if user's role is in the allowed list
        if (!in_array($user['role'], $this->allowedRoles)) {
            // Log unauthorized attempt if needed
            Response::abort(403, 'Unauthorized', 'You do not have permission to access this area.');
        }
    }
}