<?php

use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Str;
use Livewire\Volt\Volt;

it('throttles login after 5 failed attempts', function () {
    $email = 'test@test.com';

    $throttleKey = Str::transliterate(Str::lower($email).'|127.0.0.1');
    RateLimiter::clear($throttleKey);

    for ($i = 0; $i < 5; $i++) {
        Volt::test('pages.auth.login')
            ->set('form.email', $email)
            ->set('form.password', 'wrong'.$i)
            ->call('login');
    }

    $component = Volt::test('pages.auth.login')
        ->set('form.email', $email)
        ->set('form.password', 'wrong6')
        ->call('login');

    $component->assertHasErrors(['form.email']);

    $errors = $component->errors();
    expect($errors->has('form.email'))->toBeTrue();

    $errorMsg = $errors->first('form.email');
    // Accept either the translated string or the raw key (translation may not load in test env)
    expect(
        str_contains($errorMsg, 'Too many login attempts') || $errorMsg === 'auth.throttle'
    )->toBeTrue("Expected throttle error, got: {$errorMsg}");
});
