<?php

namespace WPUltimateSecurity\UltimateSecurity\Modules\Compatibility;

use WPUltimateSecurity\UltimateSecurity\Support\Helpers\PluginDetection;
use WPUltimateSecurity\UltimateSecurity\Support\Helpers\Utils;

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

    public function init()
    {
        // Exclude custom login URL from sitemaps
        if (PluginDetection::is_yoast_active()) {
            add_filter('wpseo_exclude_from_sitemap_by_url', [$this, 'exclude_security_urls_yoast']);
        }

        if (PluginDetection::is_rank_math_active()) {
            add_filter('rank_math/sitemap/exclude_post_type', [$this, 'exclude_security_urls_rankmath'], 10, 2);
        }

        if (PluginDetection::is_aioseo_active()) {
            add_filter('aioseo_sitemap_excluded_pages', [$this, 'exclude_security_urls_aioseo']);
        }

        // Add noindex to security pages
        add_action('wp_head', [$this, 'add_noindex_to_security_pages'], 1);
    }

    public function exclude_security_urls_yoast($urls): array
    {
        $security_urls = $this->get_security_urls();

        foreach ($security_urls as $url) {
            $urls[] = $url;
        }

        return $urls;
    }

    public function exclude_security_urls_rankmath($exclude, $type): bool
    {
        // Check if this is a security-related post type
        $options = Utils::ultimate_security_get_option();
        $custom_login_slug = $options['security']['login_url']['custom_login_slug'] ?? '';

        if (!empty($custom_login_slug)) {
            // Get current URL being processed
            global $post;
            if ($post && $post->post_name === $custom_login_slug) {
                return true; // Exclude
            }
        }

        return $exclude;
    }

    public function exclude_security_urls_aioseo($excluded): array
    {
        $security_urls = $this->get_security_urls();

        foreach ($security_urls as $url) {
            $excluded[] = $url;
        }

        return $excluded;
    }

    public function add_noindex_to_security_pages()
    {
        if ($this->is_security_page()) {
            echo '<meta name="robots" content="noindex, nofollow">' . "\n";
        }
    }

    private function get_security_urls(): array
    {
        $urls = [];
        $options = Utils::ultimate_security_get_option();

        // Custom login URL
        $custom_login_slug = $options['security']['login_url']['custom_login_slug'] ?? '';
        if (!empty($custom_login_slug)) {
            $urls[] = home_url('/' . $custom_login_slug);
            $urls[] = home_url('/' . $custom_login_slug . '/');
        }

        // 2FA verification page if applicable
        $urls[] = home_url('/2fa-verify/');

        return $urls;
    }

    private function is_security_page(): bool
    {
        $options = Utils::ultimate_security_get_option();
        $custom_login_slug = $options['security']['login_url']['custom_login_slug'] ?? '';

        if (!empty($custom_login_slug)) {
            $current_uri = isset($_SERVER['REQUEST_URI']) ? sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])) : '';
            if (strpos($current_uri, $custom_login_slug) !== false) {
                return true;
            }
        }

        // Check for 2FA page
        if (isset($_GET['action']) && $_GET['action'] === 'ultimate_security_2fa') {
            return true;
        }

        return false;
    }
}
