<?php
session_start();

// ✅ Check if user is logged in
if (!isset($_SESSION['email'])) {
    die("Not authenticated — session not set.");
}

$email = $_SESSION['email'];

// ✅ MySQL connection
$host = "localhost";
$dbname = "gobolcha_gobolchain";
$username = "gobolcha_gobolchain";
$password = "@Jibala1997";

$conn = new mysqli($host, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Database connection failed.");
}

// ✅ Delete user from MySQL if exists
$stmt = $conn->prepare("DELETE FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->close();
$conn->close();

// ✅ Delete user's JSON file (if exists)
$jsonFile = __DIR__ . "/users/" . md5($email) . ".json";
if (file_exists($jsonFile)) {
    unlink($jsonFile);
}

// ✅ End session
session_unset();
session_destroy();

// ✅ Redirect to goodbye page
header("Location: goodbye.html");
exit;