<?php
session_start();
include('db_connection.php');
include('functions.php');

// Check if the student is logged in
if (!isset($_SESSION['student_id'])) {
    header("Location: login.php");
    exit();
}

$student_id = $_SESSION['student_id'];

// Fetch student details
$sql_student_details = "SELECT name, address, class_id FROM students WHERE student_id = ?";
$stmt_student_details = $conn->prepare($sql_student_details);
$stmt_student_details->bind_param("i", $student_id);
$stmt_student_details->execute();
$result_student_details = $stmt_student_details->get_result();
$student = $result_student_details->fetch_assoc();

// Get criteria values
$student_name = $student['name'];
$student_address = $student['address'];
$student_class_id = $student['class_id'];

// Fetch potential friends based on criteria
// Same Group
$sql_same_group = "
    SELECT s.student_id, s.name, s.profile_picture, s.address
    FROM students s
    JOIN group_members gm ON s.student_id = gm.student_id
    WHERE gm.group_id IN (SELECT group_id FROM group_members WHERE student_id = ?)
      AND s.student_id != ?
    GROUP BY s.student_id
";
$stmt_same_group = $conn->prepare($sql_same_group);
$stmt_same_group->bind_param("ii", $student_id, $student_id);
$stmt_same_group->execute();
$result_same_group = $stmt_same_group->get_result();

// Same Address
$sql_same_address = "
    SELECT student_id, name, profile_picture, address
    FROM students
    WHERE address = ? AND student_id != ?
";
$stmt_same_address = $conn->prepare($sql_same_address);
$stmt_same_address->bind_param("si", $student_address, $student_id);
$stmt_same_address->execute();
$result_same_address = $stmt_same_address->get_result();

// Same Profile Name
$sql_same_profile_name = "
    SELECT s.student_id, s.name, s.profile_picture, s.address
    FROM students s
    JOIN profiles p ON s.student_id = p.student_id
    WHERE p.name = (SELECT name FROM profiles WHERE student_id = ?) AND s.student_id != ?
";
$stmt_same_profile_name = $conn->prepare($sql_same_profile_name);
$stmt_same_profile_name->bind_param("ii", $student_id, $student_id);
$stmt_same_profile_name->execute();
$result_same_profile_name = $stmt_same_profile_name->get_result();

// Same Class
$sql_same_class = "
    SELECT student_id, name, profile_picture, address
    FROM students
    WHERE class_id = ? AND student_id != ?
";
$stmt_same_class = $conn->prepare($sql_same_class);
$stmt_same_class->bind_param("ii", $student_class_id, $student_id);
$stmt_same_class->execute();
$result_same_class = $stmt_same_class->get_result();

// Combine results
$all_results = [];
while ($row = $result_same_group->fetch_assoc()) {
    $all_results[] = $row;
}
while ($row = $result_same_address->fetch_assoc()) {
    $all_results[] = $row;
}
while ($row = $result_same_profile_name->fetch_assoc()) {
    $all_results[] = $row;
}
while ($row = $result_same_class->fetch_assoc()) {
    $all_results[] = $row;
}

// Fetch existing friend requests
$sql_existing_requests = "SELECT requested_id FROM student_requests WHERE student_id = ?";
$stmt_existing_requests = $conn->prepare($sql_existing_requests);
$stmt_existing_requests->bind_param("i", $student_id);
$stmt_existing_requests->execute();
$existing_requests_result = $stmt_existing_requests->get_result();
$existing_requests = [];
while ($row = $existing_requests_result->fetch_assoc()) {
    $existing_requests[] = $row['requested_id'];
}

// Fetch people you may know from the database
$query = "
    SELECT s.student_id, s.name, p.profile_picture
    FROM students s
    JOIN profiles p ON s.student_id = p.student_id
    WHERE s.student_id != ? 
    AND (
        s.class_id IN (SELECT class_id FROM students WHERE student_id = ?) 
        OR s.address = (SELECT address FROM students WHERE student_id = ?) 
        OR s.name = (SELECT name FROM students WHERE student_id = ?) 
        OR s.class_id = (SELECT class_id FROM students WHERE student_id = ?)
    )
";
$stmt = $conn->prepare($query);
$stmt->bind_param('iiiii', $logged_in_student_id, $logged_in_student_id, $logged_in_student_id, $logged_in_student_id, $logged_in_student_id);
$stmt->execute();
$result = $stmt->get_result();

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="sidebar.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <title>People You May Know</title>
    <style>
        /* Add your CSS styling here */
        /* Include styles from previous example and additional styles for this page */
        /* Customize as needed */
    </style>
</head>
<body>
    <header>
        <!-- Header code from previous example -->
    </header>
    <div class="container">
        <div class="sidebar">
            <h2>People You May Know</h2>
          <?php while ($row = $result->fetch_assoc()): ?>
    <div class="profile">
        <img src="<?php echo htmlspecialchars($row['profile_picture'] ? 'path/to/images/' . $row['profile_picture'] : 'path/to/default-image.jpg'); ?>" alt="Profile Picture">
        <p><?php echo htmlspecialchars($row['name']); ?></p>
        <button onclick="sendRequest(<?php echo $row['student_id']; ?>)">Add Friend</button>
        <button onclick="removeRequest(<?php echo $row['student_id']; ?>)">Remove</button>
    </div>
<?php endwhile; ?>
        </div>
        <div class="main-content">
            <!-- Main content area -->
        </div>
    </div>
<script>
    function sendRequest(studentId) {
        if (confirm("Are you sure you want to send a friend request to this student?")) {
            window.location.href = `send_request.php?student_id=${studentId}`;
        }
    }

    function removeSuggestion(studentId) {
        if (confirm("Are you sure you want to remove this suggestion?")) {
            window.location.href = `remove_suggestion.php?student_id=${studentId}`;
        }
    }
</script>
</body>
</html>
