<?php
/**
 * Device detection - block mobile and tablet access
 */

/**
 * Check if the current request is from a mobile or tablet device
 * @return bool
 */
function isMobileOrTablet() {
    $userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';
    if (empty($userAgent)) {
        return false;
    }
    
    // Common mobile/tablet user agent patterns
    $mobilePatterns = [
        '/Mobile/i',
        '/Android/i',
        '/iPhone/i',
        '/iPad/i',
        '/iPod/i',
        '/BlackBerry/i',
        '/Windows Phone/i',
        '/Opera Mini/i',
        '/IEMobile/i',
        '/webOS/i',
        '/Kindle/i',
        '/Silk/i',
        '/Tablet/i',
    ];
    
    foreach ($mobilePatterns as $pattern) {
        if (preg_match($pattern, $userAgent)) {
            return true;
        }
    }
    
    return false;
}

/**
 * Whether this route may load on a phone/tablet.
 */
function nazareth_allow_mobile_page(string $page, ?string $role = null): bool
{
    // Main church sign-in (and home → login redirect)
    if (in_array($page, ['login', 'forgot-password', 'index'], true)) {
        return true;
    }

    // Data collection app + sign-out
    if (in_array($page, ['mobile', 'logout'], true)) {
        return true;
    }

    // After sign-in, data collectors only (other roles: use desktop)
    if ($role === 'data_collector' && in_array($page, ['collect', 'households', 'mobile-help', 'change-password'], true)) {
        return true;
    }

    return false;
}

/**
 * Show desktop-only message page
 */
function showDesktopOnlyMessage() {
    http_response_code(403);
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Desktop Only - <?= htmlspecialchars(SITE_NAME) ?></title>
    <?php if (defined('BASE_PATH')): ?><link rel="icon" href="<?= BASE_PATH ?>/public/img/logo.png" type="image/png"><?php endif; ?>
    <link rel="preconnect" href="https://fonts.googleapis.com">
        <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400&display=swap" rel="stylesheet">
        <style>
            * { box-sizing: border-box; margin: 0; padding: 0; }
            body {
                font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
                background: #f7fafc;
                color: #1e293b;
                display: flex;
                align-items: center;
                justify-content: center;
                min-height: 100vh;
                padding: 1.5rem;
            }
            .message-card {
                background: white;
                border-radius: 12px;
                box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
                padding: 3rem 2rem;
                max-width: 500px;
                text-align: center;
            }
            .icon {
                width: 64px;
                height: 64px;
                margin: 0 auto 1.5rem;
                color: #64748b;
            }
            h1 {
                font-size: 1.5rem;
                font-weight: 600;
                margin-bottom: 0.75rem;
                color: #1e293b;
            }
            p {
                font-size: 1rem;
                color: #64748b;
                line-height: 1.6;
                margin-bottom: 1rem;
            }
            .note {
                font-size: 0.875rem;
                color: #94a3b8;
                margin-top: 1.5rem;
                padding-top: 1.5rem;
                border-top: 1px solid #e2e8f0;
            }
        </style>
    </head>
    <body>
        <div class="message-card">
            <svg class="icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9.75 17L9 20l-1 1h8l-1-1-.75-3M3 13h18M5 17h14a2 2 0 002-2V5a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"/>
            </svg>
            <h1>Desktop Access Required</h1>
            <p>The church management dashboard is for desktop or laptop computers.</p>
            <?php
            $loginHref = function_exists('nazareth_url') ? nazareth_url('login') : ((defined('BASE_PATH') ? BASE_PATH : '') . '/login');
            $mobileLoginHref = function_exists('nazareth_url') ? nazareth_url('mobile/login') : ((defined('BASE_PATH') ? BASE_PATH : '') . '/mobile/login');
            ?>
            <p>You can <a href="<?= htmlspecialchars($loginHref, ENT_QUOTES, 'UTF-8') ?>" style="color:#1e3a5f;font-weight:600;">sign in here</a> on your phone. Data collection staff should use the <a href="<?= htmlspecialchars($mobileLoginHref, ENT_QUOTES, 'UTF-8') ?>" style="color:#1e3a5f;font-weight:600;">mobile data collection sign-in</a>.</p>
            <div class="note">
                <strong><?= htmlspecialchars(SITE_FULL_NAME) ?></strong><br>
                Church Management System
            </div>
        </div>
    </body>
    </html>
    <?php
    exit;
}
