<?php

namespace Tests\Feature\Api;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\RateLimiter;
use Tests\TestCase;

class RateLimitTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        RateLimiter::clear('auth');
        RateLimiter::clear('agent-register');
    }

    public function test_login_is_rate_limited_after_5_attempts(): void
    {
        for ($i = 0; $i < 5; $i++) {
            $this->postJson('/api/auth/login', ['email' => 'a@b.com', 'password' => 'wrong'])
                ->assertStatus(422);
        }

        $this->postJson('/api/auth/login', ['email' => 'a@b.com', 'password' => 'wrong'])
            ->assertStatus(429);
    }

    public function test_agent_register_is_rate_limited_after_1_per_minute(): void
    {
        $this->postJson('/api/agent/register', ['hostname' => 'A', 'os_info' => 'X', 'local_ip' => '1.1.1.1'])->assertCreated();
        $this->postJson('/api/agent/register', ['hostname' => 'B', 'os_info' => 'X', 'local_ip' => '1.1.1.1'])->assertStatus(429);
    }
}
