<?php
namespace ShopyPress\Hooks;

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

use ShopyPress\Utils\OptionUtils;
use ShopyPress\Hooks\IHook;

class ActionWPFooter implements IHook
{
    static function register()
    {
        add_action('wp_footer', [self::class, 'renderShopifyComponents'], 999);
    }

    /**
     * Render Shopify store wrapper and cart component in footer
     */
    static function renderShopifyComponents()
    {
        // Get Shopify credentials from settings
        $store_domain = OptionUtils::getInstance()->getOption('shop_domain', '');
        $api_token = OptionUtils::getInstance()->getOption('shopify_api_token', '');

        // Don't render if credentials not set
        if (empty($store_domain) || empty($api_token)) {
            return;
        }

        // Get user locale (can be enhanced later)
        $locale = get_locale(); // e.g., 'en_US'
        $language = substr($locale, 0, 2); // 'en'
        $country = 'US'; // Default, can be detected or set in settings

        ?>
        <!-- Shopify Store Context -->
        <shopify-store
            store-domain="<?php echo esc_attr($store_domain); ?>"
            public-access-token="<?php echo esc_attr($api_token); ?>"
            country="<?php echo esc_attr($country); ?>"
            language="<?php echo esc_attr($language); ?>">
        </shopify-store>

        <!-- Global Cart Modal -->
        <shopify-cart id="shopypress-cart"></shopify-cart>
        <?php
    }
}
