<?php
session_start();

require_once '../../config/database.php';
require_once '../../includes/auth.php';

$db = (new Database())->connect();

if (!isset($_SESSION['cart'])) {
    $_SESSION['cart'] = [];
}

$itemId = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($itemId <= 0) {
    exit('Invalid item');
}

$stmt = $db->prepare("SELECT id, stock FROM items WHERE id = ?");
$stmt->execute([$itemId]);
$item = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$item) {
    exit('Item not found');
}

$currentQty = $_SESSION['cart'][$itemId] ?? 0;

if (isset($_SESSION['cart'][$itemId])) {
    $_SESSION['cart'][$itemId]++;
} else {
    $_SESSION['cart'][$itemId] = 1;
}

echo 'success';
?>