<?php

namespace PortalPeople\invoicing;

use yii\base\BootstrapInterface;
use Yii;

/**
 * Surf Invoices Module
 *
 * This module handles invoice generation, management, and credit notes for orders.
 * Features:
 * - Automatic invoice generation based on centrearrddate
 * - Credit note creation against invoices
 * - Cart invoice linkage and status tracking
 * - Idempotent generation with duplicate prevention
 */
class Module extends \yii\base\Module implements BootstrapInterface
{
    /**
     * {@inheritdoc}
     */
    public $controllerNamespace = 'PortalPeople\invoicing\controllers';

    /**
     * {@inheritdoc}
     */
    public function init()
    {
        parent::init();
        $this->registerTranslations();
    }

    /**
     * Bootstrap the module
     *
     * @param \yii\base\Application $app
     */
    public function bootstrap($app)
    {
        // Module bootstrap - called during application bootstrap phase
        // Additional initialization can be added here if needed
        $app->controllerMap['invoicing'] = [
            'class' => 'PortalPeople\invoicing\controllers\InvoiceController',
        ];
        $app->urlManager->addRules([
            'invoice' => 'invoicing/invoice/index',
            'invoice/<action:\w+>' => 'invoicing/invoice/<action>',
        ], false);
    }

    /**
     * Register module translations
     */
    public function registerTranslations()
    {
        Yii::$app->i18n->translations['PortalPeople/invoicing/*'] = [
            'class' => 'yii\i18n\PhpMessageSource',
            'sourceLanguage' => 'en-US',
            'basePath' => '@app/NGauge_modules/surf-invoicing/messages',
            'fileMap' => [
                'PortalPeople/invoicing/module' => 'module.php',
                'PortalPeople/invoicing/invoice' => 'invoice.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/invoicing/module';
        } else {
            $category = 'PortalPeople/invoicing/' . $category;
        }
        return Yii::t($category, $message, $params, $language);
    }
}
