<?php
/**
 * Mobile & PWA — support notes for staff (install path, sessions, desktop vs mobile).
 * Linked from the main app sidebar and from the field mobile app.
 */
require_once __DIR__ . '/../includes/auth.php';
requireAuth();

$pageTitle = 'Mobile & PWA tips';
$currentPage = 'mobile-help';

$manifestUrl = BASE_PATH . '/mobile/manifest.php';
$swUrl = BASE_PATH . '/mobile/sw.js';
$mobileStart = BASE_PATH . '/mobile/';
$pwaHeadPath = 'mobile/pwa-head.php';
$sessionBootstrapPath = 'includes/session-bootstrap.php';

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

$mobileHelpBack = BASE_PATH . '/dashboard';
if (isFinance()) {
    $mobileHelpBack = BASE_PATH . '/finance';
} elseif (isVoting()) {
    $mobileHelpBack = BASE_PATH . '/voting';
} elseif (isGroupManager()) {
    $mobileHelpBack = BASE_PATH . '/fellowships';
} elseif (isClerk()) {
    $mobileHelpBack = BASE_PATH . '/members';
} elseif (isDataCollector()) {
    $mobileHelpBack = BASE_PATH . '/mobile';
}
?>

<div class="header">
    <h1>Mobile &amp; PWA tips</h1>
    <div class="header-actions">
        <a href="<?= htmlspecialchars($mobileHelpBack) ?>" class="link-muted">← Back</a>
        <a href="<?= BASE_PATH ?>/logout">Logout</a>
    </div>
</div>

<div class="form-card" style="max-width: 42rem;">
    <h2 class="text-muted" style="font-size: 0.875rem; text-transform: uppercase; letter-spacing: 0.04em; margin-top: 0;">Field data app (installable)</h2>
    <p>The <strong>data collection</strong> interface lives at <a href="<?= htmlspecialchars($mobileStart) ?>"><code><?= htmlspecialchars($mobileStart) ?></code></a>. Data-collector accounts are sent there after login.</p>
    <ul>
        <li><strong>Web app manifest</strong> (for “Add to Home Screen”): <a href="<?= htmlspecialchars($manifestUrl) ?>" target="_blank" rel="noopener"><code><?= htmlspecialchars($manifestUrl) ?></code></a> — generated by <code><?= htmlspecialchars($pwaHeadPath) ?></code> / <code>mobile/manifest.php</code>.</li>
        <li><strong>Service worker</strong>: <code><?= htmlspecialchars($swUrl) ?></code> — registered from <code>mobile/pwa-head.php</code> with scope <code><?= htmlspecialchars(BASE_PATH) ?>/mobile/</code>.</li>
        <li><strong>Install</strong>: open the field app URL in mobile Safari/Chrome, use the browser menu to <em>Add to Home Screen</em> / <em>Install app</em>. The installed icon opens in standalone mode (name varies; see manifest <code>short_name</code>).</li>
    </ul>

    <h2 class="text-muted" style="font-size: 0.875rem; text-transform: uppercase; letter-spacing: 0.04em; margin-top: 1.5rem;">Login &amp; sessions</h2>
    <p>Session cookies are configured in <code><?= htmlspecialchars($sessionBootstrapPath) ?></code>:</p>
    <ul>
        <li><strong>Cookie lifetime 0</strong> — treated as a <strong>browser session</strong>: fully closing the browser or sometimes the standalone PWA often clears the session and you must sign in again. Exact behaviour depends on OS and browser.</li>
        <li><strong>HttpOnly</strong> — JavaScript cannot read the session cookie.</li>
        <li><strong>SameSite=Lax</strong> — normal cross-site protections.</li>
        <li><strong>Secure</strong> — set automatically when the site is loaded over HTTPS (or common reverse-proxy headers). On plain HTTP (typical local dev), Secure may be off.</li>
    </ul>
    <p class="text-muted" style="font-size: 0.875rem;">System administrators can see live cookie flags and HTTPS detection under <strong>System → System Health</strong> → “HTTPS &amp; login sessions” (church roles cannot open <code>/system</code>).</p>

    <h2 class="text-muted" style="font-size: 0.875rem; text-transform: uppercase; letter-spacing: 0.04em; margin-top: 1.5rem;">Desktop vs phone</h2>
    <p>Some church screens are restricted on phones/tablets and may show a “desktop only” message. System maintenance, data collection, and login are allowed on mobile per <code>index.php</code> routing. If someone needs full admin on a phone, use a desktop browser or widen the allow-list deliberately in code.</p>
</div>

<?php require_once __DIR__ . '/../includes/footer.php'; ?>
