<?php
require_once 'db_functions.php';

function article_ids_by_recency()
{
    $query = 'SELECT article_id FROM article ORDER BY published_at DESC;';
    $results = query_one_or_more_results($query);

    $ids = [];

    while ($row = mysqli_fetch_array($results))
        $ids[] = $row['article_id'];

    return $ids;
}

function articles_ids_by_author($author_id)
{
    $query = 'SELECT article_id FROM article ORDER BY published_at DESC WHERE author_id = ' . $author_id . ';';
    $results = query_one_or_more_results($query);

    $ids = [];

    while ($row = mysqli_fetch_array($results))
        $ids[] = $row['article_id'];

    return $ids;
}

function article_tags()
{
    $query = 'SELECT * FROM tags;';
    $results = query_one_or_more_results($query);
    $tags = [];

    while ($row = mysqli_fetch_array($results))
        $tags[] = $row;

    return $tags;
}

function article_ids_by_tag($tag_id)
{
    $query = 'SELECT article_id FROM article_tags INNER JOIN tags ON article_tags.tag_id = tags.tag_id WHERE tag = "' . $tag_id . '";';
    $results = query_one_or_more_results($query);

    $ids = [];

    while ($row = mysqli_fetch_array($results))
        $ids[] = $row['article_id'];

    return $ids;
}

/*
 * Reads the markdown file for a given article and generates the HTML for it.
 */
function article_page_from_markdown($article_id)
{
    $query = 'SELECT * FROM article WHERE article_id = ' . $article_id . ';';
    $article = query_one_result($query);

    $query = 'SELECT user_id, username, profile_picture FROM user WHERE user_id = ' . $article['author_id'] . ';';
    $author = query_one_result($query);

    $markdown = read_file_one_string($_ENV['ARTICLES_FQ_PATH'] . $article_id . '/article.md');

    $parsedown = new Parsedown();

    $html = '
        <div class="article">
            <h3>' . $article['title'] . '</h3>
            <div class="row">
                <div class="col_right">
                    <h5>Written by <a href="user.php?action=view&user_id=' . $author['user_id'] . '" style="font-size: 1.5vw;">' . $author['username'] . '</a></h5>
                    <p>Published on ' . $article['published_at'] . '</p>
                </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>
    ';

    $html .= $parsedown->text($markdown);
    $html .= '</div>';

    return $html;
}

/*
 * Generates cards for a given article.
 */
function article_card($article_id)
{
    $article_query = 'SELECT * FROM article WHERE article_id = ' . $article_id . ';';
    $article = query_one_result($article_query);

    $article_tags_query = 'SELECT tag FROM article_tags INNER JOIN tags ON article_tags.tag_id = tags.tag_id WHERE article_id = ' . $article_id . ';';
    $tags_results = query_one_or_more_results($article_tags_query);
    $tags_html = '';
    if ($tags_results != null) {
        $tags_html .= '<div class="tag_row">';

        while ($row = mysqli_fetch_array($tags_results))
            $tags_html .= '<a href="/articles.php?tag=' . $row['tag'] . '" class="tag">#' . $row['tag'] . '</a>';

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

    $author_query = 'SELECT user_id, username, profile_picture FROM user WHERE user_id = ' . $article['author_id'] . ';';
    $author = query_one_result($author_query);

    $card = '
        <div class="article_card">
            <div class="row">
                <h4>' . $article['title'] . '</h4>
                <div class="card_info_box">
                    <div class="col-right">
                        <a href="user.php?action=view&user_id=' . $author['user_id'] . '">Written by: ' . $author['username'] . '</a>
                        <small>Published: ' . $article['published_at'] . '</small>
                    </div>
                    <div class="card_pic_box">
                        <img src="' . $_ENV['PROFILE_IMAGES_PQ_PATH'] . $author['profile_picture'] . '" class="card_profile_picture">
                    </div>
                </div>
            </div>
            <div class="line"></div>
            <p>' . $article['excerpt'] . '</p>
            <div class="row">
                <a href="/articles.php?article_id=' . $article['article_id'] . '" class="button">Read More</a>
                <div class="tag_box">' . $tags_html . '</div>
            </div>
        </div>
    ';

    return $card;
}

function create_article($author_id, $title, $excerpt, $tags, $markdown_file_contents)
{
    $stmt = 'INSERT INTO article (author_id, title, excerpt, published_at) VALUES (' . $author_id . ', "' . $title . '", "' . $excerpt . '", CURRENT_TIMESTAMP);';
    $id = exec_statement($stmt, 0);

    $path = $_ENV['ARTICLES_FQ_PATH'] . $id . '/';
    mkdir($path);
    echo '<p>created ' . $path . '</p>';

    $fs = fopen($path . 'article.md', 'a');
    fwrite($fs, $markdown_file_contents);
    fclose($fs);

    $stmt = 'INSERT INTO article_tags (article_id, tag_id) VALUES ';
    for ($i = 0; $i < count($tags); $i++) {
        $stmt .= '(' . $id . ', ' . $tags[$i] . ')';

        if ($i < count($tags) - 1)
            $stmt .= ', ';

        if ($i == count($tags) - 1)
            $stmt .= ';';
    }

    if (count($tags) > 0)
        exec_statement($stmt, 0);

    return $id;
}

function delete_article($article_id)
{
    $stmt = 'DELETE FROM article_tags WHERE article_id = ' . $article_id . ';';
    exec_statement($stmt, 1);

    $stmt = 'DELETE FROM article WHERE article_id = ' . $article_id . ';';
    exec_statement($stmt, 1);

    delete_dir($_ENV['ARTICLES_FQ_PATH'] . $article_id . '/');
}

function delete_articles_by_author($author_id)
{
    $stmt = 'DELETE FROM article WHERE author_id = "' . $author_id . '";';
    return exec_statement($stmt, 1);
}

function increment_read_counter($article_id)
{
    $stmt = 'UPDATE article SET read_count = read_count + 1 WHERE article_id = ' . $article_id . ';';
    return exec_statement($stmt, 1);
}
