<?php

require_once __DIR__ . '/../lib/init.php';
Auth::requireLogin();
Auth::requireRole('admin', 'tech');

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    header('Location: requests.php');
    exit;
}

$id      = (int)($_POST['request_id'] ?? 0);
$request = ReservationRequest::getById($id);

if (!$request) {
    $_SESSION['flash'] = ['type' => 'danger', 'message' => 'Demande introuvable.'];
    header('Location: requests.php');
    exit;
}

ReservationRequest::delete($id);
AuditLog::log('requests.refuse', 'reservation_request', $id,
    'Demande de ' . $request['requester_name'] . ' (' . $request['requester_email'] . ') — MAC: ' . $request['mac_address']);

$_SESSION['flash'] = ['type' => 'success', 'message' => 'Demande de ' . $request['requester_name'] . ' refusée et supprimée.'];
header('Location: requests.php');
exit;
