<?php
/**
 * Web App Manifest (JSON) generado desde settings.json.
 */

declare(strict_types=1);

chdir(__DIR__);

require_once __DIR__ . '/includes/grid_default_settings.php';
require_once __DIR__ . '/includes/settings_helpers.php';
require_once __DIR__ . '/includes/pwa_manifest.php';

$settingsFile = 'settings.json';
$settingsBackupFile = 'settings.json.bak';
$defaultSettings = grid_app_default_settings();
$_a = false;
$_p = false;
$settings = grid_load_settings_with_recovery($settingsFile, $settingsBackupFile, $defaultSettings, $_a, $_p);

$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://';
$host = (string) ($_SERVER['HTTP_HOST'] ?? 'localhost');
$baseUrl = $protocol . $host . rtrim(dirname($_SERVER['SCRIPT_NAME'] ?? '/'), '/') . '/';

$manifest = grid_pwa_manifest_array($settings, $baseUrl);
if (empty($settings['pwa']['enabled'])) {
    $manifest['display'] = 'browser';
}

header('Content-Type: application/manifest+json; charset=utf-8');
header('Cache-Control: public, max-age=120');
header('X-Robots-Tag: noindex');

echo json_encode($manifest, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
