<?php
require_once __DIR__ . '/../auth_middleware.php';
require_once '/home/akgbilis/fi_sf/config.php';

header('Content-Type: application/json');

$userId = $decoded->sub;
$data = json_decode(file_get_contents("php://input"), true);
$reward = $data["reward"] ?? 0;
$now = date('Y-m-d H:i:s');

// 🔹 Kullanıcıya krediyi ekle ve tam zamanı kaydet
$stmt = $pdo->prepare("UPDATE users SET credits = credits + ?, last_chest_open_time = ? WHERE id = ?");
$success = $stmt->execute([$reward, $now, $userId]);

echo json_encode([
    "success" => $success,
    "reward" => $reward
]);