<?php

namespace PortalPeople\booking;

use Yii;

/**
 * Booking Module
 * 
 * Handles sports lesson booking functionality:
 * - Timeline calendar interface
 * - Instructor management
 * - Sport management
 * - Booking CRUD operations
 * - Drag and drop functionality
 * - Email notifications
 */
class Module extends \yii\base\Module
{
        /**
         * Bootstrap the module for console application
         * @param \yii\base\Application $app
         */
    public function bootstrap($app)
    {
        // 1. Register migration namespace for console
        if ($app instanceof \yii\console\Application) {
            $migrateController = $app->controllerMap['migrate'] ?? null;
            if ($migrateController && isset($migrateController['migrationNamespaces'])) {
                if (!in_array('PortalPeople\\booking\\migrations', $migrateController['migrationNamespaces'], true)) {
                    $app->controllerMap['migrate']['migrationNamespaces'][] = 'PortalPeople\\booking\\migrations';
                }
            }
            // 8. Register custom console commands
            $app->controllerMap['booking'] = [
                'class' => 'PortalPeople\\booking\\commands\\BookingController',
            ];
        }

        // 2. Register module alias
        \Yii::setAlias('@booking', dirname(__DIR__));

        // 3. Register DI components/services
        $app->set('bookingManager', [
            'class' => 'PortalPeople\\booking\\components\\BookingManager',
        ]);
        $app->set('bookingNotifier', [
            'class' => 'PortalPeople\\booking\\components\\BookingNotifier',
        ]);

        // 4. Register event handlers
        \Yii::$app->on('booking.created', function ($event) {
            // Custom logic for booking creation event
        });

        // 5. Access control for booking controllers
        if ($app instanceof \yii\web\Application) {
            $app->attachBehavior('bookingAccess', [
                'class' => \yii\filters\AccessControl::class,
                'only' => ['booking/*'],
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ]);
        }

        // 6. Module parameters
        $this->params['bookingLimit'] = 10;
        $this->params['notificationEmail'] = 'booking@ion-club.net';

        // 7. Asset bundle registration (example)
        if ($app instanceof \yii\web\Application && isset($app->assetManager)) {
            $app->assetManager->bundles['PortalPeople\\booking\\assets\\BookingAsset'] = [
                'class' => 'PortalPeople\\booking\\assets\\BookingAsset',
            ];
        }

        // 9. API versioning (example URL rules)
        if ($app instanceof \yii\web\Application && isset($app->urlManager)) {
            $app->urlManager->addRules([
                'v1/booking' => 'booking/default/index',
                'v1/booking/<id:\d+>' => 'booking/default/view',
                'v1/booking/create' => 'booking/default/create',
                'v1/booking/update/<id:\d+>' => 'booking/default/update',
                'v1/booking/delete/<id:\d+>' => 'booking/default/delete',
                // Add more as needed
                // Original booking rules
                'booking' => 'booking/default/index',
                'booking/<id:\d+>' => 'booking/default/view',
                'booking/create' => 'booking/default/create',
                'booking/update/<id:\d+>' => 'booking/default/update',
                'booking/delete/<id:\d+>' => 'booking/default/delete',
            ], false);
        }
    }
    /**
     * {@inheritdoc}
     */
    public $controllerNamespace = 'PortalPeople\\booking\\controllers';

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

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

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