<?php
/**
 * 2FA Setup Interstitial - Login Flow
 *
 * Displayed when 2FA enforcement is active, grace period has expired,
 * and the user has no 2FA method configured. Forces user to set up
 * at least one 2FA method before completing login.
 *
 * This template is loaded via Guard_Dog_View_Loader::render_partial() which uses
 * extract() to inject variables from the calling context.
 *
 * @package GuardDog
 * @since 1.9.1
 *
 * @var int                       $user_id           The ID of the user.
 * @var string                    $username          The username.
 * @var array                     $available_methods Available 2FA methods.
 * @var string                    $redirect_to       Where to redirect after setup.
 * @var string                    $transient_key     The transient key for this setup session.
 * @var Guard_Dog_Two_Factor_Auth $guard_dog_this    The Two Factor Auth instance.
 */

if (!defined('ABSPATH')) exit; // Exit if accessed directly

// Check for error message from previous attempt (passed via state token)
$error_message = isset($setup_error) ? sanitize_text_field($setup_error) : '';
?>

<div id="guard-dog-2fa-setup-interstitial" class="guard-dog-2fa-setup-container">
    <div class="guard-dog-2fa-setup-header">
        <div class="guard-dog-2fa-setup-icon">
            <span class="dashicons dashicons-shield-alt"></span>
        </div>
        <h2><?php esc_html_e('Two-Factor Authentication Required', 'guard-dog'); ?></h2>
        <p class="description">
            <?php esc_html_e('Your administrator requires all users to set up two-factor authentication. Please choose and configure at least one method below to continue.', 'guard-dog'); ?>
        </p>
    </div>

    <?php if (!empty($error_message)): ?>
    <div class="guard-dog-2fa-setup-error">
        <p><strong><?php esc_html_e('Error:', 'guard-dog'); ?></strong> <?php echo esc_html($error_message); ?></p>
    </div>
    <?php endif; ?>

    <div class="guard-dog-2fa-setup-methods" id="guard-dog-method-list">
        <?php foreach ($available_methods as $method_key => $method): ?>
        <div class="guard-dog-2fa-method-card" data-method="<?php echo esc_attr($method_key); ?>">
            <div class="method-icon">
                <span class="dashicons <?php echo esc_attr($method['icon']); ?>"></span>
            </div>
            <div class="method-content">
                <h3><?php echo esc_html($method['name']); ?></h3>
                <p class="method-description"><?php echo esc_html($method['description']); ?></p>
            </div>
            <button type="button" class="button button-primary guard-dog-setup-method-btn" data-method="<?php echo esc_attr($method_key); ?>">
                <?php esc_html_e('Set Up', 'guard-dog'); ?>
            </button>
        </div>
        <?php endforeach; ?>
    </div>

    <!-- TOTP Setup Section (hidden initially) -->
    <?php if (isset($available_methods['totp'])): ?>
    <div id="guard-dog-totp-setup-section" class="guard-dog-setup-section" style="display: none;">
        <div class="guard-dog-setup-section-header">
            <button type="button" class="guard-dog-back-btn" data-section="totp">
                <span class="dashicons dashicons-arrow-left-alt2"></span>
                <?php esc_html_e('Back', 'guard-dog'); ?>
            </button>
            <h3><?php esc_html_e('Set Up Authenticator App', 'guard-dog'); ?></h3>
        </div>

        <div class="guard-dog-totp-instructions">
            <ol>
                <li><?php esc_html_e('Install an authenticator app (Google Authenticator, Authy, Microsoft Authenticator, or similar)', 'guard-dog'); ?></li>
                <li><?php esc_html_e('Scan the QR code below or enter the secret key manually', 'guard-dog'); ?></li>
                <li><?php esc_html_e('Enter the 6-digit code from your app to complete setup', 'guard-dog'); ?></li>
            </ol>
        </div>

        <div class="guard-dog-totp-qr-container">
            <div id="guard-dog-totp-qr-code" class="guard-dog-qr-code">
                <div class="guard-dog-qr-loading">
                    <span class="spinner is-active"></span>
                    <?php esc_html_e('Loading QR code...', 'guard-dog'); ?>
                </div>
            </div>
            <div class="guard-dog-totp-secret">
                <label><?php esc_html_e('Manual Entry Code:', 'guard-dog'); ?></label>
                <code id="guard-dog-totp-secret-key"></code>
                <button type="button" class="button button-secondary guard-dog-copy-btn" id="guard-dog-copy-totp-secret">
                    <?php esc_html_e('Copy', 'guard-dog'); ?>
                </button>
            </div>
        </div>

        <form method="post" id="guard-dog-totp-verify-form" class="guard-dog-verify-form">
            <input type="hidden" name="guard_dog_2fa_setup_action" value="complete_totp" />
            <input type="hidden" name="user_id" value="<?php echo esc_attr($user_id); ?>" />
            <input type="hidden" name="redirect_to" value="<?php echo esc_attr($redirect_to); ?>" />
            <?php wp_nonce_field('guard_dog_2fa_setup_' . $user_id, 'guard_dog_2fa_setup_nonce'); ?>

            <div class="guard-dog-verify-input-group">
                <label for="guard_dog_2fa_setup_code"><?php esc_html_e('Verification Code:', 'guard-dog'); ?></label>
                <input type="text" name="guard_dog_2fa_setup_code" id="guard-dog-totp-verify-code"
                       class="input" maxlength="6" autocomplete="off"
                       placeholder="<?php esc_attr_e('Enter 6-digit code', 'guard-dog'); ?>" />
            </div>

            <div class="guard-dog-setup-actions">
                <button type="submit" class="button button-primary button-large">
                    <?php esc_html_e('Verify & Complete Setup', 'guard-dog'); ?>
                </button>
            </div>
        </form>
    </div>
    <?php endif; ?>

    <!-- Email 2FA Setup Section (hidden initially) -->
    <?php if (isset($available_methods['email'])): ?>
    <div id="guard-dog-email-setup-section" class="guard-dog-setup-section" style="display: none;">
        <div class="guard-dog-setup-section-header">
            <button type="button" class="guard-dog-back-btn" data-section="email">
                <span class="dashicons dashicons-arrow-left-alt2"></span>
                <?php esc_html_e('Back', 'guard-dog'); ?>
            </button>
            <h3><?php esc_html_e('Set Up Email Verification', 'guard-dog'); ?></h3>
        </div>

        <div class="guard-dog-email-instructions">
            <p><?php esc_html_e('We will send a verification code to your email address. Enter the code below to complete setup.', 'guard-dog'); ?></p>
        </div>

        <div class="guard-dog-email-send-section">
            <button type="button" id="guard-dog-send-email-code-btn" class="button button-secondary button-large">
                <span class="dashicons dashicons-email-alt"></span>
                <?php esc_html_e('Send Verification Code', 'guard-dog'); ?>
            </button>
            <span id="guard-dog-email-status" class="guard-dog-email-status"></span>
        </div>

        <form method="post" id="guard-dog-email-verify-form" class="guard-dog-verify-form" style="display: none;">
            <input type="hidden" name="guard_dog_2fa_setup_action" value="complete_email" />
            <input type="hidden" name="user_id" value="<?php echo esc_attr($user_id); ?>" />
            <input type="hidden" name="redirect_to" value="<?php echo esc_attr($redirect_to); ?>" />
            <?php wp_nonce_field('guard_dog_2fa_setup_' . $user_id, 'guard_dog_2fa_setup_nonce'); ?>

            <div class="guard-dog-verify-input-group">
                <label for="guard-dog-email-verify-code"><?php esc_html_e('Verification Code:', 'guard-dog'); ?></label>
                <input type="text" name="guard_dog_2fa_setup_code" id="guard-dog-email-verify-code"
                       class="input" maxlength="6" autocomplete="off"
                       placeholder="<?php esc_attr_e('Enter 6-digit code', 'guard-dog'); ?>" />
            </div>

            <div class="guard-dog-setup-actions">
                <button type="submit" class="button button-primary button-large">
                    <?php esc_html_e('Verify & Complete Setup', 'guard-dog'); ?>
                </button>
                <button type="button" id="guard-dog-resend-email-code" class="button button-secondary">
                    <?php esc_html_e('Resend Code', 'guard-dog'); ?>
                </button>
            </div>
        </form>
    </div>
    <?php endif; ?>

    <!-- Passkey Setup Section (hidden initially) -->
    <?php if (isset($available_methods['passkey'])): ?>
    <div id="guard-dog-passkey-setup-section" class="guard-dog-setup-section" style="display: none;">
        <div class="guard-dog-setup-section-header">
            <button type="button" class="guard-dog-back-btn" data-section="passkey">
                <span class="dashicons dashicons-arrow-left-alt2"></span>
                <?php esc_html_e('Back', 'guard-dog'); ?>
            </button>
            <h3><?php esc_html_e('Set Up Passkey', 'guard-dog'); ?></h3>
        </div>

        <div class="guard-dog-passkey-instructions">
            <p><?php esc_html_e('Passkeys use biometrics (Face ID, Touch ID, fingerprint) or a security key to securely authenticate you without a password.', 'guard-dog'); ?></p>
            <ol>
                <li><?php esc_html_e('Click the button below to begin registration', 'guard-dog'); ?></li>
                <li><?php esc_html_e('Follow your browser or device prompts', 'guard-dog'); ?></li>
                <li><?php esc_html_e('Use biometrics or your security key to confirm', 'guard-dog'); ?></li>
            </ol>
        </div>

        <div class="guard-dog-passkey-register-section">
            <button type="button" id="guard-dog-register-passkey-btn" class="button button-primary button-large">
                <span class="dashicons dashicons-admin-network"></span>
                <?php esc_html_e('Register Passkey', 'guard-dog'); ?>
            </button>
            <span id="guard-dog-passkey-status" class="guard-dog-passkey-status"></span>
        </div>

        <div class="guard-dog-passkey-name-section" style="display: none;">
            <label for="guard-dog-passkey-name"><?php esc_html_e('Passkey Name (optional):', 'guard-dog'); ?></label>
            <input type="text" id="guard-dog-passkey-name" class="input"
                   placeholder="<?php esc_attr_e('e.g., MacBook Pro, iPhone', 'guard-dog'); ?>" />
        </div>
    </div>
    <?php endif; ?>

    <!-- Hidden fields for JavaScript -->
    <input type="hidden" id="guard-dog-setup-user-id" value="<?php echo esc_attr($user_id); ?>" />
    <input type="hidden" id="guard-dog-setup-username" value="<?php echo esc_attr($username); ?>" />
    <input type="hidden" id="guard-dog-setup-redirect" value="<?php echo esc_attr($redirect_to); ?>" />
</div>

<p id="backtoblog">
    <a href="<?php echo esc_url(home_url('/')); ?>">
        &larr; <?php printf(esc_html__('Go to %s', 'guard-dog'), esc_html(get_bloginfo('title', 'display'))); ?>
    </a>
</p>
