# Deployment Guide — WhatsApp CRM

## 1. Production .env Checklist

Before deploying, verify these items in your `.env` file:

- [ ] `WHATSAPP_APP_SECRET` — Set to real value from Meta Developer Portal
- [ ] `WHATSAPP_VERIFY_SIGNATURE=true` — Enable after secret is configured
- [ ] `APP_URL` — Set to real production domain (e.g. https://crm.yourdomain.com)

---

## 2. Queue Worker (Required)

WhatsApp webhook processing and message status updates are handled by Laravel's database queue. **If the worker stops, incoming WhatsApp webhooks will not be processed.**

### Manual Start

```bash
php artisan queue:work --queue=webhooks,default
```

> The `--queue=webhooks,default` flag ensures webhook jobs are processed first, followed by default queue jobs. Always use this exact flag.

### Supervisor Configuration (Linux)

Install Supervisor:

```bash
sudo apt install supervisor
```

Create config file `/etc/supervisor/conf.d/whatsapp-crm-worker.conf`:

```ini
[program:whatsapp-crm-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/your/project/artisan queue:work --queue=webhooks,default --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/path/to/your/project/storage/logs/worker.log
stdout_logfile_maxbytes=10MB
```

Then reload:

```bash
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start whatsapp-crm-worker:*
```

### Task Scheduler Configuration (Windows)

1. Open **Task Scheduler**
2. Click **Create Task**
3. **General tab**: Name `WhatsApp CRM Queue Worker`, select "Run whether user is logged on or not"
4. **Triggers tab**: New → Begin the task "At startup" (or your preferred schedule)
5. **Actions tab**: New → Start a program:
   - Program/script: `C:\php82\php.exe`
   - Arguments: `-d:\path\to\project\artisan queue:work --queue=webhooks,default --sleep=3 --tries=3 --max-time=3600`
6. **Settings tab**: Check "Allow task to be run on demand", set "Stop if running longer than 1 hour"

---

## 3. WebSocket Server (Recommended)

Laravel Reverb provides real-time message delivery. Without it, the dashboard falls back to 15-second polling.

### Start Command

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

### Supervisor Configuration

Add to the same Supervisor config file or create a separate one:

```ini
[program:whatsapp-crm-reverb]
command=php /path/to/your/project/artisan reverb:start
autostart=true
autorestart=true
user=www-data
redirect_stderr=true
stdout_logfile=/path/to/your/project/storage/logs/reverb.log
stdout_logfile_maxbytes=10MB
```

Then reload Supervisor:

```bash
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start whatsapp-crm-reverb
```

---

## 4. SQLite Backup Strategy

Since the system uses SQLite (`database/database.sqlite`), a regular backup is essential.

### Linux: Daily Backup Script

Create `/usr/local/bin/backup-whatsapp-crm.sh`:

```bash
#!/bin/bash
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR=/path/to/backups/whatsapp-crm
DB_FILE=/path/to/your/project/database/database.sqlite

mkdir -p "$BACKUP_DIR"
cp "$DB_FILE" "$BACKUP_DIR/database_$TIMESTAMP.sqlite"

# Keep only last 30 backups
find "$BACKUP_DIR" -name "database_*.sqlite" -mtime +30 -delete
```

Make it executable and add to crontab:

```bash
chmod +x /usr/local/bin/backup-whatsapp-crm.sh
crontab -e
```

Add this line (runs daily at 3 AM):

```cron
0 3 * * * /usr/local/bin/backup-whatsapp-crm.sh
```

### Windows: Daily Backup via Task Scheduler

Create a backup batch file:

```batch
@echo off
set TIMESTAMP=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
set BACKUP_DIR=C:\path\to\backups
set DB_FILE=C:\path\to\project\database\database.sqlite

if not exist "%BACKUP_DIR%" mkdir "%BACKUP_DIR%"
copy "%DB_FILE%" "%BACKUP_DIR%\database_%TIMESTAMP%.sqlite"

REM Keep only last 30 days
forfiles -p "%BACKUP_DIR%" -m *.sqlite -d -30 -c "cmd /c del @path"
```

Then create a Task Scheduler task to run this batch file daily.

---

## 5. SSL Certificate

The WhatsApp webhook URL must use HTTPS. For production, use Let's Encrypt with certbot.

### nginx

```bash
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com
```

### Auto-renewal

Certbot configures automatic renewal by default. Test with:

```bash
sudo certbot renew --dry-run
```
