<?php
/**
 * HuSNS - 举报模型
 * 
 * @author  HYR
 * @QQ      281900864
 * @website https://huyourui.com
 * @license MIT
 */
class ReportModel extends Model
{
    protected $table = 'reports';
    
    /**
     * 获取举报列表
     * @param int $page 页码
     * @param int $pageSize 每页数量
     * @param int $status 状态筛选
     * @return array
     */
    public function getReports($page = 1, $pageSize = 20, $status = -1)
    {
        $offset = ($page - 1) * $pageSize;
        
        $where = [];
        $params = [];
        
        if ($status >= 0) {
            $where[] = 'r.status = ?';
            $params[] = $status;
        }
        
        $whereStr = $where ? 'WHERE ' . implode(' AND ', $where) : '';
        
        $sql = "SELECT r.*, u.username as reporter_name, u.avatar as reporter_avatar,
                       pu.username as post_user_name, pu.avatar as post_user_avatar,
                       p.content as post_content
                FROM __PREFIX__reports r
                INNER JOIN __PREFIX__users u ON r.user_id = u.id
                LEFT JOIN __PREFIX__posts p ON r.post_id = p.id
                LEFT JOIN __PREFIX__users pu ON p.user_id = pu.id
                {$whereStr}
                ORDER BY r.status ASC, r.created_at DESC
                LIMIT ?, ?";
        
        $params[] = $offset;
        $params[] = $pageSize;
        
        $reports = $this->db->fetchAll($sql, $params);
        
        foreach ($reports as &$report) {
            $report['created_at_text'] = date('Y-m-d H:i', $report['created_at']);
            $report['handled_at_text'] = $report['handled_at'] ? date('Y-m-d H:i', $report['handled_at']) : '-';
            $report['post_content'] = mb_substr(strip_tags($report['post_content'] ?? ''), 0, 50);
        }
        
        $total = $this->countReports($status);
        $totalPages = ceil($total / $pageSize);
        
        return [
            'items' => $reports,
            'total' => $total,
            'page' => $page,
            'pageSize' => $pageSize,
            'totalPages' => $totalPages
        ];
    }
    
    /**
     * 统计举报数量
     * @param int $status 状态
     * @return int
     */
    public function countReports($status = -1)
    {
        $where = [];
        $params = [];
        
        if ($status >= 0) {
            $where[] = 'status = ?';
            $params[] = $status;
        }
        
        $whereStr = $where ? 'WHERE ' . implode(' AND ', $where) : '';
        
        $sql = "SELECT COUNT(*) as count FROM __PREFIX__reports r {$whereStr}";
        $result = $this->db->fetch($sql, $params);
        
        return (int)($result['count'] ?? 0);
    }
    
    /**
     * 获取举报详情
     * @param int $id 举报ID
     * @return array|null
     */
    public function getReport($id)
    {
        $sql = "SELECT r.*, u.username as reporter_name, u.avatar as reporter_avatar,
                       pu.username as post_user_name, pu.avatar as post_user_avatar,
                       p.content as post_content, p.images as post_images
                FROM __PREFIX__reports r
                INNER JOIN __PREFIX__users u ON r.user_id = u.id
                LEFT JOIN __PREFIX__posts p ON r.post_id = p.id
                LEFT JOIN __PREFIX__users pu ON p.user_id = pu.id
                WHERE r.id = ?";
        
        $report = $this->db->fetch($sql, [$id]);
        
        if ($report) {
            $report['created_at_text'] = date('Y-m-d H:i', $report['created_at']);
            $report['handled_at_text'] = $report['handled_at'] ? date('Y-m-d H:i', $report['handled_at']) : '-';
        }
        
        return $report;
    }
    
    /**
     * 创建举报
     * @param array $data 举报数据
     * @return int|bool
     */
    public function createReport($data)
    {
        return $this->insert([
            'user_id' => $data['user_id'],
            'post_id' => $data['post_id'],
            'reason' => $data['reason'],
            'detail' => $data['detail'] ?? '',
            'status' => 0,
            'created_at' => time()
        ]);
    }
    
    /**
     * 处理举报
     * @param int $id 举报ID
     * @param int $status 处理状态
     * @param int $handlerId 处理人ID
     * @param string $result 处理结果
     * @return bool
     */
    public function handleReport($id, $status, $handlerId, $result = '')
    {
        return $this->update($id, [
            'status' => $status,
            'handled_by' => $handlerId,
            'handled_at' => time(),
            'handle_result' => $result
        ]);
    }
    
    /**
     * 获取举报原因选项
     * @return array
     */
    public function getReasonOptions()
    {
        return [
            'spam' => '垃圾信息',
            'inappropriate' => '不良内容',
            'hate' => '仇恨言论',
            'advertisement' => '广告推广',
            'copyright' => '版权侵权',
            'other' => '其他'
        ];
    }
    
    /**
     * 获取状态标签
     * @param int $status 状态
     * @return string
     */
    public function getStatusLabel($status)
    {
        switch ($status) {
            case 0:
                return '<span class="badge badge-warning">待处理</span>';
            case 1:
                return '<span class="badge badge-success">已处理</span>';
            case 2:
                return '<span class="badge badge-secondary">已驳回</span>';
            default:
                return '<span class="badge badge-default">未知</span>';
        }
    }
}
?>