<?php
header('Content-Type: application/json');
require_once __DIR__ . '/auth_helper.php';

if (!is_logged_in()) {
    echo json_encode(['success' => false, 'error' => 'Not authenticated']);
    exit;
}

try {
    $data = json_decode(file_get_contents('php://input'), true);
    $shoppingList = $data['shopping_list'] ?? null;

    $pdo = db();
    $stmt = $pdo->prepare("UPDATE users SET shopping_list = ? WHERE id = ?");
    $stmt->execute([json_encode($shoppingList), get_logged_in_user_id()]);

    echo json_encode(['success' => true]);
} catch (PDOException $e) {
    echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}
