<?php

/*
 * Reads the markdown file for a given article and generates the HTML for it.
 */
function article_page_from_markdown($article_id)
{
    $article = exec_stmt('SELECT * FROM article WHERE article_id = ?', 'i', $article_id)->fetch_assoc();
    $author = exec_stmt('SELECT user_id, username, profile_picture FROM user WHERE user_id = ?', 'i', $article['author_id'])->fetch_assoc();
    $markdown = read_file_one_string($_ENV['ARTICLES_FQ_PATH'] . $article_id . '/article.md');
    $following = exec_stmt('SELECT follower_user_id, following_user_id FROM follows WHERE follower_user_id = ? AND following_user_id = ?', 'ii', $_SESSION['user_id'], $author['user_id'])->fetch_assoc();

    $parsedown = new Parsedown();
    $parsedown->setSafeMode(true);
    $article_content = $parsedown->text($markdown);

    $html = '
        <div class="article">
            
            <div class="row_space_between">
                <h1>' . $article['title'] . '</h1>
                ' . (isset($_SESSION['user_id']) && $_SESSION['user_id'] == $article['author_id'] ? '<a href="articles.php?view=compose&article_id=' . $article_id . '"><i class="nf nf-fa-edit"></i></a>' : '') . '
            </div>
            <div class="row_space_between">
                <div class="row_space_between">
                    <div class="col_right">
                        <h5>Written by <a href="user.php?view=display&user_id=' . $author['user_id'] . '">' . $author['username'] . '</a></h5>
                        <p>Published on ' . $article['published_at'] . '<br>
                        Lasted edited on ' . $article['updated_at'] . '</p>
                    </div>
    ';

    if ($_SESSION['user_id'] != $author['user_id'] && !$following) {
        $html .= '
                    <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="' . $author['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 ($following) {
        $html .= '
            <div class="margin_right">
                <form method="post">
                    <input type="hidden" name="form_id" value="unfollow">
                    <input type="hidden" name="author_id" value="' . $author['user_id'] . '">
                    <input type="submit" value="Unfollow">
                </form>
            </div>
        ';
    }

    $html .= '
                </div>
                <div class="card_pic_box">
                    <img src="' . $_ENV['PROFILE_IMAGES_PQ_PATH'] . $author['profile_picture'] . '" class="card_profile_picture" />
                </div>
            </div>
            <div class="line" style="margin-bottom: 15px;"></div>
                <div class="article_content">
                    ' . $article_content . '
                </div>
        </div>
    ';

    return $html;
}
