<?php
require(__DIR__ . "/../libs/page.php");
require(__DIR__ . "/../libs/form.php");
require(__DIR__ . "/../libs/comment.php");

$roles = array();

page_header();

// this is a mess
function get_handler()
{
    global $db;
    global $username;
    global $user;
    global $bio;
    global $followers;
    global $following;
    global $roles;

    // there is 100% a better way to do this but i need to test
    if (!array_key_exists('id', $_GET)) {
        die();
    }

    $user = $_GET['id'];

    if (array_key_exists('type', $_GET)) {
        $type = $_GET['type'];
        if ($type == 'follow') {
            follow();
        } elseif ($type == 'settings') {
            settings();
        }
    }

    $stmt = $db->prepare("SELECT * FROM main.user WHERE UPPER(username) LIKE UPPER(?)"); //weirdly, this requires a schema name
    $stmt->execute([$user]);

    $result = $stmt->fetch(PDO::FETCH_ASSOC);

    $stmt = $db->prepare("SELECT * FROM main.follow WHERE target = ?");
    $stmt->execute([$user]);

    $following = $stmt->fetchAll(PDO::FETCH_DEFAULT);

    $stmt = $db->prepare("SELECT * FROM main.follow WHERE username = ?");
    $stmt->execute([$user]);

    $followers = $stmt->fetchAll(PDO::FETCH_DEFAULT);
}
get_handler();
?>

<h2>Following</h2>

<?php
    foreach ($following as $user) {
        user_block($user['username'], false, false);
    }
?>

<h2>Followers</h2>

<?php
    foreach ($followers as $user) {
        user_block($user['target'], false, false);
    }
page_footer();
?>