<?php

namespace Tests\Feature\Auth;

use App\Domains\Auth\Models\ResetPasswordAttempt;
use App\Domains\Auth\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;

class ResetPasswordAttemptTest extends TestCase
{
    use RefreshDatabase;

    public function test_table_has_the_expected_columns(): void
    {
        $this->assertEqualsCanonicalizing([
            'id',
            'user_id',
            'codigo',
            'status',
        ], Schema::getColumnListing('reset_password_attempts'));
    }

    public function test_attempt_belongs_to_a_user_and_defaults_to_pending(): void
    {
        $user = User::factory()->create();

        $attempt = $user->resetPasswordAttempts()->create([
            'codigo' => '123456',
        ]);

        $this->assertSame(ResetPasswordAttempt::STATUS_PENDING, $attempt->status);
        $this->assertTrue($attempt->user->is($user));
        $this->assertTrue($user->resetPasswordAttempts->contains($attempt));
        $this->assertFalse($attempt->usesTimestamps());
        $this->assertArrayNotHasKey('codigo', $attempt->toArray());
    }

    public function test_attempts_are_deleted_with_their_user(): void
    {
        $user = User::factory()->create();
        $attempt = ResetPasswordAttempt::query()->create([
            'user_id' => $user->id,
            'codigo' => '123456',
        ]);

        $user->delete();

        $this->assertDatabaseMissing('reset_password_attempts', [
            'id' => $attempt->id,
        ]);
    }
}
