<?php
namespace PortalPeopleStatis\Firewall;

use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\Middleware\Dispatcher\MiddlewareInterface;
use Yiisoft\Http\ResponseEmitter\ResponseEmitterInterface;
use Yiisoft\Http\Response\Response;

class FirewallMiddleware implements MiddlewareInterface
{
    private array $allowedIps;

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

    public function process(ServerRequestInterface $request, callable $next): Response
    {
        $ip = $request->getServerParams()['REMOTE_ADDR'] ?? '';
        if (!in_array($ip, $this->allowedIps)) {
            // Log blocked access attempt (optional)
            return (new Response())->withStatus(403)->withBody('Forbidden: Your IP is not allowed.');
        }
        return $next($request);
    }
}
