<?php

namespace Tests\Feature\Auth;

use App\Domains\Auth\Models\ResetPasswordAttempt;
use App\Domains\Auth\Models\User;
use App\Domains\Authorization\Enums\RoleCode;
use App\Domains\Authorization\Models\Role;
use App\Domains\Notification\Events\PasswordResetRequested;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;

class ScannerResetPasswordControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_it_creates_an_attempt_for_a_tenant_bound_scanner_user(): void
    {
        Event::fake([PasswordResetRequested::class]);
        Role::query()->create([
            'codigo' => RoleCode::Scanner->value,
            'nombre' => 'Scanner',
        ]);
        $tenant = Tenant::query()->create([
            'codigo' => 'acme',
            'nombre' => 'Acme',
            'dominio' => 'store.acme.test',
        ]);
        $user = User::factory()->create([
            'email' => 'scanner@example.com',
            'rol_codigo' => RoleCode::Scanner->value,
            'tenant_codigo' => $tenant->codigo,
        ]);

        $this->postJson('/api/v1/scanner/password/reset-attempts', [
            'email' => ' SCANNER@EXAMPLE.COM ',
        ])->assertAccepted()->assertJsonPath('status', ResetPasswordAttempt::STATUS_PENDING);

        $attempt = $user->resetPasswordAttempts()->sole();
        Event::assertDispatched(
            PasswordResetRequested::class,
            fn (PasswordResetRequested $event): bool => $event->attemptId === $attempt->id
                && $event->tenantCode === $tenant->codigo
                && $event->channel === PasswordResetRequested::CHANNEL_SCANNER,
        );
    }

    public function test_it_does_not_create_an_attempt_for_a_non_scanner_user(): void
    {
        Event::fake([PasswordResetRequested::class]);

        $this->postJson('/api/v1/scanner/password/reset-attempts', [
            'email' => User::factory()->create()->email,
        ])->assertAccepted()->assertJsonPath('status', ResetPasswordAttempt::STATUS_PENDING);

        $this->assertDatabaseCount('reset_password_attempts', 0);
        Event::assertNotDispatched(PasswordResetRequested::class);
    }

    public function test_scanner_can_complete_the_password_reset_flow(): void
    {
        Event::fake([PasswordResetRequested::class]);
        Role::query()->create([
            'codigo' => RoleCode::Scanner->value,
            'nombre' => 'Scanner',
        ]);
        $tenant = Tenant::query()->create([
            'codigo' => 'acme',
            'nombre' => 'Acme',
            'dominio' => 'store.acme.test',
        ]);
        $user = User::factory()->create([
            'email' => 'scanner@example.com',
            'rol_codigo' => RoleCode::Scanner->value,
            'tenant_codigo' => $tenant->codigo,
        ]);

        $this->postJson('/api/v1/scanner/password/reset-attempts', [
            'email' => $user->email,
        ])->assertAccepted();

        $attempt = $user->resetPasswordAttempts()->sole();
        $this->postJson('/api/v1/scanner/password/reset-attempts/validate', [
            'email' => $user->email,
            'codigo' => $attempt->codigo,
        ])->assertOk()->assertJsonPath('status', ResetPasswordAttempt::STATUS_VALIDATED);

        $this->postJson('/api/v1/scanner/password/reset', [
            'email' => $user->email,
            'codigo' => $attempt->codigo,
            'password' => 'NewScanner!123',
            'password_confirmation' => 'NewScanner!123',
        ])->assertOk()->assertJsonPath('status', ResetPasswordAttempt::STATUS_USED);

        $this->assertTrue(Hash::check('NewScanner!123', $user->fresh()->password));
    }
}
