<?php
require_once '../element/connection.php';


header('Content-Type: application/json');

if (!isset($_SESSION['user_id'])) {
    echo json_encode(['success' => false, 'error' => 'Not logged in']);
    exit();
}

$conn = getConnection();
$ticket_id = isset($_POST['ticket_id']) ? intval($_POST['ticket_id']) : 0;
$user_id = intval($_SESSION['user_id']);

if ($ticket_id) {
    $query = "UPDATE support_messages m 
              JOIN support_tickets t ON m.ticket_id = t.id 
              SET m.is_read = 1, m.read_at = NOW() 
              WHERE t.id = '$ticket_id' 
              AND t.user_id = '$user_id' 
              AND m.sender_type = 'admin' 
              AND m.is_read = 0";
    mysqli_query($conn, $query);
}

echo json_encode(['success' => true]);
exit();
?>