<?php
/**
 * Forgot password — data collection (request only; admin completes reset).
 */
require_once __DIR__ . '/init.php';
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/password-reset.php';

if (!empty($_SESSION['user_id']) && ($_SESSION['role'] ?? '') === 'data_collector') {
    header('Location: ' . BASE_PATH . '/mobile');
    exit;
}

$error = null;
$submitted = false;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $result = nazareth_request_password_reset($pdo, $username, 'mobile');

    if ($result['ok']) {
        $submitted = true;
    } else {
        switch ($result['code']) {
            case 'rate_limit':
                $error = 'Too many requests. Wait an hour or contact your church administrator.';
                break;
            default:
                $error = 'Enter your username.';
        }
    }
}

$helpMessage = nazareth_password_reset_help_message();

$mobileAppIcon = BASE_PATH . '/mobile/assets/img/192.png';
$mobileAppIconV = @filemtime(__DIR__ . '/assets/img/192.png');
if ($mobileAppIconV) {
    $mobileAppIcon .= '?v=' . (int) $mobileAppIconV;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
    <title>Forgot password — <?= htmlspecialchars(SITE_NAME) ?></title>
    <link rel="icon" href="<?= htmlspecialchars($mobileAppIcon, ENT_QUOTES, 'UTF-8') ?>" type="image/png" sizes="192x192">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="<?= nazareth_asset('css/style.css') ?>?v=<?= @filemtime(__DIR__ . '/../public/css/style.css') ?>">
    <link rel="stylesheet" href="<?= nazareth_mobile_asset('mobile.css') ?>?v=<?= @filemtime(__DIR__ . '/mobile.css') ?>">
</head>
<body class="mobile-collect-body mobile-collect-login-body">
    <div class="mobile-collect-login">
        <div class="mobile-collect-login-content">
            <div class="mobile-collect-brand">
                <img src="<?= nazareth_asset('img/logo.png') ?>" alt="" class="mobile-collect-logo">
                <h1 class="mobile-collect-title">Forgot password</h1>
                <p class="mobile-collect-subtitle">Data collection accounts</p>
            </div>

            <?php if ($submitted): ?>
                <div class="mobile-collect-alert mobile-collect-alert--success">
                    If an account exists for that username, your request has been recorded. <?= htmlspecialchars($helpMessage) ?>
                </div>
            <?php elseif ($error): ?>
                <div class="mobile-collect-alert"><?= htmlspecialchars($error) ?></div>
            <?php else: ?>
                <p class="mobile-collect-hint">Enter your username. An administrator will set a new temporary password for you after verifying your identity. Passwords are not reset automatically here.</p>
            <?php endif; ?>

            <?php if (!$submitted): ?>
            <form method="post" action="" class="mobile-collect-form">
                <div class="mobile-collect-field">
                    <label for="username">Username</label>
                    <div class="mobile-collect-input-wrap">
                        <input type="text" id="username" name="username" class="mobile-collect-input" required autocomplete="username"
                            value="<?= htmlspecialchars($_POST['username'] ?? '') ?>">
                    </div>
                </div>
                <button type="submit" class="mobile-collect-btn">Submit request</button>
            </form>
            <?php endif; ?>

            <p class="mobile-collect-login-link">
                <a href="<?= htmlspecialchars(BASE_PATH . '/mobile/login', ENT_QUOTES, 'UTF-8') ?>">← Back to sign in</a>
                <span class="mobile-collect-login-link-sep" aria-hidden="true"> · </span>
                <a href="<?= htmlspecialchars(nazareth_url('login'), ENT_QUOTES, 'UTF-8') ?>">Main church sign-in</a>
            </p>
        </div>

        <footer class="mobile-collect-login-footer">
            <p class="mobile-collect-login-church"><?= htmlspecialchars(SITE_FULL_NAME) ?></p>
            <p class="mobile-collect-login-system">Church Management System</p>
        </footer>
    </div>
</body>
</html>
