<?php
require_once __DIR__ . '/../includes/auth.php';
requireLogin();
requirePermission('users.manage');

require_once __DIR__ . '/../config/database.php';

$userId = (int)$_GET['id'];

$newPassword = bin2hex(random_bytes(4)); // 8 char temp password
$hash = password_hash($newPassword, PASSWORD_DEFAULT);

$update = $pdo->prepare("UPDATE users SET password_hash = ? WHERE id = ?");
$update->execute([$hash, $userId]);

/* Audit */
$audit = $pdo->prepare("
    INSERT INTO audit_logs (user_id, action)
    VALUES (?, ?)
");
$audit->execute([
    $_SESSION['user_id'],
    "Reset password for user ID {$userId}"
]);

echo "<h2>Temporary Password: {$newPassword}</h2>";
echo "<p>Give this to the user and advise immediate password change.</p>";
