<?php
/*******************************************************************************************************************
| Content Moderation Class
| Handles user reports and content moderation
|*******************************************************************************************************************/

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

class VModeration
{
    public static function submitReport($reporter_id, $reported_type, $reported_id, $reason, $details = null)
    {
        global $class_database;

        $sql = "INSERT INTO `db_reports` (`reporter_id`, `reported_type`, `reported_id`, `report_reason`, `report_details`)
                VALUES (%d, '%s', '%s', '%s', %s)";

        $class_database->doQuery($sql,
            $reporter_id,
            $reported_type,
            $reported_id,
            $reason,
            $details ? "'" . $class_database->safe_input($details) . "'" : 'NULL'
        );

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

    public static function getReports($status = 'pending', $limit = 50, $offset = 0)
    {
        global $class_database;

        $where = $status ? "WHERE r.status = '$status'" : "";

        $sql = "SELECT r.*, u.usr_user as reporter_name, u.usr_dname as reporter_display
                FROM `db_reports` r
                JOIN `db_accountuser` u ON r.reporter_id = u.usr_id
                {$where}
                ORDER BY r.created_at DESC
                LIMIT %d OFFSET %d";

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

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

        return $reports;
    }

    public static function updateReportStatus($report_id, $status, $reviewed_by, $resolution = null)
    {
        global $class_database;

        $resolved_at = in_array($status, ['resolved', 'dismissed']) ? date('Y-m-d H:i:s') : null;

        $sql = "UPDATE `db_reports`
                SET `status` = '%s', `reviewed_by` = %d, `resolution` = %s, `resolved_at` = %s
                WHERE `report_id` = %d";

        $class_database->doQuery($sql,
            $status,
            $reviewed_by,
            $resolution ? "'" . $class_database->safe_input($resolution) . "'" : 'NULL',
            $resolved_at ? "'$resolved_at'" : 'NULL',
            $report_id
        );

        return ['success' => true];
    }

    public static function getReportedContent($reported_type, $reported_id)
    {
        global $class_database;

        $tables = [
            'video' => 'db_videofiles',
            'short' => 'db_shortfiles',
            'user' => 'db_accountuser',
            'comment' => 'db_comments',
            'post' => 'db_community_posts'
        ];

        if (!isset($tables[$reported_type])) {
            return null;
        }

        $table = $tables[$reported_type];
        $id_field = $reported_type === 'user' ? 'usr_id' : ($reported_type === 'post' ? 'post_id' : 'file_key');

        $sql = "SELECT * FROM `{$table}` WHERE `{$id_field}` = '%s' LIMIT 1";
        $result = $class_database->doQuery($sql, $reported_id);

        return $result->fetch_assoc();
    }
}
