<?php
require_once __DIR__ . '/../includes/config.php';

if (!isLoggedIn()) {
    redirectWithMessage(APP_URL . '/login.php', 'Unauthorized', 'error');
}

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    redirectWithMessage(APP_URL . '/admin/discounts.php', 'Invalid request', 'error');
}

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

if ($id <= 0) {
    redirectWithMessage(APP_URL . '/admin/discounts.php', 'Invalid discount ID', 'error');
}

// Get discount name before deleting
$discount_result = $db->query("SELECT name FROM discounts WHERE id = $id");
$discount_name = $discount_result ? $discount_result->fetch_assoc()['name'] : "Discount #$id";

$db->query("DELETE FROM discounts WHERE id = $id");

auditLog('delete', 'discounts', $id, $discount_name);
redirectWithMessage(APP_URL . '/admin/discounts.php', 'Reducere ștearsă cu succes', 'success');
?>
