<?php
include_once 'components/core/card.php';

function user_view($user_id)
{
    if ($_SESSION['user_id'] != $user_id)
        $following = exec_stmt('SELECT follower_user_id, following_user_id FROM follows WHERE follower_user_id = ? AND following_user_id = ?', 'ii', $_SESSION['user_id'], $user_id)->fetch_assoc();
    $user = exec_stmt('SELECT user_id, username, email, role_id, created_at, last_active, is_active, profile_picture FROM user WHERE user_id = ?', 'i', $user_id)->fetch_assoc();

    if ($user == null)
        header('Location: articles.php?view=sort&sort=newest');

    $view = '
    <div class="user_view">
        <div class="row_space_between">
            <div class="row_space_between">
                <h1>' . $user['username'] . '</h1>
    ';

    if ($_SESSION['user_id'] != $user_id && isset($following) && !$following) {
        $view .= '
                    <div class="dropdown">
                        <span>Follow</span>
                        <div class="dropdown_content">
                            <form method="post">
                                <input type="hidden" name="form_id" value="follow">
                                <input type="hidden" name="author_id" value="' . $user_id . '">
                                <button name="follow" value="no_notify" class="button"><i class="nf nf-md-bell_cancel"></i></button>
                                <button name="follow" value="email_notify" class="button"><i class="nf nf-cod-mail"></i></button>
                            </form>
                        </div>
                    </div>
        ';
    } else if (isset($following) && $following) {
        $view .= '
            <div class="margin_right">
                <form method="post">
                    <input type="hidden" name="form_id" value="unfollow">
                    <input type="hidden" name="author_id" value="' . $user_id . '">
                    <input type="submit" value="Unfollow">
                </form>
            </div>
        ';
    }

    $view .= '
            </div>
            <div class="card_pic_box">
                <img src="' . $_ENV['PROFILE_IMAGES_PQ_PATH'] . $user['profile_picture'] . '" class="profile_picture" />
            </div>
        </div>
        <div class="line"></div>
    ';

    // TODO: Allow user to update profile picture.
    // <button id="update_profile_picture_form_button" class="modal_button underline">Update Profile Picture</button>
    // <div id="update_profile_picture_form" class="modal_form">' . update_profile_picture_form() . '</div>
    if ((isset($_SESSION['user_id']) && $_SESSION['user_id'] == $user_id) || (isset($_SESSION['role_id']) && $_SESSION['role_id'] <= admin)) {
        $view .= '
            <h3>User Actions</h3>
            <div class="user_actions">
                <button id="reset_pw_form_button" class="modal_button underline">Reset Password</button>
                <button id="update_email_form_button" class="modal_button underline">Update Email</button>
                <button id="update_username_form_button" class="modal_button underline">Update Username</button>
                <button id="request_role_form_button" class="modal_button underline">Request Role</button>
                <button id="delete_account_form_button" class="modal_button underline">Delete Account</button>
            </div>

            <div id="user_actions_modal" class="modal">
                <div id="modal_content"></div>
            </div>

            <div style="display: none;">
                <div id="reset_pw_form" class="modal_form">' . reset_pw_form() . '</div>
                <div id="update_email_form" class="modal_form">' . update_email_form() . '</div>
                <div id="update_username_form" class="modal_form">' . update_username_form() . '</div>
                <div id="request_role_change_form" class="modal_form">' . request_role_change_form() . '</div>
                <div id="delete_account_form" class="modal_form">' . delete_account_form() . '</div>
            </div>
        </div>
        ';
    } else {
        $ids = articles_ids_by_author($_GET['user_id']);
        $view .= '
            <div class="article_cards">
        ';

        foreach ($ids as $id)
            $view .= article_card($id);

        $view .= '</div>';
    }

    return $view;
}
