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

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

$student_id = $_SESSION['student_id'];

// Check if friend_id is set
if (!isset($_POST['friend_id'])) {
    header("Location: friends.php");
    exit();
}

$friend_id = $_POST['friend_id'];

// Fetch friend's profile information
$sql_friend_profile = "SELECT p.name, p.profile_picture, p.bio 
                       FROM students s 
                       JOIN profiles p ON s.student_id = p.student_id 
                       WHERE s.student_id = ?";
$stmt_friend_profile = $conn->prepare($sql_friend_profile);
$stmt_friend_profile->bind_param("i", $friend_id);
$stmt_friend_profile->execute();
$result_friend_profile = $stmt_friend_profile->get_result();

if ($result_friend_profile->num_rows == 0) {
    echo "Profile not found.";
    exit();
}

$friend_profile = $result_friend_profile->fetch_assoc();
$conn->close();
?>

<!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="styles.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <title><?php echo $friend_profile['name']; ?>'s Profile</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f2f5;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .profile-container {
            background-color: #fff;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            width: 80%;
            max-width: 800px;
            overflow: hidden;
        }

        .profile-header {
            background-color: #1877f2;
            color: white;
            text-align: center;
            padding: 20px;
        }

        .profile-header img {
            width: 150px;
            height: 150px;
            border-radius: 50%;
            border: 5px solid white;
        }

        .profile-body {
            padding: 20px;
        }

        .profile-body h2 {
            margin: 0 0 20px;
            font-size: 28px;
            color: #333;
        }

        .profile-body p {
            margin: 10px 0;
            font-size: 18px;
            color: #555;
        }

        .profile-body h3 {
            margin-top: 30px;
            font-size: 24px;
            color: #333;
        }

        .back-button {
            display: inline-block;
            margin-top: 20px;
            padding: 10px 20px;
            font-size: 16px;
            background-color: #1877f2;
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            text-decoration: none;
        }

        .back-button:hover {
            background-color: #155db2;
        }
        
    </style>
</head>
<body>
    <div class="profile-container">
        <div class="profile-header">
            <img src="uploads/<?php echo $friend_profile['profile_picture']; ?>" alt="<?php echo $friend_profile['name']; ?>'s Picture">
            <h1><?php echo $friend_profile['name']; ?></h1>
        </div>
        <div class="profile-body">
            
            <h2>About</h2>
            
            <p><strong>Bio:</strong> <?php echo $friend_profile['bio']; ?></p>
            <h3>Interests</h3>
            
            <a href="friends.php" class="back-button">Back to Friends</a>
        </div>
    </div>
</body>
</html>
