<?php

namespace WPUltimateSecurity\UltimateSecurity\Modules\Compatibility;

use WPUltimateSecurity\UltimateSecurity\Support\Helpers\PluginDetection;

class PageBuildersIntegration
{
    private $builder_type;

    public function __construct(string $builder_type)
    {
        $this->builder_type = $builder_type;
        $this->init();
    }

    public function init()
    {
        // Skip rate limiting for page builder AJAX
        add_filter('ultimate_security_rate_limit_check', [$this, 'skip_builder_ajax'], 10, 2);

        // Adjust security headers for page builder iframes
        add_filter('ultimate_security_frame_options', [$this, 'adjust_frame_options']);

        // Whitelist heartbeat API for editors
        add_filter('ultimate_security_skip_heartbeat_limit', [$this, 'allow_editor_heartbeat']);
    }

    public function skip_builder_ajax($should_limit, $context): bool
    {
        // Check if this is an editor session
        if (!current_user_can('edit_posts')) {
            return $should_limit;
        }

        $whitelist = $this->get_ajax_whitelist();

        if (isset($_REQUEST['action'])) {
            $action = sanitize_text_field(wp_unslash($_REQUEST['action']));
            if (in_array($action, $whitelist, true)) {
                return false; // Don't rate limit
            }
        }

        return $should_limit;
    }

    public function adjust_frame_options($options): string
    {
        if (!current_user_can('edit_posts')) {
            return $options;
        }

        $is_editor = false;

        switch ($this->builder_type) {
            case 'elementor':
                $is_editor = PluginDetection::is_elementor_editor();
                break;
            case 'divi':
                $is_editor = PluginDetection::is_divi_editor();
                break;
            case 'avada':
                $is_editor = PluginDetection::is_avada_editor();
                break;
            case 'wpbakery':
                $is_editor = PluginDetection::is_wpbakery_editor();
                break;
            case 'beaver_builder':
                $is_editor = PluginDetection::is_beaver_builder_editor();
                break;
        }

        if ($is_editor) {
            return 'SAMEORIGIN';
        }

        return $options;
    }

    public function allow_editor_heartbeat($skip): bool
    {
        if ($skip) {
            return true;
        }

        if (!current_user_can('edit_posts')) {
            return false;
        }

        // Check if we're in any page builder editor
        return PluginDetection::is_any_page_builder_editor();
    }

    private function get_ajax_whitelist(): array
    {
        switch ($this->builder_type) {
            case 'elementor':
                return PluginDetection::get_elementor_ajax_whitelist();
            case 'divi':
                return PluginDetection::get_divi_ajax_whitelist();
            case 'avada':
                return PluginDetection::get_avada_ajax_whitelist();
            case 'wpbakery':
                return PluginDetection::get_wpbakery_ajax_whitelist();
            case 'beaver_builder':
                return PluginDetection::get_beaver_builder_ajax_whitelist();
            default:
                return [];
        }
    }

    public function get_builder_type(): string
    {
        return $this->builder_type;
    }

    public static function get_all_builder_ajax_whitelist(): array
    {
        $whitelist = [];

        if (PluginDetection::is_elementor_active()) {
            $whitelist = array_merge($whitelist, PluginDetection::get_elementor_ajax_whitelist());
        }
        if (PluginDetection::is_divi_active()) {
            $whitelist = array_merge($whitelist, PluginDetection::get_divi_ajax_whitelist());
        }
        if (PluginDetection::is_avada_active()) {
            $whitelist = array_merge($whitelist, PluginDetection::get_avada_ajax_whitelist());
        }
        if (PluginDetection::is_wpbakery_active()) {
            $whitelist = array_merge($whitelist, PluginDetection::get_wpbakery_ajax_whitelist());
        }
        if (PluginDetection::is_beaver_builder_active()) {
            $whitelist = array_merge($whitelist, PluginDetection::get_beaver_builder_ajax_whitelist());
        }

        return array_unique($whitelist);
    }
}
