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

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

    // 1. Fetch the image path BEFORE deleting the record
    $query = "SELECT banner_image FROM banners WHERE id = $id";
    $res = $conn->query($query);

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

        // 2. Delete the physical file from the server
        if (!empty($row['banner_image']) && file_exists($image_path)) {
            unlink($image_path);
        }

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

        $_SESSION['msg'] = "Banner and image file removed successfully.";
        $_SESSION['msg_type'] = "success";
    } else {
        $_SESSION['msg'] = "Banner not found.";
        $_SESSION['msg_type'] = "error";
    }
}

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