<?php

namespace WPUltimateSecurity\UltimateSecurity\Presentation\PublicSite;

use WPUltimateSecurity\UltimateSecurity\Support\Helpers\Utils;
use WPUltimateSecurity\UltimateSecurity\Support\Helpers\IpHelper;
use WPUltimateSecurity\UltimateSecurity\Support\Helpers\StringHelper;

class PublicHooks
{
    public static function register()
    {
        add_action('wp_enqueue_scripts', [self::class, 'enqueue_styles']);
        add_action('wp_enqueue_scripts', [self::class, 'enqueue_scripts']);
        add_action('wp_footer', [self::class, 'ultimate_security_top_bar']);

        add_action('wp_head', [self::class, 'get_footer_enqueue_styles'], 9999);

        add_shortcode("ultimate-security-gdpr-banner", [self::class, "ultimate_security_gdpr_banner_shortcode"], 10, 2);
    }

    public static function enqueue_styles()
    {
        wp_enqueue_style('ultimate-security', ULTIMATE_SECURITY_URL . 'assets/public/css/style.css', array(), ULTIMATE_SECURITY_VERSION, 'all');
        wp_enqueue_style('ultimate-security' . '-public', ULTIMATE_SECURITY_URL . 'assets/public/css/public-style.css', array(), ULTIMATE_SECURITY_VERSION, 'all');
    }
    public static function get_footer_enqueue_styles()
    {
        $request_uri = isset($_SERVER['REQUEST_URI']) ? sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])) : '';
        if (strpos($request_uri, '/ultimate-security-2fa-verification') !== false) {
            // Core WP styles - use URL helpers instead of ABSPATH.
            wp_enqueue_style(
                'dashicons',
                includes_url('css/dashicons.css'),
                array(),
                ULTIMATE_SECURITY_VERSION,
                'all'
            );

            wp_enqueue_style(
                'buttons',
                includes_url('css/buttons.css'),
                array(),
                ULTIMATE_SECURITY_VERSION,
                'all'
            );

            wp_enqueue_style(
                'forms',
                admin_url('css/forms.css'),
                array(),
                ULTIMATE_SECURITY_VERSION,
                'all'
            );

            wp_enqueue_style(
                'l10n',
                admin_url('css/l10n.css'),
                array(),
                ULTIMATE_SECURITY_VERSION,
                'all'
            );

            wp_enqueue_style(
                'login',
                admin_url('css/login.css'),
                array(),
                ULTIMATE_SECURITY_VERSION,
                'all'
            );

        }
    }

    public static function enqueue_scripts()
    {
        wp_enqueue_script('ultimate-security' . '-ajax', ULTIMATE_SECURITY_URL . 'assets/public/js/ajax.js', array('jquery'), ULTIMATE_SECURITY_VERSION, false);
        wp_enqueue_script('ultimate-security' . '-public-script', ULTIMATE_SECURITY_URL . 'assets/public/js/public-script.js', array('jquery'), ULTIMATE_SECURITY_VERSION, false);


        $ajax_params = array(
            'admin_url' => admin_url(),
            'ajax_url' => admin_url('admin-ajax.php'),
            'security' => current_user_can('manage_options') ? esc_attr(wp_create_nonce('ultimate_security_nonce')) : strtolower(StringHelper::random(10)),
        );
        wp_localize_script('ultimate-security' . '-ajax', 'ultimate_security_ajax_obj', $ajax_params);
    }
    public static function ultimate_security_shop_page_redirect()
    {
        if (is_woocommerce() || is_checkout() || is_cart()) {
            wp_redirect(home_url('/restricted/'));
            exit();
        }
    }

    public static function ultimate_security_added_page_content($content)
    {
        if (is_page()) {
            return $content = '';
        }

        return $content;
    }

    /**
     * REMOVED: Debug IP switching top bar
     * This feature depended on restrictions.settings.debug_log which has been removed
     * as part of the server-level features cleanup.
     */
    public static function ultimate_security_top_bar()
    {
        // Feature removed - restrictions config no longer exists
    }

    public static function ultimate_security_gdpr_banner_shortcode()
    {
        ob_start();
        $ultimate_security_options = Utils::ultimate_security_get_option();

        $current_banner_content_enable = (isset($ultimate_security_options['security']['login_security']['current_banner_content_enable']) && !empty($ultimate_security_options['security']['login_security']['current_banner_content_enable'])
        ) ? sanitize_text_field(wp_unslash($ultimate_security_options['security']['login_security']['current_banner_content_enable'])) : 0;

        $current_banner_content = (isset($ultimate_security_options['security']['login_security']['current_banner_content']) && !empty($ultimate_security_options['security']['login_security']['current_banner_content'])
        ) ? wp_kses_post(wp_unslash($ultimate_security_options['security']['login_security']['current_banner_content'])) : __('By logging in to this website you are consenting this website to process the IP address and browser information for security purposes.', 'ultimate-security');
        if ($current_banner_content_enable):
            ?>
            <div><input type="checkbox" required /><?php echo wp_kses_post($current_banner_content) ?></div>
            <?php
        endif;
        $html = ob_get_clean();
        return $html;
    }
}
