<?php
/**
 * Magento 2 Google Analytics 4 for Magento 2 GA4
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Anowave license that is
 * available through the world-wide-web at this URL:
 * http://www.anowave.com/license-agreement/
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category 	Anowave
 * @package 	Anowave_Ec4
 * @copyright 	Copyright (c) 2025 Anowave (http://www.anowave.com/)
 * @license  	http://www.anowave.com/license-agreement/
 */

namespace Anowave\Ec4\Helper;

use Anowave\Package\Helper\Package;

use Anowave\Ec\Helper\Data as BaseHelper;
use Anowave\Ec\Helper\Scope;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\State;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\SalesRule\Api\RuleRepositoryInterface;

class Data extends Package
{
    /**
     * Shared measurement ID config path
     *
     * @var string
     */
    const CONFIG_SHARED = 'ec/api/google_gtm_ua4_measurement_id';
    
    /**
     * Dedicated measurement ID config path
     *
     * @var string
     */
    const CONFIG_DEDICATED = 'ec/api/google_gtm_ua4_measurement_id_backend';

    /**
     * Active state config
     */
    const CONFIG_ACTIVE = 'ec/ec4/active';
    
    /**
     * Package name
     * @var string
     */
    protected $package = 'MAGE2-GTMGA4';
    
    /**
     * Config path
     * @var string
     */
    protected $config = 'ec/ec4/license';
    
    /**
     * @var BaseHelper
     */
    protected $baseHelper;
 
    /**
     * @var RuleRepositoryInterface
     */
    protected $ruleRepositoryInterface;
    
    /**
     * @var ScopeConfigInterface
     */
    protected $scopeConfig;

    /**
     * @var Scope
     */
    protected $scopeHelper;
    
    /**
     * @var State
     */
    protected $state;
    
    /**
     * Constructor 
     * 
     * @param Context $context
     * @param BaseHelper $baseHelper
     * @param RuleRepositoryInterface $ruleRepositoryInterface
     * @param ScopeConfigInterface $scopeConfig
     * @param Scope $scopeHelper,
     * @param \Magento\Framework\App\State $state
     */
    public function __construct
    (
        Context $context,
        BaseHelper $baseHelper,
        RuleRepositoryInterface $ruleRepositoryInterface,
        ScopeConfigInterface $scopeConfig,
        Scope $scopeHelper,
        State $state
    )
    {
        $this->baseHelper = $baseHelper;
        
        /**
         * Set rule repository interface 
         * 
         * @var RuleRepositoryInterface $ruleRepositoryInterface
         */
        $this->ruleRepositoryInterface = $ruleRepositoryInterface;
        
        /**
         * Set scope config 
         * 
         * @var ScopeConfigInterface $scopeConfig
         */
        $this->scopeConfig = $scopeConfig;

        /**
         * Set scope helper
         * 
         * @var Scope
         */
        $this->scopeHelper = $scopeHelper;

        /**
         * Set state
         * 
         * \Magento\Framework\App\State
         */
        $this->state = $state;
        
        
        parent::__construct($context);
    }
    
    /**
     * Check if GA4 is enabled
     *
     * @return bool
     */
    public function isGA4Enabled() : bool
    {
        if(php_sapi_name() !== "cli")
        {
            if (\Magento\Framework\App\Area::AREA_ADMINHTML === $this->state->getAreaCode())
            {
                return 1 === $this->filter((int) $this->scopeHelper->getConfig(static::CONFIG_ACTIVE));
            }
        }

        return 1 === $this->filter((int) $this->getConfig(static::CONFIG_ACTIVE));
    }
    
    /**
     * Get quote details 
     * 
     * @return array
     */
    public function getQuoteDetails() : array
    {
        /**
         * Quote array
         * 
         * @var array $quote
         */
        $quote = [];
        
        try 
        {
            /**
             * Get applies rules
             *
             * @var array $rules
             */
            $rules = explode(chr(44), (string) $this->getBaseHelper()->getCheckoutSession()->getQuote()->getAppliedRuleIds());

            $rules = array_filter($rules);

            if ($rules)
            {
                $coupon = [];
                
                $coupon[] = $this->getBaseHelper()->getCheckoutSession()->getQuote()->getCouponCode();
                
                if ($coupon)
                {
                    $coupon = join(chr(44), $coupon);
                    
                    $quote['coupon'] = $coupon;
                }
            }
        }
        catch (\Exception $e){}

        return $quote;
    }
    
    /**
     * Get base helper
     * 
     * @return BaseHelper
     */
    public function getBaseHelper() : BaseHelper
    {
        return $this->baseHelper;
    }
    
    
    /**
     * Get Measurement ID
     *
     * @param int $store
     * @return string
     */
    public function getMeasurementId(int $store = 0) : string
    {
        return $this->getBaseHelper()->getMeasurementId($store);
    }
    
    /**
     * Get measurement api secret
     *
     * @return string
     */
    public function getMeasurementApiSecret(int $store = 0) : string
    {
        return $this->getBaseHelper()->getMeasurementApiSecret($store);
    }
    
    /**
     * Get measurement id config string
     *
     * @return string
     */
    public function getMeasurementIdConfig() : string
    {
        return $this->getBaseHelper()->getMeasurementIdConfig();
    }
}