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

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

$id = intval($_POST['id'] ?? 0);

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

/* 🔒 OPTIONAL: prevent delete if options exist */
$check = mysqli_query($conn,
    "SELECT id FROM sorting_options WHERE sorting_group_id=$id LIMIT 1"
);

if (mysqli_num_rows($check) > 0) {
    echo json_encode([
        'status'=>'error',
        'message'=>'Cannot delete: options exist under this group'
    ]);
    exit;
}

/* ✅ DELETE */
mysqli_query($conn, "DELETE FROM sorting_groups WHERE id=$id");

echo json_encode([
    'status'=>'success',
    'message'=>'Sorting group deleted'
]);
