<?php

namespace Tests\Unit\Services\Security;

use App\Services\Security\IpBanCommandService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class IpBanCommandServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_ban_now_updates_blocked_until(): void
    {
        DB::table('ip_attempts')->insert([
            'ip_address' => '10.0.0.9',
            'attempts' => 1,
        ]);

        $service = app(IpBanCommandService::class);
        $ban = $service->banNow(null, '10.0.0.9', 2);

        $this->assertNotNull($ban);
        $this->assertDatabaseHas('ip_attempts', [
            'ip_address' => '10.0.0.9',
        ]);
    }

    public function test_unban_all_resets_active(): void
    {
        DB::table('ip_attempts')->insert([
            'ip_address' => '10.0.0.10',
            'attempts' => 5,
            'blocked_until' => now('UTC')->addHours(3),
        ]);

        $service = app(IpBanCommandService::class);
        $count = $service->unbanAll();

        $this->assertSame(1, $count);
        $this->assertDatabaseHas('ip_attempts', [
            'ip_address' => '10.0.0.10',
            'blocked_until' => null,
            'attempts' => 0,
        ]);
    }
}
