<?php
/**
 * POST /api/admin/categories_create.php
 * Body (JSON): { name, parent_id? }
 */

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

only_post_allowed();
require_login();
require_role(['admin']);

$input = get_json_input();
$name = trim($input['name'] ?? '');
$parentId = !empty($input['parent_id']) ? (int) $input['parent_id'] : null;

if ($name === '') {
    send_error('Category name is required.', 422);
}

$pdo = Database::connect();
$stmt = $pdo->prepare('INSERT INTO categories (name, parent_id, is_active) VALUES (?, ?, 1)');
$stmt->execute([$name, $parentId]);

send_json(true, 'Category created.', ['id' => (int) $pdo->lastInsertId()], 201);
