@extends('admin.layouts.app')

@section('title', 'Roles')
@section('subtitle', 'Create reusable roles, build custom access levels, and assign them to users from the team access screen.')

@section('content')
    <div class="admin-grid">
        <div class="admin-card">
            <div class="admin-toolbar">
                <div>
                    <p class="admin-section-title">Role manager</p>
                    <p class="admin-section-subtitle">{{ $roles->count() }} roles available for assignment.</p>
                </div>
                <div class="flex flex-wrap gap-2">
                    <a href="{{ route('admin.users.index') }}" class="admin-button admin-button--secondary">View users</a>
                    <a href="{{ route('admin.roles.create') }}" class="admin-button">New role</a>
                </div>
            </div>

            <div class="admin-response-table">
                <table>
                    <thead>
                        <tr>
                            <th>Role</th>
                            <th>Slug</th>
                            <th>Access level</th>
                            <th>Permissions</th>
                            <th>Assigned users</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($roles as $role)
                            <tr>
                                <td>
                                    <div class="admin-table-person">
                                        <strong>{{ $role->name }}</strong>
                                        <span>{{ $role->description ?: 'No description added yet.' }}</span>
                                    </div>
                                </td>
                                <td><span class="admin-badge admin-badge--read">{{ $role->slug }}</span></td>
                                <td><span class="admin-badge admin-badge--preferred">{{ $role->access_level }}</span></td>
                                <td>
                                    <div class="admin-table-person">
                                        <strong>{{ $role->permissionCount() }} enabled</strong>
                                        <span>{{ collect($role->grantedSections())->take(3)->implode(', ') ?: 'No sections enabled yet.' }}</span>
                                    </div>
                                </td>
                                <td>{{ $role->users_count }}</td>
                                <td>
                                    <div class="flex flex-wrap justify-end gap-2">
                                        <a href="{{ route('admin.roles.edit', $role) }}" class="admin-button admin-button--ghost">Edit</a>
                                        @if(! $role->is_system)
                                            <form method="POST" action="{{ route('admin.roles.destroy', $role) }}" onsubmit="return confirm('Delete this role?');">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit" class="admin-button admin-button--danger">Delete</button>
                                            </form>
                                        @endif
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="admin-empty">No roles found.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection
