<?php
session_start();
require_once '../config/db.php';

if (isset($_GET['id'])) {
    $id = intval($_GET['id']);

    // 1. Fetch file path before deleting record
    $query = "SELECT file_location FROM effects WHERE id = $id";
    $res = $conn->query($query);

    if ($res && $res->num_rows > 0) {
        $row = $res->fetch_assoc();
        $file_path = "../" . $row['file_location'];

        // 2. Delete the physical PNG/Overlay file
        if (!empty($row['file_location']) && file_exists($file_path)) {
            unlink($file_path);
        }

        // 3. Delete the database entry
        $conn->query("DELETE FROM effects WHERE id = $id");

        $_SESSION['msg'] = "Effect and graphic file deleted successfully.";
        $_SESSION['msg_type'] = "success";
    } else {
        $_SESSION['msg'] = "Effect not found.";
        $_SESSION['msg_type'] = "error";
    }
}

header("Location: ../manage-effects.php");
exit();