<?php

namespace WPSN\Core;

defined('ABSPATH') || exit;

/**
 * Detect common compatibility/conflict signals.
 *
 * We avoid listing all active plugins in diagnostics to reduce accidental oversharing.
 * This detector focuses on well-known SEO plugins that may output overlapping head tags.
 */
final class CompatibilityDetector
{
    /**
     * @return array<string,array{name:string,detected:bool,signal:string}>
     */
    public static function detectSeoPlugins(): array
    {
        $activeFiles = self::activePluginFiles();

        $checks = [
            'yoast' => [
                'name' => 'Yoast SEO',
                'signals' => [
                    'constant:WPSEO_VERSION' => defined('WPSEO_VERSION'),
                    'plugin:wordpress-seo/wp-seo.php' => isset($activeFiles['wordpress-seo/wp-seo.php']),
                    'plugin:wordpress-seo-premium/wp-seo-premium.php' => isset($activeFiles['wordpress-seo-premium/wp-seo-premium.php']),
                ],
            ],
            'rankmath' => [
                'name' => 'Rank Math',
                'signals' => [
                    'constant:RANK_MATH_VERSION' => defined('RANK_MATH_VERSION'),
                    'plugin:seo-by-rank-math/rank-math.php' => isset($activeFiles['seo-by-rank-math/rank-math.php']),
                    'plugin:seo-by-rank-math-pro/rank-math-pro.php' => isset($activeFiles['seo-by-rank-math-pro/rank-math-pro.php']),
                ],
            ],
            'aioseo' => [
                'name' => 'All in One SEO',
                'signals' => [
                    'constant:AIOSEO_VERSION' => defined('AIOSEO_VERSION'),
                    'constant:AIOSEO_PLUGIN_VERSION' => defined('AIOSEO_PLUGIN_VERSION'),
                    // Common plugin file slugs (best-effort).
                    'plugin:all-in-one-seo-pack/all_in_one_seo_pack.php' => isset($activeFiles['all-in-one-seo-pack/all_in_one_seo_pack.php']),
                    'plugin:aioseo/aioseo.php' => isset($activeFiles['aioseo/aioseo.php']),
                ],
            ],
            'seopress' => [
                'name' => 'SEOPress',
                'signals' => [
                    'constant:SEOPRESS_VERSION' => defined('SEOPRESS_VERSION'),
                    'plugin:wp-seopress/seopress.php' => isset($activeFiles['wp-seopress/seopress.php']),
                    'plugin:wp-seopress-pro/seopress-pro.php' => isset($activeFiles['wp-seopress-pro/seopress-pro.php']),
                ],
            ],
            'theseoframework' => [
                'name' => 'The SEO Framework',
                'signals' => [
                    'constant:THE_SEO_FRAMEWORK_VERSION' => defined('THE_SEO_FRAMEWORK_VERSION'),
                    'plugin:autodescription/autodescription.php' => isset($activeFiles['autodescription/autodescription.php']),
                ],
            ],
        ];

        $out = [];
        foreach ($checks as $id => $data) {
            $name = (string) ($data['name'] ?? $id);
            $signals = isset($data['signals']) && is_array($data['signals']) ? $data['signals'] : [];

            $detected = false;
            $signal = '';

            foreach ($signals as $sig => $ok) {
                if ($ok) {
                    $detected = true;
                    $signal = (string) $sig;
                    break;
                }
            }

            $out[$id] = [
                'name' => $name,
                'detected' => $detected,
                'signal' => $signal,
            ];
        }

        return $out;
    }

    public static function hasSeoConflict(): bool
    {
        foreach (self::detectSeoPlugins() as $row) {
            if (is_array($row) && !empty($row['detected'])) {
                return true;
            }
        }
        return false;
    }

    /**
     * @return array<string,bool> Map of plugin file => true
     */
    private static function activePluginFiles(): array
    {
        $files = [];

        $list = get_option('active_plugins', []);
        if (is_array($list)) {
            foreach ($list as $f) {
                $f = (string) $f;
                if ($f !== '') {
                    $files[$f] = true;
                }
            }
        }

        if (is_multisite()) {
            $sitewide = get_site_option('active_sitewide_plugins', []);
            if (is_array($sitewide)) {
                foreach ($sitewide as $f => $on) {
                    $f = (string) $f;
                    if ($f !== '') {
                        $files[$f] = true;
                    }
                }
            }
        }

        return $files;
    }
}
