<?php
include '../inc/db.php';

header('Content-Type: application/json');

if (!isset($_POST['id']) || !is_numeric($_POST['id'])) {
    echo json_encode([
        'status' => 'error',
        'message' => 'Invalid product ID'
    ]);
    exit;
}

$product_id = (int)$_POST['id'];

/* 🔥 OPTIONAL BUT RECOMMENDED
   Delete child records first if not using FK cascade
*/

// delete variants
mysqli_query($conn, "DELETE FROM product_variants WHERE product_id = $product_id");

// delete addons
mysqli_query($conn, "DELETE FROM product_addons WHERE product_id = $product_id");

// delete sorting map
mysqli_query($conn, "DELETE FROM product_sorting_map WHERE product_id = $product_id");

// delete gallery images (DB only — files optional)
mysqli_query($conn, "DELETE FROM product_images WHERE product_id = $product_id");


/* ✅ MAIN DELETE (prepared) */
$stmt = mysqli_prepare($conn, "DELETE FROM products WHERE id = ?");
mysqli_stmt_bind_param($stmt, "i", $product_id);

if (mysqli_stmt_execute($stmt)) {
    echo json_encode([
        'status' => 'success',
        'message' => 'Product deleted successfully'
    ]);
} else {
    echo json_encode([
        'status' => 'error',
        'message' => 'Database error'
    ]);
}
