<?php

namespace Tests\Feature;

use App\Jobs\AnonymizeAccount;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Str;
use Tests\TestCase;

class ProfileAccountDeletionTest extends TestCase
{
    use RefreshDatabase;

    public function test_confirmation_is_required(): void
    {
        Queue::fake();

        $tenant = Tenant::factory()->create();
        $user = User::factory()->create(['tenant_id' => $tenant->id]);

        $response = $this->actingAs($user)->delete('/profile/account', [
            'confirmation' => 'WRONG',
        ]);

        $response->assertSessionHasErrors('confirmation');
        Queue::assertNothingPushed();
    }

    public function test_account_deletion_dispatches_job(): void
    {
        Queue::fake();

        $tenant = Tenant::factory()->create();
        $user = User::factory()->create(['tenant_id' => $tenant->id]);

        $keyword = Str::upper(__('profile.delete.confirmation_keyword'));

        $response = $this->actingAs($user)->delete('/profile/account', [
            'confirmation' => $keyword,
        ]);

        $response->assertRedirect('/profile');

        Queue::assertPushed(AnonymizeAccount::class, function (AnonymizeAccount $job) use ($user) {
            return $job->userId() === $user->id;
        });
    }
}
