<?php
require_once '../includes/init.php';

if (!isAuthenticated()) {
    jsonResponse(false, 'Not authenticated');
}

$data = json_decode(file_get_contents('php://input'), true);
$userId = intval($data['user_id'] ?? 0);
$typing = filter_var($data['typing'] ?? false, FILTER_VALIDATE_BOOLEAN);

if (!$userId) {
    jsonResponse(false, 'Invalid user');
}

$db = getDB();
$currentUserId = $_SESSION['user_id'];

$stmt = $db->prepare("
    INSERT INTO typing_status (user_id, chat_with_id, is_typing) 
    VALUES (?, ?, ?)
    ON DUPLICATE KEY UPDATE is_typing = ?, updated_at = NOW()
");
$stmt->execute([$currentUserId, $userId, $typing ? 1 : 0, $typing ? 1 : 0]);

jsonResponse(true, 'Typing status updated');
?>
