<?php
// update-cart.php
require_once __DIR__ . '/includes/config.php';
require_once __DIR__ . '/includes/functions.php';

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

$key = $_POST['key'] ?? '';
$qty = intval($_POST['qty'] ?? 0);

if ($key && isset($_SESSION['cart'][$key])) {
    if ($qty <= 0) {
        unset($_SESSION['cart'][$key]);
    } else {
        $_SESSION['cart'][$key]['quantity'] = $qty;
    }
    echo json_encode([
        'success' => true,
        'cart_count' => getCartCount(),
        'cart_total' => getCartTotal()
    ]);
} else {
    echo json_encode([
        'success' => false,
        'message' => 'Item not found'
    ]);
}
?>