# Chat and Notifications Feature

This document describes how to set up and use the chat and notifications features in the World Water Congress application.

## Features

1. Real-time chat between users
2. Event notifications system
3. Real-time updates via WebSockets
4. Scheduled notifications for upcoming events

## Setup Instructions

### 1. Install Dependencies

```bash
composer require pusher/pusher-php-server
npm install laravel-echo pusher-js
```

### 2. Environment Configuration

Add these variables to your `.env` file:

```env
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=your_app_id
PUSHER_APP_KEY=your_app_key
PUSHER_APP_SECRET=your_app_secret
PUSHER_APP_CLUSTER=mt1

# For Laravel WebSockets (local development)
LARAVEL_WEBSOCKETS_HOST=127.0.0.1
LARAVEL_WEBSOCKETS_PORT=6001
LARAVEL_WEBSOCKETS_SCHEME=http
```

### 3. Database Setup

```bash
php artisan migrate
php artisan db:seed --class=ChatAndNotificationsSeeder
```

### 4. WebSocket Server (Development)

For local development using Laravel WebSockets:

```bash
# Install Laravel WebSockets
composer require beyondcode/laravel-websockets

# Publish configuration
php artisan vendor:publish --provider="BeyondCode\LaravelWebSockets\WebSocketsServiceProvider" --tag="config"

# Start WebSocket server
php artisan websockets:serve
```

### 5. Queue Worker

Start the queue worker for processing notifications:

```bash
php artisan queue:work
```

### 6. Scheduler

For event notifications, run the Laravel scheduler:

```bash
php artisan schedule:work
```

## Frontend Integration

### 1. Echo Setup

In your `resources/js/bootstrap.js`:

```javascript
import Echo from 'laravel-echo';
import Pusher from 'pusher-js';

window.Pusher = Pusher;

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    wsHost: process.env.MIX_LARAVEL_WEBSOCKETS_HOST,
    wsPort: process.env.MIX_LARAVEL_WEBSOCKETS_PORT,
    wssPort: process.env.MIX_LARAVEL_WEBSOCKETS_PORT,
    forceTLS: false,
    disableStats: true,
});
```

### 2. Listen for Events

```javascript
// Listen for new messages
Echo.private(`conversation.${conversationId}`)
    .listen('MessageSent', (e) => {
        // Handle new message
    });

// Listen for notifications
Echo.private(`App.Models.User.${userId}`)
    .notification((notification) => {
        // Handle new notification
    });
```

## Testing

Run the feature tests:

```bash
php artisan test --filter=ChatAndNotificationsTest
```

## API Endpoints

### Chat

- `GET /api/chat/conversations` - List user's conversations
- `POST /api/chat/conversations` - Create new conversation
- `GET /api/chat/conversations/{id}` - Get conversation with messages
- `POST /api/chat/conversations/{id}/messages` - Send message
- `PATCH /api/chat/conversations/{id}/messages/{messageId}/read` - Mark message as read

### Notifications

- `GET /api/notifications` - Get user's notifications
- `PATCH /api/notifications/{id}/read` - Mark notification as read
- `POST /api/notifications/mark-all-read` - Mark all notifications as read

## Troubleshooting

1. Make sure the WebSocket server is running for real-time updates
2. Check that the queue worker is running for notification processing
3. Verify the scheduler is running for event notifications
4. Confirm your Pusher/WebSocket configuration in `.env`
5. Check the Laravel logs for any errors
