<?php
/**
 * Simple .env file loader
 * Reads key=value pairs and puts them into $_ENV
 */
function loadEnv(string $path): void {
    if (!file_exists($path)) {
        die('ERROR: .env file not found at ' . $path);
    }

    $lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

    foreach ($lines as $line) {
        // Skip comments
        if (str_starts_with(trim($line), '#')) continue;

        // Split on first = only
        if (!str_contains($line, '=')) continue;

        [$key, $value] = explode('=', $line, 2);

        $key   = trim($key);
        $value = trim($value);

        // Remove surrounding quotes if present
        $value = trim($value, '"\'');

        // Set into environment
        $_ENV[$key]    = $value;
        putenv("{$key}={$value}");
    }
}

// Load the .env file — adjust path if needed
loadEnv(__DIR__ . '/.env');