<?php

header('Content-Type: application/json');
require_once __DIR__ . '/../includes/cors.php';

require_once __DIR__ . '/../includes/supabase.php';

$db = supabaseService();

$flags = $db->get('feature_flags', ['select' => 'key,value,is_enabled']);
$plugins = $db->get('game_plugins', ['select' => '*', 'is_enabled' => 'eq.true']);

$response = [
    'features' => [],
    'games' => [],
    'version' => '1.0.0'
];

if (is_array($flags) && !isset($flags['error'])) {
    foreach ($flags as $f) {
        if ($f['is_enabled'] ?? false) {
            $response['features'][$f['key']] = $f['value'] ?? true;
        }
    }
}

if (is_array($plugins) && !isset($plugins['error'])) {
    $response['games'] = $plugins;
}

echo json_encode($response, JSON_UNESCAPED_UNICODE);
