<?php

namespace PortalPeople\creditnotes;

use Yii;

/**
 * Credit Notes Module
 *
 * Provides credit note functionality for financial transactions.
 * Can be used standalone or with other modules (e.g., invoicing, subscriptions).
 *
 * Features:
 * - Draft/Issued/Cancelled states
 * - Amount validation against parent entity balance
 * - Auto-generated credit note numbers
 * - Audit trail with created_by timestamp
 */
class Module extends \yii\base\Module
{
    /**
     * {@inheritdoc}
     */
    public function init()
    {
        parent::init();
        $this->registerTranslations();
    }

    /**
     * Bootstrap the module
     *
     * @param \yii\base\Application $app
     */
    public function bootstrap($app)
    {
        $this->id = 'creditnotes';
        if (!isset($app->getModules()[$this->id])) {
            $app->setModule($this->id, $this);
        }
        // Register URL rules for module
        $app->urlManager->addRules([
            $this->id . '/<action:[\w\-]+>' => $this->id . '/config/<action>',
            $this->id . '/<controller:[\w\-]+>/<action:[\w\-]+>' => $this->id . '/<controller>/<action>',
        ], false);
        self::init();
    }

    /**
     * Register module translations
     */
    public function registerTranslations()
    {
        Yii::$app->i18n->translations['PortalPeople/creditnotes/*'] = [
            'class' => 'yii\i18n\PhpMessageSource',
            'sourceLanguage' => 'en-US',
            'basePath' => '@app/NGauge_modules/surf-creditnotes/messages',
            'fileMap' => [
                'PortalPeople/creditnotes/module' => 'module.php',
                'PortalPeople/creditnotes/creditnote' => 'creditnote.php',
            ],
        ];
    }

    /**
     * Translate a message with module i18n
     *
     * @param string $category
     * @param string $message
     * @param array $params
     * @param string $language
     * @return string
     */
    public static function t($category, $message = '', $params = [], $language = null)
    {
        if ($message === '') {
            $message = $category;
            $category = 'PortalPeople/creditnotes/module';
        } else {
            $category = 'PortalPeople/creditnotes/' . $category;
        }
        return Yii::t($category, $message, $params, $language);
    }
}
