<?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();

    $sql = "SELECT image FROM story_table WHERE id = '$id'";
    $result = mysqli_query($con, $sql);

    if (mysqli_num_rows($result) > 0) {

        $row = mysqli_fetch_assoc($result);
        $imageFile = $row['image'];

        $filePath = "../images/" . $imageFile;


        if (file_exists($filePath)) {
            unlink($filePath);
        }

        $deleteQuery = "DELETE FROM story_table WHERE id = '$id'";
        $deleteResult = mysqli_query($con, $deleteQuery);

        if ($deleteResult) {
            echo "Successfully";
        } else {
            echo "Query Error";
        }

    } else {
        echo "Story Not Found";
    }
}

?>
