<?php
/*******************************************************************************************************************
| Community Posts Management Class
| Handles YouTube Community-style posts from creators
|*******************************************************************************************************************/

defined('_ISVALID') or header('Location: /error');

class VCommunity
{
    public static function createPost($usr_id, $content, $post_type = 'text', $media_file_key = null, $poll_id = null)
    {
        global $class_database;

        $sql = "INSERT INTO `db_community_posts` (`usr_id`, `post_type`, `content`, `media_file_key`, `poll_id`)
                VALUES (%d, '%s', '%s', %s, %s)";

        $class_database->doQuery($sql,
            $usr_id,
            $post_type,
            $class_database->safe_input($content),
            $media_file_key ? "'$media_file_key'" : 'NULL',
            $poll_id ? $poll_id : 'NULL'
        );

        return ['success' => true, 'post_id' => $class_database->insert_id()];
    }

    public static function getPosts($usr_id = null, $limit = 20, $offset = 0)
    {
        global $class_database;

        $where = $usr_id ? "WHERE p.usr_id = $usr_id AND p.deleted = 0" : "WHERE p.deleted = 0";

        $sql = "SELECT p.*, u.usr_user, u.usr_dname, u.usr_key
                FROM `db_community_posts` p
                JOIN `db_accountuser` u ON p.usr_id = u.usr_id
                {$where}
                ORDER BY p.created_at DESC
                LIMIT %d OFFSET %d";

        $result = $class_database->doQuery($sql, $limit, $offset);

        $posts = [];
        while ($row = $result->fetch_assoc()) {
            $posts[] = $row;
        }

        return $posts;
    }

    public static function likePost($post_id, $usr_id)
    {
        global $class_database;

        $sql = "INSERT INTO `db_community_post_likes` (`post_id`, `usr_id`) VALUES (%d, %d)
                ON DUPLICATE KEY UPDATE `post_id` = `post_id`";

        $class_database->doQuery($sql, $post_id, $usr_id);

        // Update like count
        $sql = "UPDATE `db_community_posts` SET `likes` = (SELECT COUNT(*) FROM `db_community_post_likes` WHERE `post_id` = %d)
                WHERE `post_id` = %d";

        $class_database->doQuery($sql, $post_id, $post_id);

        return ['success' => true];
    }

    public static function addComment($post_id, $usr_id, $comment)
    {
        global $class_database;

        $sql = "INSERT INTO `db_community_post_comments` (`post_id`, `usr_id`, `comment`)
                VALUES (%d, %d, '%s')";

        $class_database->doQuery($sql, $post_id, $usr_id, $class_database->safe_input($comment));

        // Update comment count
        $sql = "UPDATE `db_community_posts` SET `comments` = (SELECT COUNT(*) FROM `db_community_post_comments` WHERE `post_id` = %d AND `deleted` = 0)
                WHERE `post_id` = %d";

        $class_database->doQuery($sql, $post_id, $post_id);

        return ['success' => true, 'comment_id' => $class_database->insert_id()];
    }

    public static function deletePost($post_id, $usr_id)
    {
        global $class_database;

        $sql = "UPDATE `db_community_posts` SET `deleted` = 1 WHERE `post_id` = %d AND `usr_id` = %d";
        $class_database->doQuery($sql, $post_id, $usr_id);

        return ['success' => true];
    }
}
