<?php

namespace App\Infrastructure\Security;

use App\Drivers\CacheDriver;

class RateLimiter
{
    public function __construct(private CacheDriver $cache, private array $config)
    {
    }

    public function tooManyAttempts(string $key): bool
    {
        return (int)$this->cache->get('rate:' . $key, 0) >= (int)($this->config['max_login_attempts'] ?? 5);
    }

    public function hit(string $key): int
    {
        return $this->cache->increment('rate:' . $key, (int)($this->config['lockout_duration'] ?? 900));
    }

    public function clear(string $key): void
    {
        $this->cache->forget('rate:' . $key);
    }
}
