<?php
/**
 * GET /api/pantry/get.php?id=123
 * Owner-only. Used to prefill product-form.html when relisting a pantry
 * item to the marketplace.
 */

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

$userId = require_login();

$id = (int) ($_GET['id'] ?? 0);
if ($id <= 0) {
    send_error('A valid pantry item id is required.', 422);
}

$pdo = Database::connect();
$stmt = $pdo->prepare('SELECT id, user_id, product_name, quantity, expiry_date FROM pantry_items WHERE id = ?');
$stmt->execute([$id]);
$item = $stmt->fetch();

if (!$item) {
    send_error('Pantry item not found.', 404);
}
if ((int) $item['user_id'] !== $userId) {
    send_error('You can only access your own pantry items.', 403);
}

send_json(true, 'OK', ['item' => $item]);
