<?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();
            } else if ($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();
	//todo: rewrite
?>

<h2>Following</h2>

<?php	
	foreach ($following as $user) { $usern = $user['username']; ?>
		<div class='comment'>
			<div class="avatar">
				<img src="/pfp/<?php echo $usern ?>.png" class="avatar-img">
				<div>
					<div><b>
						<a class="link" href="/user.php?id=<?php echo $usern ?>">
							<?php echo $usern ?>
						</a>
					</b>
					</div>
				</div>
			</div>
		</div>	
<?php   
}
?>

<h2>Followers</h2>

<?php	
	foreach ($followers as $user) { $usern = $user['target']; ?>
		<div class='comment'>
			<div class="avatar">
				<img src="/pfp/<?php echo $usern ?>.png" class="avatar-img">
				<div>
					<div><b>
						<a class="link" href="/user.php?id=<?php echo $usern ?>">
							<?php echo $usern ?>
						</a>
					</b>
					</div>
				</div>
			</div>
		</div>	
<?php   
}
?>
<?php 
	page_footer();
?>