<?php

/**
 * HappyFenceNG Total Enterprise Security Plugin
 * HAPPYWPRESS is a RazorVue Trademark.
 *
 * @package     HappyFenceNG
 * @developer   Simonluca Scillitani
 * @copyright   2025 RazorVue di Scillitani Simonluca
 * @link        https://razorvue.it
 * @author      RazorVue di Scillitani Simonluca
 * @brand       HappyWPress
 * @uri         https://happywpress.com
 * @set all
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**PHPCS -> code compliance: we put useful comments where needed.. */
class HappyFenceNGVRMFA extends HappyFenceNGAVRoute {
    /**PHPCS -> code compliance: we put useful comments where needed.. */
    public function doRoute() {
        try {
            $nonce_save_settings = wp_create_nonce( 'happyfenceng_MFA_save_settings' );
            $nonce_ch_user = wp_create_nonce( 'happyfenceng_MFA_chuser' );
            $burn = HappyFenceNGFailFast::setup();
            $pages = get_pages();
            $pages_array = array();
            foreach ( $pages as $page ) {
                $pages_array[] = array(
                    'post_name'  => $page->post_name,
                    'post_title' => $page->post_title,
                );
            }
            $params = array(
                'texts'  => array(
                    'title'                                  => esc_html__( 'MFA Settings', 'happyfenceng-total-enterprise-security' ),
                    'warning_MFA'                            => esc_html__( '⚠️ HappyFenceNG uses its own MFA system, designed to be integrated with custom frontend pages.', 'happyfenceng-total-enterprise-security' ),
                    'warning_MFA_line_1'                     => esc_html__( 'Before enabling this feature, make sure to disable any other MFA plugin to avoid conflicts.', 'happyfenceng-total-enterprise-security' ),
                    'warning_MFA_line_2'                     => esc_html__( 'If you want create a custom frontend page, put in the custom page the stortcode [happyfenceng_manage_mfa] and then  select the "after mfa page" in setting as the redirect target after MFA success.', 'happyfenceng-total-enterprise-security' ),
                    'enable_mfa_title'                       => esc_html__( 'Enable MFA', 'happyfenceng-total-enterprise-security' ),
                    'enable_mfa_description'                 => esc_html__( 'Enable multifactor authentication for all users', 'happyfenceng-total-enterprise-security' ),
                    'enable_mfa_tooltip'                     => esc_html__( 'Check this option if you want to let all users to enable MFA for their logins.', 'happyfenceng-total-enterprise-security' ),
                    'default_wordpress'                      => esc_html__( 'Default WordPress behavior', 'happyfenceng-total-enterprise-security' ),
                    'redirect_mfa_title'                     => esc_html__( 'Redirect after MFA success', 'happyfenceng-total-enterprise-security' ),
                    'redirect_mfa_tooltip'                   => esc_html__( 'With this option you can control where the user lands after a successfult MFA authentication.', 'happyfenceng-total-enterprise-security' ),
                    'admin_options_title'                    => esc_html__( 'MFA Options for Administrators', 'happyfenceng-total-enterprise-security' ),
                    'require_admin_activate_mfa_description' => esc_html__( 'Require all admins to activate MFA', 'happyfenceng-total-enterprise-security' ),
                    'require_admin_activate_mfa_tooltip'     => esc_html__( 'Force all admins to activate MFA before login.', 'happyfenceng-total-enterprise-security' ),
                    'disable_mfa_for_admins_description'     => esc_html__( 'Disable MFA for admin', 'happyfenceng-total-enterprise-security' ),
                    'disable_mfa_for_admins_tooltip'         => esc_html__( 'Admins can login without MFA', 'happyfenceng-total-enterprise-security' ),
                    'mfa_user_management_title'              => esc_html__( 'MFA Users Management', 'happyfenceng-total-enterprise-security' ),
                    'confirm_change'                         => esc_html__( 'Disable MFA for this user?', 'happyfenceng-total-enterprise-security' ),
                    'show_management_table'                  => esc_html__( 'Show MFA User Management Table', 'happyfenceng-total-enterprise-security' ),
                    'feature_requires'                       => esc_html__( 'THIS FEATURE REQUIRES PRO OR BETTER', 'happyfenceng-total-enterprise-security' ),
                    'save_settings'                          => esc_html__( 'Save Settings', 'happyfenceng-total-enterprise-security' ),
                    'settings_saved'                         => esc_html__( 'Settings Saved', 'happyfenceng-total-enterprise-security' ),
                    'error_saving_settings'                  => esc_html__( 'Error saving settings', 'happyfenceng-total-enterprise-security' ),
                ),
                'links'  => array(
                    'happyfenceng_logo_url' => esc_url( HAPPYFENCENG_PLUGIN_URL . 'assets/images/happyfenceng-small.png' ),
                ),
                'values' => array(
                    'mfa_enabled'       => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->MFA->mfa_enabled, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT ),
                    'redirect_page'     => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->MFA->redirect_page, HappyFenceNGPlaceholder::EMPTY_STRING ),
                    'mfa_force_admin'   => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->MFA->mfa_force_admin, HappyFenceNGPlaceholder::CHECKBOX_OFF ),
                    'mfa_disable_admin' => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->MFA->mfa_disable_admin, HappyFenceNGPlaceholder::CHECKBOX_OFF ),
                    'users_table'       => $this->prepare_table_items(),
                    'all_pages'         => $pages_array,
                    'checkbox_on'       => HappyFenceNGPlaceholder::CHECKBOX_ON,
                    'checkbox_off'      => HappyFenceNGPlaceholder::CHECKBOX_OFF,
                    'nonce'             => $nonce_save_settings,
                    'nonceChUser'       => $nonce_ch_user,
                    'burnNonce'         => $burn,
                    'feature_enabled'   => function_exists( 'htes_fs' ) && HappyFenceNGTR::is_in( static::class, array(HappyFenceNGTR::PREAMBLE) ),
                ),
            );
            ob_start();
            HappyFenceNGVControl::renderView( $params, 'mfa' );
            // phpcs:ignore WordPress.Security.EscapeOutput
            echo ob_get_clean();
        } catch ( Throwable $e ) {
            HappyFenceNGAlog::get_instance()->log( 'HappyFenceNGVRMFA - cannot render UI ' . $e->getMessage(), HappyFenceNGSyslogFacility::UI, HappyFenceNGSyslogSeverity::ERROR );
            ob_end_clean();
            HappyFenceNGCleaner::cleaner_oops();
        }
    }

    /**PHPCS -> code compliance: we put useful comments where needed.. */
    private function prepare_table_items() {
        $data = HappyFenceNGPlaceholder::EMPTY_ARRAY;
        try {
            return $data;
        } catch ( Throwable $e ) {
            HappyFenceNGLOGCORE::logcore( 'HappyFenceNGVRMFA UserTable error: ' . $e->getMessage(), HappyFenceNGSyslogFacility::DB, HappyFenceNGSyslogSeverity::ERROR );
            return $data;
        }
    }

}
