<?php
/**
 * API - Marcar conversación como NO LEÍDA (set is_read = 0)
 */
require_once '../config/config.php';

header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Content-Type');

try {
    $input = json_decode(file_get_contents('php://input'), true);
    $userId = isset($input['user_id']) ? intval($input['user_id']) : 0;
    if (!$userId) {
        http_response_code(400);
        echo json_encode(['success' => false, 'error' => 'user_id is required']);
        exit;
    }

    $db = Database::getInstance();
    // Marcar como no leídos todos los mensajes entrantes del usuario
    $db->execute("UPDATE conversations SET is_read = 0 WHERE user_id = ? AND direction = 'incoming'", [$userId]);

    echo json_encode(['success' => true]);
} catch (Exception $e) {
    error_log('mark_conversation_unread failed: ' . $e->getMessage());
    http_response_code(500);
    echo json_encode(['success' => false, 'error' => 'Internal server error']);
}
?>