<?php
session_start();
include '../config/database.php';

// Proteksi Admin
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') { 
    header("Location: ../auth/login_page.php"); 
    exit; 
}

if (isset($_GET['id'])) {
    $id = mysqli_real_escape_string($conn, $_GET['id']);

    // 1. Hapus riwayat chat user terlebih dahulu agar tidak ada error foreign key (opsional tergantung struktur DB)
    mysqli_query($conn, "DELETE FROM chat_history WHERE user_id = '$id'");

    // 2. Hapus data user
    $query = "DELETE FROM users WHERE id = '$id'";
    
    if (mysqli_query($conn, $query)) {
        header("Location: index.php?msg=deleted");
    } else {
        echo "Gagal menghapus: " . mysqli_error($conn);
    }
} else {
    header("Location: index.php");
}