<?php
// Environment and Configuration Loader for FlixCooks

function load_env() {
    $path = __DIR__ . '/.env';
    if (!file_exists($path)) {
        return;
    }
    
    $lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    foreach ($lines as $line) {
        // Skip comments
        if (strpos(trim($line), '#') === 0) {
            continue;
        }
        
        // Parse Name=Value
        if (strpos($line, '=') !== false) {
            list($name, $value) = explode('=', $line, 2);
            $name = trim($name);
            $value = trim($value);
            
            // Remove wrapping quotes if present
            $value = trim($value, '"\'');
            
            // Populate getenv(), $_ENV, $_SERVER
            putenv("{$name}={$value}");
            $_ENV[$name] = $value;
            $_SERVER[$name] = $value;
        }
    }
}

// Automatically load on include
load_env();

function get_firebase_config(): array {
    return [
        'apiKey'            => getenv('FIREBASE_API_KEY') ?: '',
        'authDomain'        => getenv('FIREBASE_AUTH_DOMAIN') ?: '',
        'projectId'         => getenv('FIREBASE_PROJECT_ID') ?: '',
        'storageBucket'     => getenv('FIREBASE_STORAGE_BUCKET') ?: '',
        'messagingSenderId' => getenv('FIREBASE_MESSAGING_SENDER_ID') ?: '',
        'appId'             => getenv('FIREBASE_APP_ID') ?: '',
    ];
}
