<?php
/**
 * Environment Variable Loader
 * Loads environment variables from .env file
 */

function loadEnvironmentVariables() {
    // Look for .env in root directory
    $envFile = __DIR__ . '/../.env';
    
    if (!file_exists($envFile)) {
        error_log("env-loader: .env file not found at $envFile");
        return false;
    }
    
    $lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    
    foreach ($lines as $line) {
        // Skip comments
        if (strpos(trim($line), '#') === 0) {
            continue;
        }
        
        // Parse KEY=VALUE
        if (strpos($line, '=') !== false) {
            list($key, $value) = explode('=', $line, 2);
            $key = trim($key);
            $value = trim($value);
            
            // Remove quotes if present
            $value = trim($value, '"\'');
            
            // Set environment variable
            putenv("$key=$value");
            $_ENV[$key] = $value;
        }
    }
    
    return true;
}

// Load environment variables
loadEnvironmentVariables();
?>
