<?php
    session_start();
if(!isset($_SESSION['user_id'])) {
    die("<p>Du musst dich zuerst Einloggen oder Regestrieren <br> <a href='login/login-page.html'>Hier Einloggen</a> <br> <a href='signup/signup-page.html'>Hier Regestrieren</a>");
}

include "db_connect.php";

if (!isset($_GET['user'])) {
    die("Kein Benutzer angegeben.");
}

$username = $_GET['user'];
$userId = $_SESSION['user_id'];

try {
    // Benutzerdaten abrufen
    $stmt = $pdo->prepare("SELECT id, username, email, created_at FROM users WHERE username = :username");
    $stmt->execute([':username' => $username]);
    $selected_user = $stmt->fetch(PDO::FETCH_ASSOC);

    $stmt1 = $pdo->prepare("SELECT username, email, created_at FROM users WHERE id = :id");
    $stmt1->execute([':id' => $userId]);
    $user = $stmt1->fetch(PDO::FETCH_ASSOC);

    $stmt2 = $pdo->prepare("SELECT file_path FROM profile_pictures WHERE user_id = :user_id");
    $stmt2->execute([':user_id' => $userId]);
    $profile_pic2 = $stmt2->fetch(PDO::FETCH_ASSOC);

    $stmt3 = $pdo->prepare("
        SELECT users.username, profile_pictures.file_path 
        FROM users 
        LEFT JOIN profile_pictures ON users.id = profile_pictures.user_id 
        ORDER BY profile_pictures.uploaded_at DESC
        ");
    $stmt3->execute();
    $users = $stmt3->fetchAll(PDO::FETCH_ASSOC);

    if (!$selected_user) {
        die("Benutzer nicht gefunden!");
    }

    $stmt2 = $pdo->prepare("SELECT file_path FROM profile_pictures WHERE user_id = :user_id");
    $stmt2->execute([':user_id' => $selected_user['id']]);
    $profile_pic = $stmt2->fetch(PDO::FETCH_ASSOC);


    if ($profile_pic) {
        $selected_profile_image = $profile_pic['file_path'];
    } else {
        $selected_profile_image = 'profile-pics/default.jpeg';
    }

    if ($profile_pic2) {
        $selected_profile_image1 = $profile_pic2['file_path'];
    } else {
        $selected_profile_image1 = 'profile-pics/default.jpeg';
    }

} catch (PDOException $e) {
    die("Fehler: " . $e->getMessage());
}
?>

<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Profil von <?php echo htmlspecialchars($selected_user['username']); ?></title>
    <link rel="stylesheet" href="style.css?v=<?php echo time(); ?>">
</head>
<body>
<div class="container">
    <div class="col-3">
        <div class="navigation-body">
            <h1>
                <?php echo "<img class = 'profile-pictures' src='$selected_profile_image1' alt='pv' width='50'>";?>
                <a href=""><?php echo htmlspecialchars($user['username']); ?></a>
            </h1>
            <ul>
                <li><a href="index.php">Home</a></li>
                <li><a href="index.php?page=settings">Profil</a></li>
                <li><a href="">Nachrichten</a></li>
                <li><a href="">Benachrichtiungen</a></li>

            </ul>
        </div>
        <div class="navigation-body">
            <ul>
                <?php foreach ($users as $benutzer): ?>
                    <li>
                        <form action="profile.php" method="post">
                            <img src="<?=  htmlspecialchars($benutzer['file_path'])  ?: 'profile-pics/default.jpeg' ?>" alt="Profilbild">
                            <a href="profile.php?user=<?= urlencode($benutzer['username']) ?>"><?=  htmlspecialchars($benutzer['username']) ?></a>
                        </form>
                    </li>
                <?php endforeach; ?>
            </ul>
        </div>

    </div>
    <div class="profile-body col-9">
        <div class="profile-header">
            <h1>Profil von <?php echo htmlspecialchars($selected_user['username']); ?></h1>
            <img src="<?php echo htmlspecialchars($selected_profile_image); ?>" alt="Profilbild" width="100">
            <p>Email: <?php echo htmlspecialchars($selected_user['email']); ?></p>
            <p>Registriert seit: <?php echo htmlspecialchars($selected_user['created_at']); ?></p>
            <a href="index.php">Zurück zur Startseite</a>
        </div>

        <?php
        $stmt45 = $pdo->prepare("
            SELECT posts.*, profile_pictures.file_path AS profile_picture, users.username
            FROM posts
            LEFT JOIN profile_pictures ON posts.user_id = profile_pictures.user_id
            LEFT JOIN users ON posts.user_id = users.id
            WHERE users.username = :username
            ORDER BY posts.created_at DESC
        ");
        $stmt45->execute([':username' => $username]);
        $posts = $stmt45->fetchAll(PDO::FETCH_ASSOC);
        ?>
        <?php foreach ($posts as $post): ?>
            <div class="post">
                <img src="<?= htmlspecialchars($post['profile_picture']) ?: 'profile-pics/default.jpeg'; ?>" width="50px" alt="">
                <p><strong><?php echo $post['username']; ?></strong></p>
                <p><?php echo nl2br(htmlspecialchars($post['text_content'])); ?></p>

                <?php if (!empty($post['image_path'])): ?>
                    <img class="post-pics" src="<?php echo htmlspecialchars($post['image_path']); ?>" alt="Bild zum Post">
                <?php endif; ?>

                <p><small>Erstellt am: <?php echo $post['created_at']; ?></small></p>
                <?php if ($userId == $post['user_id']): ?>
                    <form action="upload-post/delete-post.php" method="post">
                        <input type="hidden" name="post_id" value="<?= htmlspecialchars($post['id']) ?>">
                        <button type="submit">Löschen</button>
                    </form>
                <?php endif; ?>

            </div>
        <?php endforeach; ?>

    </div>
</div>
</body>
</html>