<?php
// app/models/Comment.php

class Comment {
    private $pdo;

    public function __construct($pdo) {
        $this->pdo = $pdo;
    }

    // Get all comments for a specific task
    public function getByTask($taskId) {
        $sql = "SELECT comments.*, users.name as user_name, users.role 
                FROM comments 
                JOIN users ON comments.user_id = users.id 
                WHERE task_id = :tid 
                ORDER BY created_at ASC"; // Oldest first (Chat style)
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute(['tid' => $taskId]);
        return $stmt->fetchAll();
    }

    // Post a new comment
    public function create($taskId, $userId, $text) {
        $sql = "INSERT INTO comments (task_id, user_id, comment) VALUES (:tid, :uid, :txt)";
        $stmt = $this->pdo->prepare($sql);
        return $stmt->execute([
            'tid' => $taskId,
            'uid' => $userId,
            'txt' => $text
        ]);
    }
}
?>