<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

// إظهار الأخطاء للتصحيح
error_reporting(E_ALL);
ini_set('display_errors', 1);

try {
    include_once '../config/database.php';

    if (!$conn) {
        die(json_encode(["error" => "فشل الاتصال بقاعدة البيانات"]));
    }

    $query = "SELECT * FROM games ORDER BY id DESC";
    $stmt = $conn->prepare($query);
    $stmt->execute();

    $rides = array();

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        $rides[] = array(
            "id" => $row['id'],
            "name" => $row['name'],
            "description" => $row['description'],
            "image" => "https://festivalland2.elnadaonline.com/uploads/" . $row['image_url'],
            "capacity" => $row['capacity'] ?? 1
        );
    }

    echo json_encode($rides);

} catch (Exception $e) {
    echo json_encode(["error" => $e->getMessage()]);
}
?>
