# DEN MOBILE - Deployment & Setup Guide

**Digital Engagement Network Mobile**  
*Smart Auto Dialer & Sales Execution Platform*

## Prerequisites

- PHP 7.4 or higher
- MySQL 5.7 or higher
- cPanel access (File Manager, phpMyAdmin)
- Composer (or manual file upload)
- SSL Certificate (HTTPS required)

---

## Part 1: Laravel Backend Setup (cPanel)

### Step 1: Create Database

1. Log into cPanel → phpMyAdmin
2. Create new database: `smartcall_db`
3. Create user: `smartcall_user` with password
4. Grant all privileges to `smartcall_user` on `smartcall_db`

### Step 2: Upload Laravel Files

1. Download Laravel project files
2. Via cPanel File Manager, upload to `public_html/autodial` (or your domain root)
3. Structure:
```
public_html/
├── autodial/
│   ├── app/
│   ├── database/
│   ├── routes/
│   ├── config/
│   ├── public/
│   │   ├── css/
│   │   └── js/
│   ├── storage/
│   ├── .env
│   ├── composer.json
│   └── artisan
```

### Step 3: Configure .env File

Create/edit `.env` in project root:

```env
APP_NAME="Smart Call CRM"
APP_ENV=production
APP_KEY=base64:YOUR_KEY_HERE
APP_DEBUG=false
APP_URL=https://yourdomain.com/autodial

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=smartcall_db
DB_USERNAME=smartcall_user
DB_PASSWORD=YourPassword123

MAIL_MAILER=sendmail
MAIL_FROM_ADDRESS=noreply@yourdomain.com

SESSION_DRIVER=file
CACHE_DRIVER=file
QUEUE_CONNECTION=sync

LOG_CHANNEL=stack
```

Generate APP_KEY:
```bash
php artisan key:generate
```

### Step 4: Run Migrations

Via cPanel Terminal or SSH (if available):

```bash
cd /home/user/public_html/autodial

# Install dependencies
composer install --no-dev --optimize-autoloader

# Run migrations (creates tables)
php artisan migrate:fresh --seed

# Cache config
php artisan config:cache
php artisan route:cache
```

### Step 5: Set Permissions

```bash
chmod -R 755 storage/
chmod -R 755 bootstrap/cache/
chmod -R 755 public/
```

### Step 6: Create PHP.ini Overrides (if needed)

Create `.htaccess` in `public/`:

```apache
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

# Add PHP settings
php_flag display_errors Off
php_value upload_max_filesize 50M
php_value post_max_size 50M
php_value max_execution_time 300
```

---

## Part 2: Android App Build & Deploy

### Step 1: Build APK

Use Android Studio:

1. Open project in Android Studio
2. Go to Build → Build Bundle(s) / APK(s) → Build APK(s)
3. Wait for build to complete
4. APK location: `app/release/app-release.apk`

### Step 2: Update Server URL in Code

In `RetrofitClient.kt`:

```kotlin
private const val BASE_URL = "https://yourdomain.com/autodial/"
```

Rebuild APK after updating URL.

### Step 3: Distribute APK

- Upload to cloud storage (Google Drive, Dropbox)
- Or use Google Play Store
- Or internal MDM solution

### Step 4: Device Setup

1. Install APK on device
2. Open Smart Call app
3. Enter device name and register
4. Enable auto-call toggle
5. Verify in dashboard

---

## Part 3: Dashboard Access

### Login Credentials

Create admin user via command:

```bash
php artisan tinker

>>> DB::table('users')->insert(['name' => 'Admin', 'email' => 'admin@example.com', 'password' => bcrypt('Password123')]);
```

Access dashboard at: `https://yourdomain.com/autodial/`

### Dashboard Features

1. **CRM List** - View all customers
2. **Call Logs** - View call history
3. **Analytics** - Call metrics, tag distribution
4. **Device Control** - Toggle auto-call, manage devices
5. **Manual Campaign** - Start calling campaign

---

## Part 4: System Monitoring

### Check System Health

```bash
php artisan tinker

// Get device count
>>> DB::table('devices')->count()

// Get pending tasks
>>> DB::table('call_queues')->where('status', 'pending')->count()

// Get today's calls
>>> DB::table('call_logs')->whereDate('created_at', today())->count()
```

### Clear Cache

```bash
php artisan cache:clear
php artisan route:cache
php artisan config:cache
```

### View Logs

Logs stored in `storage/logs/laravel.log`

Use cPanel File Manager to view or download.

---

## Part 5: Security Setup

### HTTPS Requirement

- Ensure SSL certificate is installed
- Force HTTPS in `.htaccess`:

```apache
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
```

### API Rate Limiting

Configured in code:
- 10 requests per device per 10 seconds
- Prevents abuse

### Database Backup

Via cPanel:
1. Home → Backup → Download a Full Website Backup
2. Or use phpMyAdmin → Export

Automate with cron (if available):
```bash
0 2 * * * mysqldump -u smartcall_user -p[PASSWORD] smartcall_db > /home/user/backups/db_$(date +\%Y\%m\%d).sql
```

---

## Part 6: Troubleshooting

### Issue: Devices not receiving tasks

**Solution:** Check in dashboard:
1. Verify device `auto_call = true`
2. Check `last_heartbeat` is recent
3. View pending tasks count
4. Check API logs in `storage/logs/`

### Issue: App crashes on startup

**Solution:**
1. Check device token is saved
2. Verify server URL in APK
3. Check internet connectivity
4. View Android Logcat for errors

### Issue: WhatsApp not opening

**Solution:**
1. Ensure WhatsApp is installed
2. Check phone number format (+country code)
3. Test URL: `https://wa.me/+1234567890?text=hello`

### Issue: Slow task assignment

**Solution:**
1. Check database indexes
2. Monitor server load
3. Clear cache: `php artisan cache:clear`
4. Optimize queries in logs

---

## Performance Tips

1. **Index Optimization:** Ensure all indexes are created (migrations handle this)
2. **Connection Pooling:** Use persistent MySQL connections
3. **Caching:** Enable Redis caching if available
4. **Polling Interval:** Increase to 5-10s if server load is high
5. **Log Rotation:** Prevent log files from growing too large

---

## File Structure Summary

```
autodial/
├── app/
│   ├── Models/
│   │   ├── Customer.php
│   │   ├── Device.php
│   │   ├── CallQueue.php
│   │   ├── CallLog.php
│   │   ├── Tag.php
│   │   ├── WhatsAppMessage.php
│   │   └── Campaign.php
│   ├── Http/
│   │   └── Controllers/
│   │       ├── API/ (TaskController, TagController, WhatsAppController, DeviceController)
│   │       └── Web/ (DashboardController, CRMController, AnalyticsController, DeviceManagementController)
│   └── Services/ (TaskAssignmentService, WhatsAppService)
├── database/
│   └── migrations/ (all table creation)
├── routes/
│   ├── api.php (API endpoints)
│   └── web.php (web dashboard routes)
├── resources/
│   └── views/ (Blade templates)
├── config/
│   └── app.php, database.php, etc
├── public/
│   ├── index.php
│   ├── css/
│   └── js/
├── storage/
│   ├── app/
│   ├── logs/
│   └── cache/
├── .env (configuration)
└── composer.json
```

---

## Next Steps

1. Deploy backend to cPanel
2. Build and distribute Android APK
3. Test polling system with 1 device
4. Scale to multiple devices
5. Monitor and optimize
6. Regular backups and updates

