<?php

use App\Livewire\Auth\ForgotPassword;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Support\Facades\Notification;
use Livewire\Livewire;

beforeEach(function () {
    $this->setupUser([
        'email' => 'registered@example.com',
    ]);
});

it('renders the forgot password page correctly', function () {
    $this->get(route('password.request'))
        ->assertOk()
        ->assertSeeLivewire(ForgotPassword::class);
});

it('shows validation error when email is empty', function () {
    Livewire::test(ForgotPassword::class)
        ->set('form.email', '')
        ->call('sendLink')
        ->assertHasErrors(['form.email' => 'required']);
});

it('shows validation error for invalid email format', function () {
    Livewire::test(ForgotPassword::class)
        ->set('form.email', 'not-an-email')
        ->call('sendLink')
        ->assertHasErrors(['form.email' => 'email']);
});

it('can send reset link to a valid email', function () {
    Notification::fake();

    // Note: If the component has a bug in ['form.email' => ...], this might fail.
    // We expect it to succeed if the component works correctly.
    Livewire::test(ForgotPassword::class)
        ->set('form.email', 'registered@example.com')
        ->call('sendLink')
        ->assertHasNoErrors();

    Notification::assertSentTo(
        $this->user,
        ResetPassword::class
    );
});

it('shows failure behavior when email does not exist', function () {
    Notification::fake();

    Livewire::test(ForgotPassword::class)
        ->set('form.email', 'nonexistent@example.com')
        ->call('sendLink')
        ->assertHasNoErrors();

    Notification::assertNothingSent();
});
