<?php

namespace Tests\Feature;

use App\Models\User;
use App\Notifications\ResetPasswordNotification;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;

class ForgotPasswordTest extends TestCase
{
    use RefreshDatabase;

    public function test_forgot_password_sends_notification_for_existing_email(): void
    {
        Notification::fake();

        $user = User::factory()->create(['email' => 'ada@gembira.id']);

        $response = $this->postJson('/api/auth/forgot-password', [
            'email' => 'ada@gembira.id',
        ]);

        $response->assertStatus(200)
            ->assertJson(['message' => 'Jika email terdaftar di sistem kami, link reset password telah dikirim.']);

        Notification::assertSentTo($user, ResetPasswordNotification::class);
    }

    public function test_forgot_password_returns_same_generic_message_for_unknown_email(): void
    {
        Notification::fake();

        $response = $this->postJson('/api/auth/forgot-password', [
            'email' => 'tidak-ada@gembira.id',
        ]);

        $response->assertStatus(200)
            ->assertJson(['message' => 'Jika email terdaftar di sistem kami, link reset password telah dikirim.']);

        Notification::assertNothingSent();
    }

    public function test_forgot_password_requires_valid_email_format(): void
    {
        $response = $this->postJson('/api/auth/forgot-password', [
            'email' => 'bukan-email',
        ]);

        $response->assertStatus(422)
            ->assertJsonValidationErrors('email');
    }
}
