<?php
// POST /api/stock/create_item.php
require_once __DIR__ . '/../config/db.php';
require_once __DIR__ . '/../config/auth.php';

$user = requireRole([1]);
$db   = getDB();

$sku  = trim(post('sku', ''));
$name = trim(post('name', ''));
if (!$sku)  apiError('SKU required.', 422);
if (!$name) apiError('Name required.', 422);

$db->prepare("
    INSERT INTO stock_items (sku, name, description, category_id, item_type, unit, unit_cost, min_qty, max_qty, is_active)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 1)
")->execute([
    $sku, $name, post('description'), post('category_id') ?: null,
    post('item_type', 'consumable'), post('unit', 'each'),
    post('unit_cost') ?: null, post('min_qty', 0), post('max_qty') ?: null,
]);

apiSuccess(['id' => (int)$db->lastInsertId()], 'Stock item created.', 201);
