<?php
declare(strict_types=1);
require_once __DIR__ . '/../helpers.php';
require_post();
$adminUserId = as_int(get_post('admin_user_id'));
require_admin($adminUserId);
$fullName = get_post('full_name');
$username = get_post('username');
$password = get_post('password');
$role = get_post('role', 'cashier');
if (!validate_role($role)) respond(['success' => false, 'message' => 'Invalid role'], 400);
if ($password === '') respond(['success' => false, 'message' => 'Password is required'], 400);

$hash = password_hash($password, PASSWORD_DEFAULT);
$conn = db();
$stmt = $conn->prepare('INSERT INTO users (full_name, username, password_hash, role, created_at, updated_at) VALUES (?, ?, ?, ?, NOW(), NOW())');
$stmt->bind_param('ssss', $fullName, $username, $hash, $role);
if (!$stmt->execute()) respond(['success' => false, 'message' => 'Unable to create user: ' . $stmt->error], 400);
log_activity($adminUserId, 'CREATE_USER', "Created user {$fullName} ({$username}) role {$role}");
respond(['success' => true, 'message' => 'User saved']);
