<?php
session_start();
header("Content-Type: application/json");

require_once "config/db.php";

$data = json_decode(file_get_contents("php://input"), true);

if (!isset($data['score'])) {
    echo json_encode(["error" => "score missing"]);
    exit;
}

$score = (int)$data['score'];
$user_id = isset($_SESSION['id']) ? (int)$_SESSION['id'] : null;

if (!$user_id) {
    echo json_encode(["error" => "no session user"]);
    exit;
}

$sql = "UPDATE users SET score = score + ? where id =? ";
$stmt = $db ->prepare($sql);

if ($stmt){
    $stmt->bind_param("ii",$score,$user_id);
    if($stmt->execute()){
        echo json_encode(["success" => true, "message" => "Score accumulated"]);
    }else{
        echo json_encode(["error" => "update failed: " . $stmt->error]);
    }
    $stmt->close();
}else{
    echo json_encode(["error" => "stetment prep failed"]);
}

$db->close();
?>