<?php

/**
 * Composer Bootstrap File for Ngauge SQL Backup Plugin
 * 
 * This file is automatically executed by Yii2's composer plugin during
 * application initialization, allowing the plugin to register itself
 * without manual configuration.
 * 
 * The bootstrap mechanism works as follows:
 * 1. Composer installs the plugin package
 * 2. Yii2 composer plugin reads the "bootstrap" entry in composer.json
 * 3. This file is automatically included during app bootstrap
 * 4. The Module is instantiated and configuration is loaded
 * 
 * No manual configuration required - just install the package!
 */

use Yii;
use yii\base\Application;
use yii\console\Application as ConsoleApp;
use yii\web\Application as WebApp;

// Only bootstrap if not already registered
if (!isset(Yii::$app->getModules()['db-backup'])) {
    
    // Load appropriate configuration based on application type
    $basePath = dirname(__DIR__);
    
    if (Yii::$app instanceof ConsoleApp) {
        $configFile = $basePath . '/config/console.php';
    } elseif (Yii::$app instanceof WebApp) {
        $configFile = $basePath . '/config/web.php';
    } else {
        $configFile = $basePath . '/config/web.php'; // Default to web config
    }
    
    // Load and merge configuration if file exists
    if (file_exists($configFile)) {
        $config = require $configFile;
        
        // Register module if configured
        if (isset($config['modules']['db-backup'])) {
            Yii::$app->setModule('db-backup', $config['modules']['db-backup']);
        }
        
        // Add to bootstrap if specified
        if (isset($config['bootstrap']) && in_array('db-backup', $config['bootstrap'])) {
            $module = Yii::$app->getModule('db-backup');
            if ($module instanceof \yii\base\BootstrapInterface) {
                $module->bootstrap(Yii::$app);
            }
        }
    }
}
