<?php
session_start();
header('Content-Type: application/json');
include('../../includes/config.php');

if (!isset($_SESSION['user_id'])) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit;
}

$store_id = isset($_GET['store_id']) ? (int)$_GET['store_id'] : 0;
$product_id = isset($_GET['product_id']) ? (int)$_GET['product_id'] : 0;

if ($store_id <= 0 || $product_id <= 0) {
    echo json_encode(['success' => false, 'message' => 'Invalid parameters']);
    exit;
}

$stmt = $conn->prepare("SELECT quantity FROM inventory WHERE store_id = ? AND product_id = ?");
$stmt->bind_param("ii", $store_id, $product_id);
$stmt->execute();
$result = $stmt->get_result()->fetch_assoc();
$stmt->close();

$quantity = $result ? (int)$result['quantity'] : 0;

echo json_encode(['success' => true, 'quantity' => $quantity]);
