<?php

namespace WPUltimateSecurity\UltimateSecurity\Modules\Compatibility;

use WPUltimateSecurity\UltimateSecurity\Support\Helpers\PluginDetection;

class FlavorIntegration
{
    public function __construct()
    {
        $this->init();
    }

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

        // Adjust security headers for Flavor editor
        add_filter('ultimate_security_frame_options', [$this, 'adjust_frame_options']);

        // Whitelist Flavor REST API endpoints
        add_filter('ultimate_security_url_guard_check', [$this, 'whitelist_flavor_endpoints'], 10, 2);

        // Allow heartbeat API for Flavor editors
        add_filter('ultimate_security_skip_heartbeat_limit', [$this, 'allow_flavor_heartbeat']);
    }

    public function skip_flavor_ajax($should_limit, $context): bool
    {
        // Check if user can edit
        if (!current_user_can('edit_posts')) {
            return $should_limit;
        }

        // Skip for Flavor theme AJAX
        if (PluginDetection::is_flavor_ajax()) {
            return false;
        }

        // Skip for Flavor Studio AJAX
        if (PluginDetection::is_flavor_studio_ajax()) {
            return false;
        }

        // Check for specific Flavor actions
        if (isset($_REQUEST['action'])) {
            $action = sanitize_text_field(wp_unslash($_REQUEST['action']));
            $whitelist = array_merge(
                PluginDetection::get_flavor_ajax_whitelist(),
                PluginDetection::get_flavor_studio_ajax_whitelist()
            );

            if (in_array($action, $whitelist, true)) {
                return false;
            }
        }

        return $should_limit;
    }

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

        // Allow iframes for Flavor editors
        if (PluginDetection::is_flavor_editor() || PluginDetection::is_flavor_studio_editor()) {
            return 'SAMEORIGIN';
        }

        return $options;
    }

    public function whitelist_flavor_endpoints($should_block, $request_uri): bool
    {
        // Whitelist Flavor REST API
        if (strpos($request_uri, '/wp-json/flavor/') !== false) {
            return false;
        }

        // Whitelist Flavor Studio REST API
        if (strpos($request_uri, '/wp-json/flavor-studio/') !== false) {
            return false;
        }

        // Whitelist Flavor Developer REST API
        if (strpos($request_uri, '/wp-json/fd/') !== false) {
            return false;
        }

        // Whitelist Flavor Starter REST API
        if (strpos($request_uri, '/wp-json/fs/') !== false) {
            return false;
        }

        return $should_block;
    }

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

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

        // Allow heartbeat for Flavor editors
        return PluginDetection::is_flavor_editor() || PluginDetection::is_flavor_studio_editor();
    }

    public static function get_flavor_compatibility_info(): array
    {
        return [
            'flavor_theme' => PluginDetection::is_flavor_theme_active(),
            'flavor_developer' => PluginDetection::is_flavor_developer_theme_active(),
            'flavor_starter' => PluginDetection::is_flavor_starter_theme_active(),
            'flavor_studio' => PluginDetection::is_flavor_studio_active(),
            'is_editor' => PluginDetection::is_flavor_editor() || PluginDetection::is_flavor_studio_editor(),
        ];
    }
}
