# 📱 FEATURES 2-5: IMPLEMENTASI LENGKAP

## 📋 Feature List:
1. ✅ **Feature #1: Screen Mirror** - DONE
2. ⏳ **Feature #2: Bandwidth Limiting** - Backend Ready
3. ⏳ **Feature #3: Shutdown Command** - Backend Ready
4. ✅ **Feature #4: Running Applications** - DONE
5. ✅ **Feature #5: Media Social Detection** - DONE

---

## 🔧 FEATURE #2: PEMBATASAN BANDWIDTH

### Database
```sql
CREATE TABLE bandwidth_limits (
    id INT,
    agent_id VARCHAR(50),
    limit_kbps INT,           -- Kecepatan limit dalam Kbps
    status ENUM('active', 'inactive', 'failed'),
    issued_by VARCHAR(100),
    issued_at TIMESTAMP,
    activated_at TIMESTAMP
);
```

### API Endpoints

#### Limit Bandwidth
```
POST /api/command
Body: {
    "agent_id": "117295468",
    "command_type": "throttle_bandwidth",
    "limit_kbps": 1024,        // Limit ke 1 Mbps
    "issued_by": "admin"
}

Response:
{
    "success": true,
    "message": "Bandwidth throttle command sent",
    "command_id": 123
}
```

#### Get Bandwidth Status
```
GET /api/bandwidth?agent_id=117295468

Response:
{
    "success": true,
    "data": {
        "limit_kbps": 1024,
        "status": "active",
        "issued_at": "2026-03-28 15:00:00"
    }
}
```

### Dashboard Integration
```php
<!-- Button untuk limit bandwidth -->
<button onclick="limitBandwidth(117295468, 512)">
    🚫 Limit ke 512 Kbps
</button>

<script>
function limitBandwidth(agent_id, kbps) {
    fetch('/api/command', {
        method: 'POST',
        body: JSON.stringify({
            agent_id: agent_id,
            command_type: 'throttle_bandwidth',
            limit_kbps: kbps,
            issued_by: 'admin'
        })
    }).then(r => r.json()).then(d => alert('Bandwidth limited to ' + kbps + ' Kbps'));
}
</script>
```

---

## 🔌 FEATURE #3: SHUTDOWN COMMAND

### Database
```sql
CREATE TABLE shutdown_logs (
    id INT,
    agent_id VARCHAR(50),
    shutdown_type ENUM('immediate', 'scheduled', 'restart'),
    delay_seconds INT DEFAULT 0,
    issued_by VARCHAR(100),
    issued_at TIMESTAMP,
    executed_at TIMESTAMP,
    status ENUM('pending', 'executing', 'completed', 'failed')
);
```

### API Endpoints

#### Shutdown PC (Immediate)
```
POST /api/command
Body: {
    "agent_id": "117295468",
    "command_type": "shutdown",
    "delay_seconds": 0,
    "issued_by": "admin"
}

Response:
{
    "success": true,
    "message": "Shutdown command sent",
    "command_id": 124,
    "execution_in": 0
}
```

#### Shutdown dengan Delay (e.g., 10 menit)
```
POST /api/command
Body: {
    "agent_id": "117295468",
    "command_type": "shutdown",
    "delay_seconds": 600,
    "issued_by": "admin"
}
```

#### Restart PC
```
POST /api/command
Body: {
    "agent_id": "117295468",
    "command_type": "restart",
    "delay_seconds": 0,
    "issued_by": "admin"
}
```

### Dashboard Integration
```php
<button onclick="shutdownPC(117295468)">🔴 Shutdown Sekarang</button>
<button onclick="shutdownPC(117295468, 300)">⏱️ Shutdown dalam 5 menit</button>
<button onclick="restartPC(117295468)">🔄 Restart Sekarang</button>

<script>
function shutdownPC(agent_id, delay = 0) {
    if (!confirm('Shutdown PC ini?')) return;
    
    fetch('/api/command', {
        method: 'POST',
        body: JSON.stringify({
            agent_id: agent_id,
            command_type: 'shutdown',
            delay_seconds: delay,
            issued_by: 'admin'
        })
    }).then(r => r.json()).then(d => {
        if (d.success) alert('Shutdown command sent!');
    });
}

function restartPC(agent_id, delay = 0) {
    if (!confirm('Restart PC ini?')) return;
    
    fetch('/api/command', {
        method: 'POST',
        body: JSON.stringify({
            agent_id: agent_id,
            command_type: 'restart',
            delay_seconds: delay,
            issued_by: 'admin'
        })
    }).then(r => r.json()).then(d => {
        if (d.success) alert('Restart command sent!');
    });
}
</script>
```

---

## 📦 FEATURE #4: RUNNING APPLICATIONS

### Database
```sql
CREATE TABLE running_applications (
    id INT,
    agent_id VARCHAR(50),
    process_id INT,
    process_name VARCHAR(255),     -- chrome.exe, notepad.exe, etc
    window_title VARCHAR(500),     -- Browser tab title, window title
    executable_path VARCHAR(500),  -- Full path to executable
    cpu_usage DECIMAL(5, 2),       -- Percentage
    memory_usage INT,              -- In MB
    status VARCHAR(50),
    snapshot_at TIMESTAMP
);
```

### API Endpoints

#### Save Running Applications (dari Agent)
```
POST /api/application
Body: {
    "agent_id": "117295468",
    "applications": [
        {
            "process_id": 1234,
            "process_name": "chrome.exe",
            "window_title": "Google - Chrome",
            "executable_path": "C:\\Program Files\\Google\\Chrome\\chrome.exe",
            "cpu_usage": 5.2,
            "memory_usage": 512
        },
        {
            "process_id": 5678,
            "process_name": "notepad.exe",
            "window_title": "Untitled - Notepad",
            "executable_path": "C:\\Windows\\System32\\notepad.exe",
            "cpu_usage": 0.1,
            "memory_usage": 32
        }
    ]
}

Response:
{
    "success": true,
    "message": "Applications updated",
    "count": 2
}
```

#### Get Running Applications
```
GET /api/application?agent_id=117295468

Response:
{
    "success": true,
    "data": [
        {
            "process_name": "chrome.exe",
            "window_title": "Google - Chrome",
            "cpu_usage": 5.2,
            "memory_usage": 512
        },
        {
            "process_name": "notepad.exe",
            "window_title": "Untitled - Notepad",
            "cpu_usage": 0.1,
            "memory_usage": 32
        }
    ],
    "count": 2
}
```

#### Get Top Applications (by resource usage)
```
GET /api/application?agent_id=117295468&type=top&limit=5

Response: Top 5 aplikasi dengan resource usage tertinggi
```

### Dashboard View
```php
<!-- di dashboard/views/applications.php -->
<h2>📦 Running Applications</h2>

<table class="table">
    <thead>
        <tr>
            <th>Process</th>
            <th>Window Title</th>
            <th>CPU %</th>
            <th>Memory (MB)</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($applications as $app): ?>
        <tr>
            <td><?php echo htmlspecialchars($app['process_name']); ?></td>
            <td><?php echo htmlspecialchars($app['window_title']); ?></td>
            <td><?php echo number_format($app['cpu_usage'], 1); ?>%</td>
            <td><?php echo number_format($app['memory_usage']); ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
```

---

## 📱 FEATURE #5: MEDIA SOCIAL DETECTION

### Database
```sql
CREATE TABLE media_social_activity (
    id INT,
    agent_id VARCHAR(50),
    platform VARCHAR(50),     -- Facebook, Instagram, TikTok, Twitter, YouTube, WhatsApp, Telegram
    app_name VARCHAR(255),    -- Browser/App terdeteksi
    window_title VARCHAR(500),
    url VARCHAR(500),
    activity_type ENUM('browser', 'app', 'chat', 'video', 'post'),
    detected_at TIMESTAMP
);
```

### Supported Platforms
- ✅ Facebook (facebook.com)
- ✅ Instagram (instagram.com)
- ✅ TikTok (tiktok.com)
- ✅ Twitter/X (twitter.com, x.com)
- ✅ YouTube (youtube.com)
- ✅ WhatsApp (web.whatsapp.com)
- ✅ Telegram (web.telegram.org)
- ✅ LinkedIn (linkedin.com)
- ✅ Discord (discord.com)
- ✅ Snapchat (snapchat.com)

### API Endpoints

#### Log Media Social Activity (dari Agent)
```
POST /api/media-social
Body: {
    "agent_id": "117295468",
    "activities": [
        {
            "app_name": "chrome.exe",
            "url": "https://www.facebook.com/",
            "window_title": "Facebook - Chrome",
            "activity_type": "browser"
        },
        {
            "app_name": "TikTok.exe",
            "window_title": "TikTok - Desktop App",
            "activity_type": "app"
        }
    ]
}

Response:
{
    "success": true,
    "message": "Media social activities logged",
    "inserted": 2
}
```

#### Get All Media Social Activity
```
GET /api/media-social?agent_id=117295468&limit=50

Response: List semua aktivitas media sosial terdeteksi
```

#### Get Activity by Platform
```
GET /api/media-social?agent_id=117295468&type=platform&platform=Facebook&limit=20

Response: Semua aktivitas Facebook yang terdeteksi
```

#### Get Active Platforms (last 30 min)
```
GET /api/media-social?agent_id=117295468&type=active

Response:
{
    "success": true,
    "data": [
        {"platform": "Facebook", "count": 5},
        {"platform": "Instagram", "count": 3},
        {"platform": "YouTube", "count": 2}
    ]
}
```

#### Get Daily Report
```
GET /api/media-social?agent_id=117295468&type=daily-report&date=2026-03-28

Response:
{
    "success": true,
    "data": [
        {
            "platform": "Facebook",
            "activity_count": 15,
            "first_detected": "2026-03-28 08:30:00",
            "last_detected": "2026-03-28 17:45:00"
        },
        {
            "platform": "Instagram",
            "activity_count": 8,
            "first_detected": "2026-03-28 12:00:00",
            "last_detected": "2026-03-28 16:20:00"
        }
    ],
    "date": "2026-03-28"
}
```

### Dashboard View
```php
<!-- di dashboard/views/media-social.php -->
<h2>📱 Media Social Activity</h2>

<div class="alert alert-info">
    <strong>Currently Active:</strong>
    <?php 
    $activePlatforms = json_decode($apiResponse, true)['data'];
    foreach ($activePlatforms as $p) {
        echo $p['platform'] . ' (' . $p['count'] . ' activities) | ';
    }
    ?>
</div>

<table class="table">
    <thead>
        <tr>
            <th>Platform</th>
            <th>Activity Count</th>
            <th>First Detected</th>
            <th>Last Detected</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($activePlatforms as $platform): ?>
        <tr>
            <td><?php echo htmlspecialchars($platform['platform']); ?></td>
            <td><?php echo $platform['activity_count']; ?></td>
            <td><?php echo htmlspecialchars($platform['first_detected']); ?></td>
            <td><?php echo htmlspecialchars($platform['last_detected']); ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
```

---

## 🚀 UPLOAD CHECKLIST

Files to upload ke webhost:

- [ ] `database/schema_updates_features_2_5.sql` - Run ini di database
- [ ] `api/controllers/ApplicationMonitorController.php`
- [ ] `api/controllers/MediaSocialController.php`
- [ ] `api/index.php` (updated dengan new handlers)
- [ ] `dashboard/views/applications.php` (new)
- [ ] `dashboard/views/media-social.php` (new)
- [ ] `screenshots/` directory

---

## 💻 AGENT C# UPDATES NEEDED

1. **ScreenshotService** - Capture screenshot setiap 30 detik
2. **ApplicationMonitorService** - Monitor running processes setiap 60 detik
3. **MediaSocialDetectionService** - Detect active media sosial windows setiap 30 detik
4. **BandwidthThrottleService** - Apply bandwidth limits via command

---

## 📊 Performance Notes

| Feature | Data Size | Frequency | Storage/Min |
|---------|-----------|-----------|------------|
| Screenshots | 100-500 KB | 30 sec | ~100 MB/hour |
| Applications | 10-50 KB | 60 sec | ~0.6 MB/hour |
| Media Social | 1-5 KB | 30 sec | ~10 KB/hour |
| Bandwidth Log | 0.5 KB | on-demand | Variable |

---

## ✅ Status

- ✅ Database schemas created
- ✅ API Controllers implemented
- ✅ API Endpoints configured
- ✅ Documentation complete
- ⏳ Dashboard views pending
- ⏳ Agent C# code pending

**Next: Upload files & test!** 🚀

