<?php
require_once __DIR__ . '/../includes/config.php';

if (!isLoggedIn()) {
    jsonResponse(['success' => false, 'error' => 'Unauthorized'], 401);
}

$product_id = (int)($_GET['product_id'] ?? 0);

if ($product_id <= 0) {
    jsonResponse(['success' => false, 'error' => 'Invalid product'], 400);
}

$query = "SELECT pc.*, u.name FROM product_comments pc 
          JOIN users u ON pc.user_id = u.id 
          WHERE pc.product_id = $product_id 
          ORDER BY pc.created_at DESC";

$result = $db->query($query);
$comments = [];

if ($result && $result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $comments[] = $row;
    }
}

jsonResponse([
    'success' => true,
    'comments' => $comments
], 200);
?>
