<?php

namespace App\Components\Admin\Role;

use App\Models\User\Permission;
use App\Models\User\Role;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Component;

class RoleShow extends Component {
    use AuthorizesRequests;
    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'))
            ->with(['roles' => fn($q) => $q->orderBy('name')])
            ->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,
        ]);
    }
}
