<?php
function topics_in_category(int $category, int $variation = 0): array {
    global $pdo;

    if($category < 1)
        return [];

    $query = 'SELECT *, UNIX_TIMESTAMP(`topic_created`) AS `topic_created`, UNIX_TIMESTAMP(`topic_bumped`) AS `topic_bumped`, UNIX_TIMESTAMP(`topic_locked`) AS `topic_locked`, UNIX_TIMESTAMP(`topic_resolved`) AS `topic_resolved`, UNIX_TIMESTAMP(`topic_confirmed`) AS `topic_confirmed`'
        . ' FROM `fmf_topics` WHERE `cat_id` = :category AND `topic_bumped` IS NOT NULL'
        . ' ORDER BY ';

    if($variation === 1)
        $query .= 'IF(`topic_confirmed` IS NULL, 0, IF(`topic_resolved` IS NULL, -1, 1)), ';

    $query .= '`topic_bumped` DESC';

    $getTopics = $pdo->prepare($query);
    $getTopics->bindValue('category', $category);
    $topics = $getTopics->execute() ? $getTopics->fetchAll(PDO::FETCH_ASSOC) : false;

    return $topics ? $topics : [];
}

function create_topic(int $category, int $user, string $title): int {
    global $pdo;

    $createTopic = $pdo->prepare('INSERT INTO `fmf_topics` (`cat_id`, `user_id`, `topic_title`) VALUES (:cat, :user, :title)');
    $createTopic->bindValue('cat', $category);
    $createTopic->bindValue('user', $user);
    $createTopic->bindValue('title', $title);
    $createTopic->execute();

    return (int)$pdo->lastInsertId();
}

function topic_info(int $topic): array {
    global $pdo;

    if($topic < 1)
        return [];

    $getTopic = $pdo->prepare('SELECT *, UNIX_TIMESTAMP(`topic_created`) AS `topic_created`, UNIX_TIMESTAMP(`topic_locked`) AS `topic_locked`, UNIX_TIMESTAMP(`topic_resolved`) AS `topic_resolved`, UNIX_TIMESTAMP(`topic_confirmed`) AS `topic_confirmed` FROM `fmf_topics` WHERE `topic_id` = :id');
    $getTopic->bindValue('id', $topic);
    $topic = $getTopic->execute() ? $getTopic->fetch(PDO::FETCH_ASSOC) : false;

    return $topic ? $topic : [];
}

function topic_bump(int $topic, ?int $post = null, bool $onlyPostId = false): void {
    global $pdo;

    if($topic < 1)
        return;

    $bump = $pdo->prepare('UPDATE `fmf_topics` SET `topic_bumped` = IF(:no_bump, `topic_bumped`, NOW()), `topic_count_replies` = IF(`topic_bumped` IS NULL, `topic_count_replies`, `topic_count_replies` + 1), `topic_last_post_id` = COALESCE(:post, `topic_last_post_id`) WHERE `topic_id` = :topic');
    $bump->bindValue('topic', $topic);
    $bump->bindValue('post', $post);
    $bump->bindValue('no_bump', $onlyPostId ? 1 : 0);
    $bump->execute();
}

function lock_topic(int $topic, bool $state): void {
    global $pdo;

    $lock = $pdo->prepare('UPDATE `fmf_topics` SET `topic_locked` = IF(:state, NOW(), NULL) WHERE `topic_id` = :topic');
    $lock->bindValue('state', $state ? 1 : 0);
    $lock->bindValue('topic', $topic);
    $lock->execute();
}

function mark_topic_resolved(int $topic, bool $state): void {
    global $pdo;

    $resolve = $pdo->prepare('UPDATE `fmf_topics` SET `topic_resolved` = IF(:state, NOW(), NULL) WHERE `topic_id` = :topic');
    $resolve->bindValue('state', $state ? 1 : 0);
    $resolve->bindValue('topic', $topic);
    $resolve->execute();
}

function mark_topic_confirmed(int $topic, bool $state): void {
    global $pdo;

    $confirm = $pdo->prepare('UPDATE `fmf_topics` SET `topic_confirmed` = IF(:state, NOW(), NULL) WHERE `topic_id` = :topic');
    $confirm->bindValue('state', $state ? 1 : 0);
    $confirm->bindValue('topic', $topic);
    $confirm->execute();
}