<?php
session_start();
include "../../connect/db.php";

if (isset($_POST['user'])) {
    $chap = $_POST['user'];

    $sql = mysqli_query($con, "DELETE FROM users WHERE email = '$chap'");

    if ($sql) {
        // Best-effort cleanup of uploads directory
        $uploadDir = '../../uploads/' . $chap;
        if (is_dir($uploadDir)) {
            $files = array_diff(scandir($uploadDir), array('.', '..'));
            foreach ($files as $file) {
                $path = "$uploadDir/$file";
                is_dir($path) ? rmdir($path) : unlink($path);
            }
            rmdir($uploadDir);
        }
        echo "success";
    } else {
        echo "Error deleting user.";
    }
}

