<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
if (!isset($_SESSION['admin'])) {
    header('Location: login.php');
    exit();
}

include 'db.php';
if (!$conn) {
    die("Database connection failed: " . mysqli_connect_error());
}

// Validate user ID
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    $_SESSION['error_message'] = "Invalid user ID.";
    header("Location: profile_management.php");
    exit();
}

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

// Check if user exists
$stmt = $conn->prepare("SELECT UserID FROM users WHERE UserID = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
if ($stmt->get_result()->num_rows == 0) {
    $_SESSION['error_message'] = "User not found.";
    header("Location: profile_management.php");
    exit();
}
$stmt->close();

// Delete user
$stmt = $conn->prepare("DELETE FROM users WHERE UserID = ?");
$stmt->bind_param("i", $user_id);
if ($stmt->execute()) {
    $_SESSION['success_message'] = "Profile deleted successfully!";
} else {
    $_SESSION['error_message'] = "Error deleting profile: " . $stmt->error;
}
$stmt->close();

header("Location: profile_management.php");
exit;
?>