<?php
// delete_product.php - Delete Product
// File location: /bidwheels.net/admin/delete_product.php

require_once '../includes/auth.php';
require_once '../includes/functions.php';

requireAdmin();

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

if ($product_id <= 0) {
    setError('Invalid product ID.');
    redirect('/admin/products.php');
}

// Get product images to delete files
$images = fetchAll("SELECT image_path FROM product_images WHERE product_id = $product_id");

// Delete image files
$upload_dir = $_SERVER['DOCUMENT_ROOT'] . '/bidwheels/uploads/products/';
foreach ($images as $image) {
    $filepath = $upload_dir . $image['image_path'];
    if (file_exists($filepath)) {
        unlink($filepath);
    }
}

// Delete product (cascades to bids, comments, images)
delete('products', "id = $product_id");

setSuccess('Product deleted successfully.');
redirect('/admin/products.php');
?>