<?php
require_once '../../config/config.php';
require_once '../../config/database.php';
require_once '../../core/helpers.php';
requireRole('admin');

$id = (int)($_GET['id'] ?? 0);

if ($id === (int)$_SESSION['user_id']) {
    setFlash('error','Anda tidak dapat menghapus akun sendiri.');
    redirect(BASE_URL.'/modules/users/index.php');
}

$user = Database::fetchOne("SELECT * FROM users WHERE id=?", [$id]);
if (!$user) { setFlash('error','User tidak ditemukan.'); redirect(BASE_URL.'/modules/users/index.php'); }

Database::execute("DELETE FROM users WHERE id=?", [$id]);
auditLog('DELETE','users',$id,'Hapus user: '.$user['username']);
setFlash('success','User '.$user['nama'].' berhasil dihapus.');
redirect(BASE_URL.'/modules/users/index.php');
