<?php
// ===== DATABASE CONNECTION =====
$host = "localhost";
$user = "vuitrisj_derek";
$pass = "vuitrisj_derek";
$db   = "vuitrisj_derek";

$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
    die("Connection failed");
}

// ===== FETCH PREMIUM CONTENT =====
$query = "SELECT group_link FROM class_links WHERE status='active' LIMIT 1";
$result = $conn->query($query);

$link = "#";

if ($result && $result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $link = $row['group_link'];
}

// ✅ PHP REDIRECT (FASTEST)
if (!empty($link) && filter_var($link, FILTER_VALIDATE_URL)) {
    header("Location: " . $link);
    exit();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redirecting...</title>

    <!-- HTML META REDIRECT (fallback) -->
    <meta http-equiv="refresh" content="0;url=<?php echo htmlspecialchars($link); ?>">

    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            font-family: Arial, sans-serif;
            background: #f9fafb;
        }
        .box {
            text-align: center;
        }
        a {
            color: #0072ff;
            text-decoration: none;
            font-weight: bold;
        }
    </style>

    <!-- JS REDIRECT (extra fallback) -->
    <script>
        window.location.href = "<?php echo htmlspecialchars($link); ?>";
    </script>
</head>
<body>
    <div class="box">
        <p>Redirecting you...</p>
        <p>If not redirected, <a href="<?php echo htmlspecialchars($link); ?>">click here</a></p>
    </div>
</body>
</html>