<?php

namespace Tests\Feature\Console;

use App\Events\DeleteUnverifiedUser;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Tests\TestCase;

class DeleteUnverifiedUsersCommandTest extends TestCase
{
    use RefreshDatabase;

    public function test_command_dispatches_event_for_expired_user(): void
    {
        DB::table('users')->insert([
            'id' => 20,
            'firstname' => 'Old',
            'lastname' => 'User',
            'email' => 'old@example.com',
            'status' => 'Inactive',
            'is_verified' => 0,
            'created_at' => now()->subDays(2),
            'updated_at' => now()->subDays(2),
        ]);

        DB::table('users')->insert([
            'id' => 21,
            'firstname' => 'Recent',
            'lastname' => 'User',
            'email' => 'recent@example.com',
            'status' => 'Inactive',
            'is_verified' => 0,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        Event::fake();

        $this->artisan('users:delete-unverified', ['--timeout' => 3600])->assertExitCode(0);

        Event::assertDispatched(DeleteUnverifiedUser::class, function ($event) {
            return $event->userId === 20;
        });

        Event::assertNotDispatched(DeleteUnverifiedUser::class, function ($event) {
            return $event->userId === 21;
        });
    }
}
