<?php
// check_puzzle_status.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);
$puzzleId = (int) ($input['puzzle_id'] ?? 0);

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

try {
    $stmt = $pdo->prepare("SELECT id FROM solved_puzzles WHERE guest_id = ? AND puzzle_id = ?");
    $stmt->execute([$guestId, $puzzleId]);
    if ($stmt->fetch()) {
        echo json_encode(['status' => 'solved']);
    } else {
        echo json_encode(['status' => 'available']);
    }
} catch (Exception $e) {
    echo json_encode(['status' => 'error', 'message' => 'System error']);
}
?>