<?php

include_once __DIR__ . '/../secrets.php';

include '../db_connect.php';

$id = $_POST['id'];
$key = $_POST['key'];

$security_key = decrypData($key);

if ($security_key === false) {
    echo 'Invalid encryption data';
    exit;
}

if ($security_key == SECURITY_KEY_PLAIN) {

    $con = dbConnect();

    $getSql = "SELECT image1, image2, image3, image4 FROM post_table WHERE id = '$id'";
    $getResult = mysqli_query($con, $getSql);

    if ($getResult && mysqli_num_rows($getResult) > 0) {
        $row = mysqli_fetch_assoc($getResult);

        $images = [$row['image1'], $row['image2'], $row['image3'], $row['image4']];
        foreach ($images as $imgPath) {
            if ($imgPath !== "0" && file_exists("../" . $imgPath)) {
                unlink("../" . $imgPath); // ফাইল ডিলিট
            }
        }
    }

    $sql = "DELETE FROM post_table WHERE id = '$id'";
    $result = mysqli_query($con, $sql);

    $sql1 = "DELETE FROM post_likes WHERE post_id = '$id'";
    $result1 = mysqli_query($con, $sql1);

    if ($result && $result1) {
        echo "Delete Successfully";
    } else {
        echo "Query Error";
    }

    mysqli_close($con);
}


?>
