<?php
header('Content-Type: application/json; charset=UTF-8');
$root = rtrim((string)($_SERVER['DOCUMENT_ROOT'] ?? ''), '/\\');
if ($root === '' || !file_exists($root . '/config.php')) {
    http_response_code(500);
    echo json_encode(['success' => false, 'message' => 'Tenant configuration could not be loaded.']);
    exit;
}
require_once $root . '/config.php';
$templateUrl = rtrim(TENANT_URL, '/') . '/images/lev_templates';
$templateDir = $root . '/images/lev_templates/';
$templates = [];
foreach (glob($templateDir . '*.json') ?: [] as $path) {
    if (is_file($path)) $templates[] = basename($path);
}
natcasesort($templates);
echo json_encode(['success' => true, 'templates' => array_values($templates), 'templateUrl' => $templateUrl]);
