connect_error) { throw new Exception("Database connection failed: " . $conn->connect_error); } // Get POST data $data = json_decode(file_get_contents('php://input'), true); if (!$data) { throw new Exception("Invalid JSON data received"); } $ticketId = $data['ticket_id']; // Initialize models $commentModel = new CommentModel($conn); $auditLog = new AuditLogModel($conn); // Add comment with user tracking $result = $commentModel->addComment($ticketId, $data, $userId); // Log comment creation to audit log if ($result['success'] && isset($result['comment_id'])) { $auditLog->logCommentCreate($userId, $result['comment_id'], $ticketId); } // Discard any unexpected output ob_end_clean(); // Return JSON response header('Content-Type: application/json'); echo json_encode($result); } catch (Exception $e) { // Discard any unexpected output ob_end_clean(); // Return error response header('Content-Type: application/json'); echo json_encode([ 'success' => false, 'error' => $e->getMessage() ]); }