<div class="role-view">
    <x-title>
        Role: {{ $role->name }}

        <x-slot:buttons>
            @unless($role->is_system ?? false)
                <a href="{{ route('admin.roles.edit', $role) }}"
                   class="btn btn-primary waves-effect waves-light">
                    <i class="mdi mdi-pencil me-2"></i>
                    Edit Role
                </a>
            @endunless
        </x-slot:buttons>
    </x-title>

    <div class="row">
        {{-- MAIN --}}
        <div class="col-12 col-md-9">

            {{-- ROLE SUMMARY --}}
            <div class="card mb-3">
                <div class="card-body">

                    <div class="d-flex justify-content-between align-items-start">
                        <div>
                            <h4 class="mb-1">{{ $role->name }}</h4>

                            @if($role->is_system ?? false)
                                <span class="badge bg-soft-warning text-warning">
                                    System Role
                                </span>
                            @endif
                        </div>

                        <div class="text-muted small">
                            Created {{ $role->created_at?->format('d M Y') }}
                        </div>
                    </div>

                    <hr>

                    <div class="row text-center">
                        <div class="col-4">
                            <div class="fw-bold fs-4">
                                {{ $users->count() }}
                            </div>
                            <div class="text-muted small">
                                Users Assigned
                            </div>
                        </div>

                        <div class="col-4">
                            <div class="fw-bold fs-4">
                                {{ $role->permissions->count() }}
                            </div>
                            <div class="text-muted small">
                                Permissions
                            </div>
                        </div>

                        <div class="col-4">
                            <div class="fw-bold fs-4">
                                {{ $permissionGroups->count() }}
                            </div>
                            <div class="text-muted small">
                                Permission Groups
                            </div>
                        </div>
                    </div>

                </div>
            </div>

            {{-- PERMISSIONS --}}
            <div class="card mb-3">
                <div class="card-body">
                    <h4 class="card-title mb-3">
                        Permissions
                    </h4>

                    @foreach($permissionGroups as $group => $permissions)
                        <div class="mb-4">
                            <h5 class="mb-2">{{ $group }}</h5>

                            <div class="table-responsive">
                                <table class="table table-sm table-striped align-middle">
                                    <thead class="table-light">
                                    <tr>
                                        <th>Permission</th>
                                        <th>Description</th>
                                        <th>Used by Roles</th>
                                    </tr>
                                    </thead>

                                    <tbody>
                                    @foreach($permissions as $permission)
                                        <tr>
                                            <td>
                                                <strong>{{ $permission->label }}</strong>
                                                <div class="text-muted small">
                                                    {{ $permission->name }}
                                                </div>
                                            </td>

                                            <td class="text-muted">
                                                {{ $permission->description ?? '—' }}
                                            </td>

                                            <td>
                                                <span class="badge bg-soft-info text-info">
                                                    {{ $permission->roles_count }}
                                                </span>
                                            </td>
                                        </tr>
                                    @endforeach
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    @endforeach
                </div>
            </div>

            {{-- USERS --}}
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title mb-3">
                        Users with this Role
                    </h4>

                    @if($users->count())
                        <div class="table-responsive">
                            <table class="table table-sm table-striped align-middle">
                                <thead class="table-light">
                                <tr>
                                    <th>Name</th>
                                    <th>Username</th>
                                    <th>Email</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($users as $user)
                                    <tr>
                                        <td>
                                            <strong>
                                                {{ $user->name_first }}
                                                {{ $user->name_last }}
                                            </strong>
                                        </td>
                                        <td class="text-muted">
                                            {{ $user->username }}
                                        </td>
                                        <td class="text-muted">
                                            {{ $user->email_address }}
                                        </td>

                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                        </div>
                    @else
                        <div class="text-muted">
                            No users currently assigned to this role.
                        </div>
                    @endif
                </div>
            </div>
        </div>

        {{-- SIDEBAR --}}
        <div class="col-12 col-md-3">
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title">
                        Role Insights
                    </h4>

                    <hr>

                    <p class="text-muted small">
                        This page shows everything this role grants access to,
                        who currently has it, and how widely each permission is used.
                    </p>

                    @if($role->is_system ?? false)
                        <div class="alert alert-warning small mb-0">
                            This is a system role. Editing is restricted to
                            prevent accidental privilege escalation.
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
