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

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

if (empty($guestId) || $pointsToAdd <= 0) {
    echo json_encode(['status' => 'error', 'message' => 'Niepoprawne dane']);
    exit;
}

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

    // Fetch new total
    $stmt = $pdo->prepare("SELECT points FROM guests WHERE id = ?");
    $stmt->execute([$guestId]);
    $newPoints = $stmt->fetchColumn();

    echo json_encode([
        'status' => 'success',
        'message' => 'Punkty dodane!',
        'new_total' => (int) $newPoints
    ]);
} catch (Exception $e) {
    echo json_encode(['status' => 'error', 'message' => 'Błąd podczas dodawania punktów']);
}
?>