<?php
require_once __DIR__ . '/../config/database.php';

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

$method = $_SERVER['REQUEST_METHOD'];

if ($method === 'GET') {
    $category = $_GET['category'] ?? '';
    $endpoint = 'system_config?select=key,value,category&is_secret=eq.false';
    if ($category) {
        $endpoint .= '&category=eq.' . urlencode($category);
    }

    $res = supabase_rest('GET', $endpoint);
    $config = [];
    if (!empty($res['data'])) {
        foreach ($res['data'] as $row) {
            $config[$row['key']] = json_decode($row['value'], true) ?? $row['value'];
        }
    }
    echo json_encode(['config' => $config]);
} else {
    http_response_code(405);
    echo json_encode(['error' => 'method not allowed']);
}
