<?php
include '../auth.php';
include '../../koneksi.php';

// Pastikan ID tersedia
if (isset($_GET['id'])) {
    $id_user = mysqli_real_escape_string($conn, $_GET['id']);

    // Ambil slug_url dari landingpage berdasarkan id_user
    $slugQuery = "SELECT slug_url FROM landingpage WHERE id_user = '$id_user'";
    $slugResult = mysqli_query($conn, $slugQuery);

    if ($slugResult && mysqli_num_rows($slugResult) > 0) {
        while ($row = mysqli_fetch_assoc($slugResult)) {
            $slug_url = $row['slug_url'];
            $folderPath = "../../page/$slug_url";

            // Hapus folder jika ada
            if (is_dir($folderPath)) {
                deleteFolder($folderPath);
            }
        }
    }
}

function deleteFolder($folder) {
    if (!is_dir($folder)) return;
    
    $files = array_diff(scandir($folder), ['.', '..']);
    foreach ($files as $file) {
        $filePath = "$folder/$file";
        is_dir($filePath) ? deleteFolder($filePath) : unlink($filePath);
    }
    rmdir($folder);
}

// delete gambar produk
$imageQuery = "SELECT gambar_produk FROM detail_produk WHERE id_produk IN (SELECT id_produk FROM produk WHERE id_user = '$id_user')";
$imageResult = mysqli_query($conn, $imageQuery);

if ($imageResult && mysqli_num_rows($imageResult) > 0) {
    while ($imgRow = mysqli_fetch_assoc($imageResult)) {
        $imagePath = "../../uploads/produk/" . $imgRow['gambar_produk'];
        if (file_exists($imagePath) && is_file($imagePath)) {
            unlink($imagePath);
        }
    }
}

//delete gambar produk
$assetsQuery = "SELECT gambar_utama, gambar_tentang, gambar_keunggulan FROM landingpage WHERE id_user = '$id_user'";
$assetsResult = mysqli_query($conn, $assetsQuery);

if ($assetsResult && mysqli_num_rows($assetsResult) > 0) {
    while ($row = mysqli_fetch_assoc($assetsResult)) {
        $assetsPaths = [
            "../../uploads/assets/" . $row['gambar_utama'],
            "../../uploads/assets/" . $row['gambar_tentang'],
            "../../uploads/assets/" . $row['gambar_keunggulan']
        ];

        // Hapus file jika ada
        foreach ($assetsPaths as $path) {
            if (file_exists($path) && is_file($path)) {
                unlink($path);
            }
        }
    }
}

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

    // Hapus data terkait berdasarkan id_user
    $deleteQueries = [
        "DELETE FROM detail_produk WHERE id_produk IN (SELECT id_produk FROM produk WHERE id_user = '$id_user')",
        "DELETE FROM produk WHERE id_user = '$id_user'",
        
        "DELETE FROM detail_testimoni WHERE id_testimoni IN (SELECT id_testimoni FROM testimoni WHERE id_user = '$id_user')",
        "DELETE FROM testimoni WHERE id_user = '$id_user'",
        
        "DELETE FROM detail_keunggulan WHERE id_keunggulan IN (SELECT id_keunggulan FROM keunggulan WHERE id_user = '$id_user')",
        "DELETE FROM keunggulan WHERE id_user = '$id_user'",
        
        "DELETE FROM seo WHERE id_user = '$id_user'",
        "DELETE FROM datausaha WHERE id_user = '$id_user'",
        "DELETE FROM landingpage WHERE id_user = '$id_user'",
        
        "DELETE FROM users WHERE id_user = '$id_user'"
    ];

    $allDeleted = true;
    
    // Eksekusi semua query
    foreach ($deleteQueries as $query) {
        if (!mysqli_query($conn, $query)) {
            $allDeleted = false;
            $_SESSION['error'] = "Gagal menghapus data: " . mysqli_error($conn);
            break;
        }
    }

    if ($allDeleted) {
        $_SESSION['success'] = "Pengguna dan data terkait berhasil dihapus!";
    }

    // Redirect kembali ke halaman pengguna
    header("Location: ../pengguna/");
    exit;
} else {
    $_SESSION['error'] = "ID pengguna tidak ditemukan!";
    header("Location: ../pengguna/");
    exit;
}
?>
