<?php
namespace PortalPeople\packagemanager;

use Yii;
use yii\base\{BootstrapInterface, Module as BaseModule};
use yii\i18n\PhpMessageSource;

/**
 * HSE Fatigue & Risk Calculator Module
 *
 * This module provides HSE-compliant fatigue and risk index calculations
 * for shift work patterns. It can be integrated into any Yii2 application.
 */
class Module extends BaseModule implements BootstrapInterface
{
    //public $id = 'packagemanager';
    
    public $controllerNamespace = 'PortalPeople\packagemanager\controllers';
    /**
     * @var string The [[UrlRule]] class to use for rules generated by this module.
     * @since 2.1.1
    */
    public $urlRuleClass = 'yii\web\UrlRule';
    /**
     * 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 = 'packagemanager';
        // 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);
        }
        
        // Register URL rules
        $app->urlManager->addRules([
            // Specific action routes (most specific first)
            'package-manager' => "{$this->id}/package-manager/index",
            'package-manager/<action:[\w\-]+>' => "{$this->id}/package-manager/<action>",
            'package-manager/<action:\w+>/<id:\d+>' => "{$this->id}/package-manager/<action>",
            
        ], false); // false = append rules (don't prepend)
    }

    public function init()
    {
        parent::init();

        // Register translations if needed
        if (!isset(Yii::$app->i18n->translations['api*'])) {
            Yii::$app->i18n->translations['api*'] = [
                'class' => PhpMessageSource::class,
                'sourceLanguage' => 'en-US',
                'basePath' => '@portalpeople/packagemanager/messages',
            ];
        }
    }
}