<?php

namespace App\Models;

use App\Core\Model;

class ForumPost extends Model
{
    protected $table = 'forum_posts';
    protected $primaryKey = 'id';

    public function getByTopic($topicId)
    {
        $stmt = $this->db->prepare("SELECT p.*, u.name as user_name, u.avatar as user_avatar, u.created_at as user_since, u.role as user_role FROM {$this->table} p LEFT JOIN users u ON p.user_id = u.id WHERE p.topic_id = ? AND p.status = 'active' ORDER BY p.created_at ASC");
        $stmt->execute([$topicId]);
        return $stmt->fetchAll(\PDO::FETCH_OBJ);
    }

    public function getCountByTopic($topicId)
    {
        $stmt = $this->db->prepare("SELECT COUNT(*) as count FROM {$this->table} WHERE topic_id = ? AND status = 'active'");
        $stmt->execute([$topicId]);
        return $stmt->fetch(\PDO::FETCH_OBJ)->count;
    }

    public function softDelete($id)
    {
        $stmt = $this->db->prepare("UPDATE {$this->table} SET status = 'deleted' WHERE id = ?");
        return $stmt->execute([$id]);
    }

    public function getWithMedia($id)
    {
        $stmt = $this->db->prepare("SELECT p.*, u.name as user_name FROM {$this->table} p LEFT JOIN users u ON p.user_id = u.id WHERE p.id = ?");
        $stmt->execute([$id]);
        return $stmt->fetch(\PDO::FETCH_OBJ);
    }
}
