#!/bin/bash
# PCMonitor Agent Connection Diagnostic

echo "========================================"
echo " PCMonitor Agent - Connection Diagnostic"
echo "========================================"
echo ""

# 1. Check agent config
echo "[1] Checking Agent Configuration..."
AGENT_CONFIG="/var/www/html/tracker/agent-csharp/config.json"
if [ -f "$AGENT_CONFIG" ]; then
    API_URL=$(grep -o '"api_url":"[^"]*' "$AGENT_CONFIG" | cut -d'"' -f4)
    echo "   ✓ config.json found"
    echo "   API URL: $API_URL"
else
    echo "   ✗ config.json not found at $AGENT_CONFIG"
fi

echo ""
echo "[2] Testing API Connectivity..."

# Test connection to webhost
API_ENDPOINT="http://tracker.lppmunud.id/tracker/api/status"
echo "   Testing: $API_ENDPOINT"

RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "$API_ENDPOINT")
if [ "$RESPONSE" = "200" ]; then
    echo "   ✓ API is reachable (HTTP 200)"
else
    echo "   ✗ API returned HTTP $RESPONSE"
fi

echo ""
echo "[3] Checking Recent Activity Logs..."
LOG_DIR="C:\\ProgramData\\PCMonitor\\Logs"
if [ -d "$LOG_DIR" ]; then
    echo "   Latest log entries:"
    ls -lt "$LOG_DIR"/*.log 2>/dev/null | head -3
else
    echo "   ✗ Log directory not found"
fi

echo ""
echo "[4] Checking Database..."
echo "   Verify MySQL is accessible at webhost..."
echo "   Database should have tables: agents, pc_activities, running_processes"

echo ""
echo "========================================"
