<?php

namespace Tests\Feature\User;

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

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

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

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

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

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

        $response->assertForbidden();

    }
    public function test_user_with_permission_can_show_users(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage_users',
                'name_fa' => 'مدیریت کاربران'
            ]))
            ->create();
        $role = Role::factory()
            ->has(Permission::factory([
                'name' => 'test'
            ]))->create();

        $user->assignRole($role->id);

        $response = $this->actingAs($user)->getJson(route('users.show',[$user->id]));
        $response->assertJsonStructure([
            'data' => [
                'username',
                'full_name',
                'id',
                'gender',
                'email',
                'phone_number',
                'avatar',
                'national_id',
                'position',
                'province_id',
                'province_name',
                'role',
                'telephone_id'
            ]
        ]);
    }
}
