<?php
session_start();

// Check if user is logged in and is the developer
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true || $_SESSION['username'] !== 'unrealengine_developer') {
    header('Location: https://tradervai.com/');
    exit;
}

function resetUserPassword($username) {
    $dbFile = '../js/users_db.json';
    if (!file_exists($dbFile)) {
        return false;
    }
    
    $data = json_decode(file_get_contents($dbFile), true);
    foreach ($data['users'] as &$user) {
        if ($user['username'] === $username) {
            $user['updated_password'] = '';
            $user['default_password'] = '12345678';
            file_put_contents($dbFile, json_encode($data, JSON_PRETTY_PRINT));
            return true;
        }
    }
    return false;
}

$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['reset_user'])) {
    $userToReset = $_POST['reset_user'];
    if (resetUserPassword($userToReset)) {
        $message = "Password reset successful for user: $userToReset";
    } else {
        $message = "Failed to reset password for user: $userToReset";
    }
}

if (isset($_POST['logout'])) {
    session_destroy();
    header('Location: https://tradervai.com/');
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Analytics Dashboard</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Roboto', sans-serif;
            background: #151A28;
            color: #fff;
            min-height: 100vh;
        }
        
        .header {
            background: #192033;
            padding: 20px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .header h1 {
            color: #2B99FF;
            font-size: 24px;
        }
        
        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .logout-btn {
            background: linear-gradient(135deg, #ff4757, #c44569);
            border: none;
            padding: 10px 20px;
            border-radius: 8px;
            color: #fff;
            cursor: pointer;
            font-weight: 600;
            transition: all 0.3s ease;
        }
        
        .logout-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(255, 71, 87, 0.4);
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 40px 20px;
        }
        
        .analytics-section {
            background: #192033;
            border-radius: 15px;
            padding: 30px;
            margin-bottom: 30px;
            border: 1px solid rgba(255, 255, 255, 0.1);
        }
        
        .section-title {
            color: #2B99FF;
            font-size: 20px;
            margin-bottom: 25px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .reset-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
        }
        
        .user-card {
            background: rgba(255, 255, 255, 0.05);
            border: 2px solid rgba(255, 255, 255, 0.1);
            border-radius: 12px;
            padding: 20px;
            text-align: center;
            transition: all 0.3s ease;
        }
        
        .user-card:hover {
            border-color: #2B99FF;
            transform: translateY(-5px);
            box-shadow: 0 10px 30px rgba(43, 153, 255, 0.2);
        }
        
        .user-icon {
            font-size: 40px;
            color: #2B99FF;
            margin-bottom: 15px;
        }
        
        .user-name {
            font-size: 16px;
            font-weight: 600;
            margin-bottom: 15px;
            color: #fff;
        }
        
        .reset-btn {
            background: linear-gradient(135deg, #ff6b6b, #ee5a52);
            border: none;
            padding: 12px 24px;
            border-radius: 8px;
            color: #fff;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            width: 100%;
        }
        
        .reset-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(255, 107, 107, 0.4);
        }
        
        .message {
            background: rgba(15, 160, 84, 0.2);
            border: 1px solid #0FA054;
            color: #0FA054;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            text-align: center;
        }
        
        .back-btn {
            background: linear-gradient(135deg, #2B99FF, #026FD3);
            border: none;
            padding: 12px 24px;
            border-radius: 8px;
            color: #fff;
            text-decoration: none;
            font-weight: 600;
            display: inline-flex;
            align-items: center;
            gap: 8px;
            transition: all 0.3s ease;
        }
        
        .back-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(43, 153, 255, 0.4);
        }
    </style>
</head>
<body>
    <div class="header">
        <h1><i class="fas fa-chart-line"></i> Analytics Dashboard</h1>
        <div class="user-info">
            <span>Welcome, <?php echo htmlspecialchars($_SESSION['username']); ?></span>
            <form method="POST" style="display: inline;">
                <button type="submit" name="logout" class="logout-btn">
                    <i class="fas fa-sign-out-alt"></i> Logout
                </button>
            </form>
        </div>
    </div>

    <div class="container">
        <a href="../index.php" class="back-btn">
            <i class="fas fa-arrow-left"></i> Back to Dashboard
        </a>
        
        <?php if ($message): ?>
            <div class="message"><?php echo htmlspecialchars($message); ?></div>
        <?php endif; ?>
        
        <div class="analytics-section">
            <h2 class="section-title">
                <i class="fas fa-key"></i> Reset Password
            </h2>
            
            <div class="reset-grid">
                <div class="user-card">
                    <div class="user-icon">
                        <i class="fas fa-user-tie"></i>
                    </div>
                    <div class="user-name">unrealengine_admin</div>
                    <form method="POST">
                        <button type="submit" name="reset_user" value="unrealengine_admin" class="reset-btn">
                            <i class="fas fa-redo"></i> Reset Password
                        </button>
                    </form>
                </div>
                
                <div class="user-card">
                    <div class="user-icon">
                        <i class="fas fa-user"></i>
                    </div>
                    <div class="user-name">unrealengine_user1</div>
                    <form method="POST">
                        <button type="submit" name="reset_user" value="unrealengine_user1" class="reset-btn">
                            <i class="fas fa-redo"></i> Reset Password
                        </button>
                    </form>
                </div>
                
                <div class="user-card">
                    <div class="user-icon">
                        <i class="fas fa-user"></i>
                    </div>
                    <div class="user-name">unrealengine_user2</div>
                    <form method="POST">
                        <button type="submit" name="reset_user" value="unrealengine_user2" class="reset-btn">
                            <i class="fas fa-redo"></i> Reset Password
                        </button>
                    </form>
                </div>
                
                <div class="user-card">
                    <div class="user-icon">
                        <i class="fas fa-code"></i>
                    </div>
                    <div class="user-name">unrealengine_developer</div>
                    <form method="POST">
                        <button type="submit" name="reset_user" value="unrealengine_developer" class="reset-btn">
                            <i class="fas fa-redo"></i> Reset Password
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>