<?php

namespace App\Components\Admin\Role;

use App\Models\User\Permission;
use App\Models\User\Role;
use Livewire\Component;

class RoleShow extends Component {
    public Role $role;

    public function mount(Role $role) {
        $this->authorize('view', $role);
    }

    public function render() {
        $permissions = Permission::query()
            ->whereIn('id', $this->role->permissions->pluck('id'))
            ->withCount('roles')
            ->orderBy('group')
            ->orderBy('label')
            ->get()
            ->groupBy('group');

        $users = $this->role
            ->users()
            ->select('id', 'name_first', 'name_last', 'email_address', 'username')
            ->orderBy('name_first')
            ->get();

        return view('components.admin.role.role-show', [
            'permissionGroups' => $permissions,
            'users' => $users,
        ]);
    }
}
