<?php
session_start();
header('Content-Type: application/json');
include 'Connection.php';

// Pastikan user sudah login
if (!isset($_SESSION['username'])) {
    echo json_encode(["status" => "error", "message" => "Belum login"]);
    exit;
}

$username = $_SESSION['username'];
$score = $_POST['score'] ?? 0;

if ($score <= 0) {
    echo json_encode(["status" => "error", "message" => "Skor tidak valid"]);
    exit;
}

// Simpan ke database
$stmt = $conn->prepare("INSERT INTO leaderboard (username, score) VALUES (?, ?)");
$stmt->bind_param("si", $username, $score);

if ($stmt->execute()) {
    echo json_encode(["status" => "success", "message" => "Skor berhasil disimpan"]);
} else {
    echo json_encode(["status" => "error", "message" => "Gagal menyimpan skor"]);
}

$stmt->close();
$conn->close();
?>