<?php

namespace SoftminalB2B\Services;

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

use Automattic\WooCommerce\Utilities\FeaturesUtil;

/**
 * WooCommerce feature compatibility (FEATURES H1).
 *
 * Declares support for HPOS (custom order tables) and the Cart/Checkout Blocks.
 * Both declarations must be registered on `before_woocommerce_init`, which fires
 * while WooCommerce boots — so this is hooked at plugin-file load time, before
 * that action runs, rather than from a `plugins_loaded` callback that could be
 * too late.
 *
 * The acceptance bar is that WooCommerce never shows an incompatibility notice
 * for this plugin, which is only true if these are declared correctly and every
 * order access really does go through the CRUD (it does — see the resolver,
 * PDF invoices and cart code).
 */
class Compat
{
    private static bool $registered = false;

    /**
     * @param string $mainFile The plugin's main file, for the compat handle.
     */
    public static function register(string $mainFile): void
    {
        if (self::$registered) {
            return;
        }

        self::$registered = true;

        add_action('before_woocommerce_init', static function () use ($mainFile) {
            if (!class_exists(FeaturesUtil::class)) {
                return;
            }

            // HPOS: this plugin stores order-level B2B facts as order meta via
            // the WC_Order CRUD and never queries the posts table directly.
            FeaturesUtil::declare_compatibility('custom_order_tables', $mainFile, true);

            // Blocks: every checkout-affecting feature (pricing, quantity rules,
            // price hiding) is implemented for the Store API as well as classic.
            FeaturesUtil::declare_compatibility('cart_checkout_blocks', $mainFile, true);
        });
    }
}
