<?php
/**
 * QVC Sync Module for Yii2
 *
 * @category    PortalPeople
 * @package     QVC
 * @author      Portal People Development Team
 * @copyright   Copyright (c) 2025 Portal People (https://portalpeople.co.uk)
 */

namespace PortalPeople\qvc;

use Yii;
use yii\base\Module as BaseModule;
use yii\base\Event;
use yii\db\ActiveRecord;
use backend\components\Security;
/**
 * QVC Sync Module
 * 
 * This module provides integration with QVC API for product synchronization
 */
class Module extends BaseModule implements \yii\base\BootstrapInterface
{
    /**
     * @var string the namespace that controller classes are in
     */
    public $controllerNamespace = 'PortalPeople\qvc\controllers';

    /**
     * @var string QVC API base URL
     */
    public $apiUrl;

    /**
     * @var bool Enable debug logging
     */
    public $debug = false;

    /**
     * Bootstrap method to add URL rules when the module is loaded.
     * @param \yii\base\Application $app the application currently running
    */
    public function bootstrap($app)
    {
        $this->id = 'qvc';
        // Register the module if not already registered
     //   Yii::warning($app->getModules()[$this->id], 'Module bootstrap');
        if (!isset($app->getModules()[$this->id])) {
            $app->setModule($this->id, $this);
        }
        
        $app->urlManager->addRules([
            "<controller:qvc>/<action:[\w\-]+>" => "qvc/<controller>/<action>",
        ], false); // false = append rules (don't prepend)

        
        Event::on(\backend\models\CartsData::class, 'carts-set-data-types', function($event){
            $event->customData['shipping'] = 'shipping';
        });
        Yii::$app->on(
            'carts-after-DataLoad-shipping', 
            function($event){
                $event->result = \PortalPeople\qvc\models\carts\Shipping::class;
            }
        );
        
        self::init();
    }
    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();
    
        // Load configuration from params
        if (isset(Yii::$app->params['plugins']['qvc'])) {
            $config = Yii::$app->params['plugins']['qvc'];
            $this->apiUrl = $config['apiUrl'] ?? $this->apiUrl;
            // Decrypt sensitive authentication fields
            $this->debug = $config['debug'] ?? $this->debug;
        }

        Event::on(\modules\custom\models\ProductsData::class, 'products-set-data-types', function($event){
            $event->buttons['qvc'] = 'qvc';
        });
        Yii::$app->on('products-after-DataLoad-qvc', function($event){
           $event->result = \PortalPeople\qvc\models\products\Qvc::class;
        });
        // Register all view customizations from this module's config directory
        //AdminViewsRegistry::registerModuleViews('qvc');
    }

    /**
     * Attach event handlers for automatic synchronization
     */
    protected function attachEventHandlers()
    {


    }

    /**
     * Get module version
     * 
     * @return string
     */
    public function getVersion()
    {
        return '1.2.0';
    }

    /**
     * Get module configuration
     * 
     * @return array
     */
    public function getConfig()
    {
        return [
            'debug' => $this->debug,
            'version' => $this->getVersion(),
            'configured' => $this->isConfigured(),
        ];
    }
}