<?php
/**
 * Web App Manifest for the mobile field app (installable PWA).
 * Served as JSON; no database required.
 */
declare(strict_types=1);

require_once __DIR__ . '/../config/app.php';

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

$base = BASE_PATH;
$scope = $base . '/mobile/';
$start = $base . '/mobile/';

$icon192Path = __DIR__ . '/assets/img/192.png';
$icon512Path = __DIR__ . '/assets/img/512.png';
$v192 = (int) (@filemtime($icon192Path) ?: 0);
$v512 = (int) (@filemtime($icon512Path) ?: 0);
$icon192 = $base . '/mobile/assets/img/192.png' . ($v192 ? '?v=' . $v192 : '');
$icon512 = $base . '/mobile/assets/img/512.png' . ($v512 ? '?v=' . $v512 : '');

$manifest = [
    'name' => SITE_FULL_NAME . ' — Field data',
    'short_name' => SITE_NAME . ' Field',
    'description' => 'Sign in to capture and review member records in the field.',
    'id' => $start,
    'start_url' => $start,
    'scope' => $scope,
    'display' => 'standalone',
    'background_color' => '#0b1b89',
    'theme_color' => '#0b1b89',
    'orientation' => 'portrait-primary',
    'icons' => [
        [
            'src' => $icon192,
            'sizes' => '192x192',
            'type' => 'image/png',
            'purpose' => 'any',
        ],
        [
            'src' => $icon512,
            'sizes' => '512x512',
            'type' => 'image/png',
            'purpose' => 'any',
        ],
    ],
];

$json = json_encode($manifest, JSON_UNESCAPED_SLASHES);
if ($json === false) {
    http_response_code(500);
    header('Content-Type: text/plain; charset=utf-8');
    echo 'Manifest error';
    exit;
}
echo $json;
