<?php
// penalize_trap.php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Pragma: no-cache");
header('Content-Type: application/json');
require 'db.php';

$input = json_decode(file_get_contents('php://input'), true);
$guestId = (int) ($input['guest_id'] ?? 0);

if ($guestId <= 0) {
    echo json_encode(['status' => 'error', 'message' => 'Invalid biometric identity']);
    exit;
}

try {
    // Kara -5 punktów za wpadnięcie w pułapkę QR
    $penalty = 5;

    $stmt = $pdo->prepare("UPDATE guests SET points = points - ? WHERE id = ?");
    $stmt->execute([$penalty, $guestId]);

    echo json_encode([
        'status' => 'success',
        'message' => 'Trap triggered. Points deducted.'
    ]);

} catch (Exception $e) {
    echo json_encode(['status' => 'error', 'message' => 'System error during penalty application']);
}
?>