<?php

declare(strict_types=1);

namespace CloudXM\SDK\Providers;

use CloudXM\SDK\Contracts\ConfigurationInterface;
use CloudXM\SDK\Contracts\ContainerInterface;
use CloudXM\SDK\Support\HasLogger;
use CloudXM\SDK\WooCommerce\Compatibility\BlocksCompatibility;
use CloudXM\SDK\WooCommerce\Compatibility\HposCompatibility;

/**
 * WooCommerce Compatibility Service Provider
 *
 * Handles WooCommerce compatibility declarations using reusable SDK helpers.
 * This provider can be used by any CloudXM plugin to declare WooCommerce compatibility.
 */
class WooCompatibilityServiceProvider
{
    use HasLogger;

    public function __construct(
        private ContainerInterface $container,
        private ConfigurationInterface $config
    ) {}

    /**
     * Register WooCommerce compatibility declarations
     */
    public function register(): void
    {
        // Use configuration to get plugin file path automatically
        $pluginFile = $this->getPluginFilePath();

        // Register WooCommerce compatibility using the plugin file
        $this->registerWooCommerceCompatibility($pluginFile);

        $this->log()->debug('sdk.woocommerce_compatibility.registered', [
            'plugin_name' => $this->config->getPluginName(),
            'plugin_file' => $pluginFile,
            'sink' => 'internal'
        ]);
    }

    /**
     * Get the plugin file path from configuration
     */
    private function getPluginFilePath(): string
    {
        // Use the configuration's plugin file path method
        return $this->config->getPluginFilePath();
    }

    /**
     * Declare WooCommerce compatibility using SDK helpers
     *
     * @param string $pluginFile Absolute path to the main plugin file
     */
    public function registerWooCommerceCompatibility(string $pluginFile): void
    {
        // Use SDK helper for HPOS compatibility
        HposCompatibility::declare($pluginFile);

        // Use SDK helper for Blocks compatibility
        BlocksCompatibility::hook($pluginFile);

        $this->log()->debug('sdk.woocommerce_compatibility.declared', [
            'plugin_file' => $pluginFile,
            'sink' => 'internal'
        ]);
    }
}