<?php

namespace Tests\Feature\Role;

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
    {
        $role = Role::factory()->create();
        $response = $this->postJson(route('roles.show',[$role->id]));

        $response->assertUnauthorized();
    }

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

        $role = Role::factory()->create();


        $permission = Permission::factory()->create([
            'id' => 2,
            'name' => $this->faker->name,
            'name_fa' => $this->faker->name
        ]);

        $role->syncPermissions($permission);

        $role = Role::factory()->create();
        $response =  $this->actingAs($user)->getJson(route('roles.show', [$role->id]));

        $response->assertOk()
            ->assertJson([
                'data' => [
                    'id' => $role->id,
                    'name' => $role->name,
                    'name_fa' => $role->name_fa,
                    'permissions' => $role->permissions->pluck('name')->toArray(),
                ]
            ]);
    }
}
