<?php
// verify_riddle.php
header('Content-Type: application/json');
require 'db.php';

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['status' => 'error', 'message' => 'Invalid request method']);
    exit;
}

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

if (empty($guestId) || empty($answer)) {
    echo json_encode(['status' => 'error', 'message' => 'Missing data']);
    exit;
}

try {
    $stmt = $pdo->prepare("SELECT riddle_answer FROM guests WHERE id = ?");
    $stmt->execute([$guestId]);
    $guest = $stmt->fetch();

    if ($guest && strtoupper($guest['riddle_answer']) === strtoupper($answer)) {
        echo json_encode(['status' => 'success', 'message' => 'Logic Verified']);
    } else {
        echo json_encode(['status' => 'error', 'message' => 'INCORRECT ANSWER - ACCESS DENIED']);
    }
} catch (Exception $e) {
    echo json_encode(['status' => 'error', 'message' => 'System error']);
}
?>