<?php

namespace Tests\Feature\User;

use App\Models\Permission;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class DeleteTest extends TestCase
{
   use RefreshDatabase, WithFaker;
    public function test_unauthenticated_user_cannot_access(): void
    {
        $user = User::factory()->create();
        $response = $this->getJson(route('users.destroy',[$user->id]));

        $response->assertUnauthorized();
    }
    public function test_user_without_permission_cannot_destroy_users(): void
    {
        $user = User::factory()
            ->create();

        $response = $this->actingAs($user)->getJson(route('users.destroy',[$user->id]));
        $response->assertForbidden();
    }
    public function test_superAdmin_cant_updated()
    {

        $user = User::factory()->create([
            'username' => "witel",
        ]);

        $response = $this->actingAs($user)->getJson(route('users.destroy',[$user->id]));

        $response->assertForbidden();

    }
    public function test_can_delete_a_user()
    {
        $user = User::factory()
            ->has(permission::factory([
                'name'=> 'manage_users',
                "name_fa" => 'مدیریت کاربران',
            ]))
            ->create();

        $response = $this->actingAs($user)->deleteJson(route('users.destroy', [$user->id]));

        $response->assertStatus(200)
            ->assertExactJson([
                'message' => __('messages.successful')
            ]);

        $this->assertDatabaseMissing('users', [
            'username' => $user->username
        ]);
    }
}
