<?php

namespace SoftminalB2B\Services;

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Platform dependency guard (FEATURES H7).
 *
 * The PHP-version floor is checked in the main plugin file with old-PHP-safe
 * code, because this class itself uses PHP 8.1 syntax and would fail to parse
 * on an older runtime. By the time this class loads, PHP is already known good,
 * so it only has to check the things that need WordPress and WooCommerce
 * loaded: the WP version, and whether WooCommerce is active and new enough.
 *
 * On any failure it shows an admin notice and stops the plugin booting — it
 * never fatals, so the merchant's site stays up and they see a clear reason.
 */
class Requirements
{
    /** @var array<int, string> */
    private static array $problems = [];

    /**
     * @return bool True when everything the plugin needs is present.
     */
    public static function met(): bool
    {
        self::$problems = [];

        global $wp_version;

        if (version_compare($wp_version, SMB2B_MIN_WP, '<')) {
            self::$problems[] = sprintf(
                /* translators: 1: required WordPress version, 2: current version */
                __('WordPress %1$s or newer (you have %2$s)', 'softminal-b2b-wholesale-for-woocommerce'),
                SMB2B_MIN_WP,
                $wp_version
            );
        }

        if (!self::wooCommerceActive()) {
            self::$problems[] = __('WooCommerce to be installed and active', 'softminal-b2b-wholesale-for-woocommerce');
        } elseif (defined('WC_VERSION') && version_compare(WC_VERSION, SMB2B_MIN_WC, '<')) {
            self::$problems[] = sprintf(
                /* translators: 1: required WooCommerce version, 2: current version */
                __('WooCommerce %1$s or newer (you have %2$s)', 'softminal-b2b-wholesale-for-woocommerce'),
                SMB2B_MIN_WC,
                WC_VERSION
            );
        }

        if (!empty(self::$problems)) {
            add_action('admin_notices', [self::class, 'renderNotice']);
            return false;
        }

        return true;
    }

    private static function wooCommerceActive(): bool
    {
        // class_exists is the reliable signal once plugins are loaded; the
        // active-plugins option is the fallback for very early calls.
        if (class_exists('WooCommerce')) {
            return true;
        }

        $active = (array) get_option('active_plugins', []);

        if (in_array('woocommerce/woocommerce.php', $active, true)) {
            return true;
        }

        if (is_multisite()) {
            $network = (array) get_site_option('active_sitewide_plugins', []);
            return isset($network['woocommerce/woocommerce.php']);
        }

        return false;
    }

    public static function renderNotice(): void
    {
        if (empty(self::$problems) || !current_user_can('activate_plugins')) {
            return;
        }

        $intro = __('B2B Suite for WooCommerce needs the following before it can run:', 'softminal-b2b-wholesale-for-woocommerce');

        echo '<div class="notice notice-error"><p><strong>' . esc_html($intro) . '</strong></p><ul style="list-style:disc;margin-left:20px">';

        foreach (self::$problems as $problem) {
            echo '<li>' . esc_html($problem) . '</li>';
        }

        echo '</ul></div>';
    }
}
