<?php
/**
 * UPN (United Pallet Network) Delivery Module for NGauge/Yii2
 *
 * @category    PortalPeople
 * @package     UPN
 * @author      Portal People Development Team
 * @copyright   Copyright (c) 2026 Portal People (https://portalpeople.co.uk)
 */

namespace PortalPeople\upn;

use Yii;
use yii\base\Module as BaseModule;
use backend\components\Security;

/**
 * UPN Delivery Module
 *
 * Provides integration with the UPN (United Pallet Network) External Links
 * SOAP API for creating and managing pallet consignments.
 *
 * Configuration (backend/config/params-local.php):
 * ```php
 * 'plugins' => [
 *     'upn' => [
 *         'enabled'    => true,
 *         'apiUrl'     => 'https://services.upn.co.uk:20675/ExternalLinks',
 *         'depot'      => 123,
 *         'customerId' => 10016,
 *         'username'   => 'youruser',
 *         'password'   => '<encrypted>',
 *         'debug'      => false,
 *     ],
 * ],
 * ```
 */
class Module extends BaseModule implements \yii\base\BootstrapInterface
{
    /** @var string Yii2 controller namespace. */
    public $controllerNamespace = 'PortalPeople\upn\controllers';

    /** @var string UPN External Links SOAP API URL. */
    public $apiUrl = 'https://services.upn.co.uk:20675/ExternalLinks';

    /** @var int UPN depot number. */
    public $depot;

    /** @var int UPN customer ID (unique ID per customer account at the depot). */
    public $customerId;

    /** @var string UPN API username. */
    public $username;

    /** @var string UPN API password (decrypted). */
    public $password;

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

    /** @var string URL rule class. */
    public $urlRuleClass = 'yii\web\UrlRule';

    /**
     * Bootstrap: register URL rules.
     * @param \yii\base\Application $app
     */
    public function bootstrap($app)
    {
        $this->id = 'upn';

        if (!isset($app->getModules()[$this->id])) {
            $app->setModule($this->id, $this);
        }

        if (Yii::$app->name !== 'NGaugeApi') {
            $app->urlManager->addRules([
                "{$this->id}/<action:[\w\-]+>" => "{$this->id}/config/<action>",
                "{$this->id}/<controller:[\w\-]+>/<action:[\w\-]+>" => "{$this->id}/<controller>/<action>",
            ], false);
        }

        self::init();
    }

    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();

        if (isset(Yii::$app->params['plugins']['upn'])) {
            $config = Yii::$app->params['plugins']['upn'];

            $this->apiUrl     = $config['apiUrl']     ?? $this->apiUrl;
            $this->depot      = $config['depot']      ?? $this->depot;
            $this->customerId = $config['customerId'] ?? $this->customerId;
            $this->username   = $config['username']   ?? $this->username;
            $this->password   = Security::decryptSensitiveData($config['password'] ?? '');
            $this->debug      = $config['debug']      ?? $this->debug;
        }

        $this->registerComponents();
    }

    /**
     * Register module-level components.
     */
    protected function registerComponents()
    {
        Yii::$app->setComponents([
            'upnClient' => [
                'class'      => 'PortalPeople\upn\components\UpnClient',
                'apiUrl'     => $this->apiUrl,
                'depot'      => $this->depot,
                'customerId' => $this->customerId,
                'username'   => $this->username,
                'password'   => $this->password,
                'debug'      => $this->debug,
            ],
        ]);
    }

    /**
     * Whether the module has the minimum required configuration.
     * @return bool
     */
    public function isConfigured(): bool
    {
        return !empty($this->depot) && !empty($this->username) && !empty($this->password);
    }
}
