<?php

/**
 * Kelas untuk fungsi admin SecureAuth Authenticator 2FA
 */
class SecureAuth_Authenticator_2FA_Admin {

    private $plugin_name;
    private $version;

    /**
     * Konstruktor
     */
    public function __construct($plugin_name, $version) {
        $this->plugin_name = $plugin_name;
        $this->version = $version;

        // Hook penyimpanan profil user
        add_action('personal_options_update', array($this, 'save_2fa_user_profile_fields'));
        add_action('edit_user_profile_update', array($this, 'save_2fa_user_profile_fields'));
    }

    /**
     * Tambahkan menu admin (jika ada halaman pengaturan)
     */
    public function add_plugin_admin_menu() {
        add_options_page('SecureAuth Authenticator 2FA', 'SecureAuth 2FA', 'manage_options', $this->plugin_name, array($this, 'display_plugin_admin_page'));
    }

    /**
     * Tampilkan halaman admin (jika perlu)
     */
    public function display_plugin_admin_page() {
        include_once SA2FA_PLUGIN_DIR . 'admin/partials/secureauth-authenticator-2fa-admin-display.php';
    }

    /**
     * Tambahkan field 2FA di profil pengguna
     */
    public function add_2fa_user_profile_fields($user) {
        $enabled = get_user_meta($user->ID, 'secureauth_authenticator_enabled', true);
        $secret = get_user_meta($user->ID, 'secureauth_authenticator_secret', true);

        $authenticator = new SecureAuth_Authenticator();

        if (empty($secret)) {
            $secret = $authenticator->createSecret();
            update_user_meta($user->ID, 'secureauth_authenticator_secret', $secret);
        }

        $qrCodeUrl = $authenticator->getQRCodeGoogleUrl($user->user_login, $secret);
        ?>
        <h3>SecureAuth Authenticator 2FA</h3>
        <table class="form-table">
            <tr>
                <th><label for="secureauth_authenticator_enabled">Aktifkan 2FA</label></th>
                <td>
                    <input type="checkbox" name="secureauth_authenticator_enabled" id="secureauth_authenticator_enabled" value="yes" <?php checked($enabled, 'yes'); ?> />
                    <span class="description">Aktifkan Two-Factor Authentication menggunakan SecureAuth Authenticator</span>
                </td>
            </tr>
            <tr>
                <th><label for="secureauth_authenticator_secret">Secret Key</label></th>
                <td>
                    <input type="text" name="secureauth_authenticator_secret" id="secureauth_authenticator_secret" value="<?php echo esc_attr($secret); ?>" class="regular-text" readonly />
                    <p class="description">Masukkan kunci ini ke aplikasi authenticator Anda</p>
                </td>
            </tr>
            <tr>
                <th><label>QR Code</label></th>
                <td>
                    <img src="<?php echo esc_url($qrCodeUrl); ?>" />
                    <p class="description">Scan QR code ini dengan aplikasi authenticator Anda</p>
                </td>
            </tr>
        </table>
        <?php wp_nonce_field('secureauth_2fa_save', 'secureauth_2fa_nonce'); ?>
        <?php
    }

    /**
     * Simpan data 2FA dari profil pengguna
     */
    public function save_2fa_user_profile_fields($user_id) {
        if (!current_user_can('edit_user', $user_id)) {
            return false;
        }
    
        // ✅ Verifikasi nonce
        if (
            !isset($_POST['secureauth_2fa_nonce']) ||
            !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['secureauth_2fa_nonce'])), 'secureauth_2fa_save')
        ) {
            return false;
        }
    
        // ✅ Sanitize & update data
        $enabled = (isset($_POST['secureauth_authenticator_enabled']) &&
                    sanitize_text_field(wp_unslash($_POST['secureauth_authenticator_enabled'])) === 'yes') ? 'yes' : 'no';
    
        update_user_meta($user_id, 'secureauth_authenticator_enabled', $enabled);
    
        if (isset($_POST['secureauth_authenticator_secret'])) {
            $secret = sanitize_text_field(wp_unslash($_POST['secureauth_authenticator_secret']));
            update_user_meta($user_id, 'secureauth_authenticator_secret', $secret);
        }
    }
    
}
