{% extends "base.html" %}

{% block title %}2FA Einrichten{% endblock %}

{% block extra_css %}
<style>
    .setup-card {
        transition: all 0.3s ease;
        border: none;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    .setup-card:hover {
        box-shadow: 0 6px 12px rgba(0,0,0,0.15);
    }
    .step-number {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 35px;
        height: 35px;
        background-color: #0d6efd;
        color: white;
        border-radius: 50%;
        font-weight: bold;
        margin-right: 10px;
    }
    .app-icon {
        width: 40px;
        height: 40px;
        object-fit: contain;
        margin-right: 10px;
    }
    .qr-container {
        background: white;
        padding: 20px;
        border-radius: 10px;
        display: inline-block;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    .secret-code {
        font-family: monospace;
        font-size: 1.2rem;
        letter-spacing: 2px;
        background-color: #f8f9fa;
        padding: 10px 15px;
        border-radius: 5px;
        word-break: break-all;
    }
    .code-input {
        font-size: 2rem;
        letter-spacing: 0.5rem;
        text-align: center;
        font-family: monospace;
        font-weight: bold;
    }
</style>
{% endblock %}

{% block content %}
<div class="container py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1>🔐 2FA einrichten</h1>
        <a href="{{ url_for('auth.profile') }}" class="btn btn-secondary">← Zurück zum Profil</a>
    </div>
            
    {% with messages = get_flashed_messages(with_categories=true) %}
        {% if messages %}
            {% for category, message in messages %}
                <div class="alert alert-{{ 'danger' if category == 'error' else category }} alert-dismissible fade show" role="alert">
                    {{ message }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            {% endfor %}
        {% endif %}
    {% endwith %}
    
    <!-- Step 1: Install App -->
    <div class="card setup-card mb-4">
        <div class="card-body">
            <h5 class="card-title">
                <span class="step-number">1</span>
                Authenticator-App installieren
            </h5>
            <p class="ms-5">Wählen Sie eine der folgenden Apps für Ihr Smartphone:</p>
            <div class="row ms-4">
                <div class="col-md-4 mb-2">
                    <div class="d-flex align-items-center">
                        <span style="font-size: 2rem; margin-right: 10px;">📱</span>
                        <div>
                            <strong>Google Authenticator</strong><br>
                            <small class="text-muted">Android / iOS</small>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 mb-2">
                    <div class="d-flex align-items-center">
                        <span style="font-size: 2rem; margin-right: 10px;">🔷</span>
                        <div>
                            <strong>Microsoft Authenticator</strong><br>
                            <small class="text-muted">Android / iOS</small>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 mb-2">
                    <div class="d-flex align-items-center">
                        <span style="font-size: 2rem; margin-right: 10px;">🔴</span>
                        <div>
                            <strong>Authy</strong><br>
                            <small class="text-muted">Android / iOS / Desktop</small>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
                    
    <!-- Step 2: Scan QR Code -->
    <div class="card setup-card mb-4">
        <div class="card-body">
            <h5 class="card-title">
                <span class="step-number">2</span>
                QR-Code scannen oder Code eingeben
            </h5>
            <div class="row mt-4">
                <div class="col-md-6 text-center mb-4">
                    <p class="fw-bold">Option A: QR-Code scannen</p>
                    <div class="qr-container">
                        <img src="data:image/png;base64,{{ qr_code }}" alt="2FA QR Code" style="max-width: 250px;">
                    </div>
                    <p class="text-muted mt-2">
                        <small>Öffnen Sie Ihre Authenticator-App und scannen Sie diesen Code</small>
                    </p>
                </div>
                <div class="col-md-6 mb-4">
                    <p class="fw-bold">Option B: Code manuell eingeben</p>
                    <div class="mb-3">
                        <label class="text-muted small">Account-Name:</label>
                        <div class="alert alert-light py-2">
                            <strong>V2 Admin Panel</strong>
                        </div>
                    </div>
                    <div class="mb-3">
                        <label class="text-muted small">Geheimer Schlüssel:</label>
                        <div class="secret-code">{{ totp_secret }}</div>
                        <button type="button" class="btn btn-sm btn-outline-primary mt-2" onclick="copySecret()">
                            📋 Schlüssel kopieren
                        </button>
                    </div>
                    <div class="alert alert-warning">
                        <small>
                            <strong>⚠️ Wichtiger Hinweis:</strong><br>
                            Speichern Sie diesen Code sicher. Er ist Ihre einzige Möglichkeit,
                            2FA auf einem neuen Gerät einzurichten.
                        </small>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Step 3: Verify -->
    <div class="card setup-card mb-4">
        <div class="card-body">
            <h5 class="card-title">
                <span class="step-number">3</span>
                Code verifizieren
            </h5>
            <p class="ms-5">Geben Sie den 6-stelligen Code aus Ihrer Authenticator-App ein:</p>
            
            <form method="POST" action="{{ url_for('auth.enable_2fa') }}" class="ms-5 me-5">
                <div class="row align-items-center">
                    <div class="col-md-6 mb-3">
                        <input type="text" 
                               class="form-control code-input" 
                               id="token" 
                               name="token" 
                               placeholder="000000"
                               maxlength="6"
                               pattern="[0-9]{6}"
                               autocomplete="off"
                               autofocus
                               required>
                        <div class="form-text text-center">Der Code ändert sich alle 30 Sekunden</div>
                    </div>
                    <div class="col-md-6 mb-3">
                        <button type="submit" class="btn btn-success btn-lg w-100">
                            ✅ 2FA aktivieren
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
function copySecret() {
    const secret = '{{ totp_secret }}';
    navigator.clipboard.writeText(secret).then(function() {
        alert('Code wurde in die Zwischenablage kopiert!');
    });
}

// Auto-format the code input
document.getElementById('token').addEventListener('input', function(e) {
    // Remove non-digits
    e.target.value = e.target.value.replace(/\D/g, '');
});
</script>
{% endblock %}