# Admin Groups Module - SJBHS Message Merge

Complete Laravel module for admin group chat and private messaging with real-time WebSocket support.

## Features

### Messaging
- **Private Chat**: One-on-one messaging between members and admin
- **Group Chat**: Group messaging with categories
- **Real-time Updates**: WebSocket support using Laravel Reverb
- **File Attachments**: Support for images and files
- **Read Receipts**: Track message read status
- **Unread Badges**: Real-time notification badges

### Admin Features
- Create and manage groups (public/private)
- Set member limits per group
- Approve/reject join requests
- Delete inappropriate messages
- Merged chatbox for all conversations

### Client Features
- Browse available groups
- Request to join groups
- Chat in groups and private conversations
- View group members
- Leave groups

## Requirements

- Laravel 10+
- PHP 8.1+
- Laravel Reverb (for WebSockets - optional)
- Redis (recommended for broadcasting)

## Installation

1. Copy the module to `Modules/ChatCentre` directory
2. Run migrations: `php artisan migrate`
3. Create storage link: `php artisan storage:link`

## WebSocket Setup (Laravel Reverb) - OPTIONAL

The module works with AJAX polling by default. For real-time updates, set up Laravel Reverb:

### Step 1: Install Laravel Reverb

```bash
composer require laravel/reverb
php artisan reverb:install
```

### Step 2: Configure Environment (.env)

```env
BROADCAST_DRIVER=reverb

REVERB_APP_ID=your-app-id
REVERB_APP_KEY=your-app-key
REVERB_APP_SECRET=your-app-secret
REVERB_HOST="localhost"
REVERB_PORT=8080
REVERB_SCHEME=http

VITE_REVERB_APP_KEY="${REVERB_APP_KEY}"
VITE_REVERB_HOST="${REVERB_HOST}"
VITE_REVERB_PORT="${REVERB_PORT}"
VITE_REVERB_SCHEME="${REVERB_SCHEME}"
```

### Step 3: Create Echo Configuration

Create `resources/js/echo.js`:

```javascript
import Echo from 'laravel-echo';
import Pusher from 'pusher-js';

window.Pusher = Pusher;

window.Echo = new Echo({
    broadcaster: 'reverb',
    key: import.meta.env.VITE_REVERB_APP_KEY,
    wsHost: import.meta.env.VITE_REVERB_HOST,
    wsPort: import.meta.env.VITE_REVERB_PORT,
    wssPort: import.meta.env.VITE_REVERB_PORT,
    forceTLS: (import.meta.env.VITE_REVERB_SCHEME ?? 'https') === 'https',
    enabledTransports: ['ws', 'wss'],
});
```

### Step 4: Install NPM Dependencies

```bash
npm install laravel-echo pusher-js
npm run build
```

### Step 5: Configure Broadcasting (config/broadcasting.php)

```php
'reverb' => [
    'driver' => 'reverb',
    'key' => env('REVERB_APP_KEY'),
    'secret' => env('REVERB_APP_SECRET'),
    'app_id' => env('REVERB_APP_ID'),
    'options' => [
        'host' => env('REVERB_HOST'),
        'port' => env('REVERB_PORT', 443),
        'scheme' => env('REVERB_SCHEME', 'https'),
        'useTLS' => env('REVERB_SCHEME', 'https') === 'https',
    ],
    'client_options' => [],
],
```

### Step 6: Run the WebSocket Server

```bash
php artisan reverb:start
```

For production, use Supervisor:

```ini
[program:reverb]
command=php /var/www/html/artisan reverb:start
autostart=true
autorestart=true
user=www-data
redirect_stderr=true
stdout_logfile=/var/log/reverb.log
```

### Step 7: Run Queue Worker (for broadcasting)

```bash
php artisan queue:work
```

## Without WebSockets

If you don't want to use WebSockets, the module will automatically fall back to AJAX polling (every 30 seconds). No additional setup required.

## Folder Structure

```
ChatCentre/
├── Config/
├── Database/Migrations/
├── Events/                    # Broadcasting events
│   ├── NewGroupMessage.php
│   ├── NewPrivateMessage.php
│   └── GroupMessageUpdated.php
├── Http/Controllers/
│   ├── Admin/
│   └── Client/
├── Models/
├── Providers/
├── Resources/views/
│   ├── admin/
│   └── client/
└── Routes/
    ├── web.php
    ├── client.php
    └── channels.php           # Broadcast channel authorization
```

## URLs

### Client Routes
| URL | Description |
|-----|-------------|
| `/client/groups/chatbox` | Merged chatbox (Private + Groups) |
| `/client/groups` | Browse all groups |

### Admin Routes
| URL | Description |
|-----|-------------|
| `/admin/chat-centre/merged-chatbox` | Merged chatbox |
| `/admin/chat-centre/groups` | Manage groups |
| `/admin/chat-centre/requests` | Manage join requests |

## Version History

- **2.0.0** - Added WebSocket support with Laravel Reverb
- **1.0.0** - Initial release with polling

## License

Proprietary - EchoPx Technologies
