<?php
session_start();

require_once '../../config/database.php';
require_once '../../includes/auth.php';

$db = (new Database())->connect();

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($id <= 0) {
    die('Invalid item ID.');
}

/*
    Get item image
*/
$stmt = $db->prepare("
    SELECT image_path
    FROM items
    WHERE id = ?
");

$stmt->execute([$id]);
$item = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$item) {
    die('Item not found.');
}

/*
    Delete image file
*/
if (!empty($item['image_path'])) {
    $imageFile = '../../' . $item['image_path'];

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

/*
    Delete item
*/
$stmt = $db->prepare("
    DELETE FROM items
    WHERE id = ?
");

$stmt->execute([$id]);

header('Location: index.php');
exit;
?>