<?php
// #33 Configuração de 2FA TOTP
$appName  = 'MonitorPing';
$email    = h($user['email']);
$secret   = $totp2faSecret; // passado pelo index.php
$otpauth  = 'otpauth://totp/' . rawurlencode($appName . ':' . $user['email'])
          . '?secret=' . $secret . '&issuer=' . rawurlencode($appName);
$qrUrl    = 'https://api.qrserver.com/v1/create-qr-code/?size=180x180&ecc=M&data=' . rawurlencode($otpauth);
?>
<?php require_once __DIR__ . '/../includes/header.php'; ?>

<div style="max-width:520px; margin:0 auto;">
    <div style="background:linear-gradient(135deg,#1e293b,#0f172a); border-radius:20px; padding:28px 32px; color:#f1f5f9; margin-bottom:24px;">
        <h1 style="font-size:1.5rem; font-weight:800; margin:0;">Autenticação em Dois Fatores (2FA)</h1>
        <p style="color:#94a3b8; margin-top:4px; font-size:0.9rem;">Proteja sua conta com um segundo fator de verificação.</p>
    </div>

    <?php if (!empty($error)): ?>
        <div style="background:rgba(239,68,68,0.1); color:#dc2626; border:1px solid #fca5a5; padding:12px 16px; border-radius:10px; margin-bottom:20px; font-weight:600;"><?= h($error) ?></div>
    <?php endif; ?>

    <div style="background:#fff; border:1px solid #e2e8f0; border-radius:16px; padding:28px; margin-bottom:20px;">
        <h3 style="margin:0 0 16px; font-size:1rem; font-weight:800; color:#0f172a;">Passo 1 — Escaneie o QR Code</h3>
        <p style="font-size:0.9rem; color:#64748b; margin-bottom:20px;">Use o <strong>Google Authenticator</strong>, <strong>Authy</strong> ou qualquer app TOTP compatível.</p>

        <div style="text-align:center; margin-bottom:20px;">
            <img src="<?= h($qrUrl) ?>" alt="QR Code 2FA" width="180" height="180"
                 style="border:6px solid #f1f5f9; border-radius:12px; display:inline-block;">
        </div>

        <div style="background:#f8fafc; border:1px solid #e2e8f0; border-radius:10px; padding:14px; text-align:center; margin-bottom:8px;">
            <p style="font-size:0.8rem; color:#64748b; margin:0 0 6px;">Ou insira manualmente a chave:</p>
            <code style="font-size:1.1rem; font-weight:700; color:#0f172a; letter-spacing:3px;"><?= h($secret) ?></code>
        </div>
        <p style="font-size:0.78rem; color:#94a3b8; text-align:center;">Essa chave só é exibida uma vez. Guarde-a em local seguro.</p>
    </div>

    <div style="background:#fff; border:1px solid #e2e8f0; border-radius:16px; padding:28px;">
        <h3 style="margin:0 0 16px; font-size:1rem; font-weight:800; color:#0f172a;">Passo 2 — Confirme o código</h3>
        <p style="font-size:0.9rem; color:#64748b; margin-bottom:20px;">Digite o código de 6 dígitos gerado pelo app para ativar o 2FA.</p>

        <form method="post" action="index.php?page=setup_2fa">
            <input type="hidden" name="csrf_token" value="<?= h(csrf_token()) ?>">
            <input type="hidden" name="action" value="verify_setup">
            <input type="hidden" name="secret" value="<?= h($secret) ?>">

            <div style="margin-bottom:20px;">
                <label style="display:block; font-size:0.85rem; font-weight:600; color:#475569; margin-bottom:6px;">Código de verificação</label>
                <input type="text" name="code" inputmode="numeric" pattern="\d{6}" maxlength="6" autocomplete="one-time-code" required autofocus
                    placeholder="000000"
                    style="width:100%; padding:14px; border:1px solid #e2e8f0; border-radius:10px; font-size:1.4rem; text-align:center; letter-spacing:8px; font-weight:700; color:#0f172a; font-family:monospace;">
            </div>
            <button type="submit"
                style="width:100%; background:#22c55e; color:#fff; border:none; padding:13px; border-radius:10px; font-weight:700; font-size:1rem; cursor:pointer;">
                Ativar 2FA
            </button>
        </form>

        <div style="margin-top:16px; text-align:center;">
            <a href="index.php?page=user_profile" style="font-size:0.85rem; color:#64748b;">Cancelar</a>
        </div>
    </div>
</div>

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