<?php

declare(strict_types=1);

// ─── Autoloader ──────────────────────────────────────────────────────────────
require_once __DIR__ . '/../vendor/autoload.php';

// ─── Load .env (project defaults) ────────────────────────────────────────────
// phpunit.xml <env> entries override these, providing clean test isolation.
$envFile = __DIR__ . '/../.env';
if (file_exists($envFile)) {
    foreach (file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
        $line = trim($line);
        if ($line === '' || str_starts_with($line, '#')) {
            continue;
        }
        if (str_contains($line, '=')) {
            [$key, $value] = explode('=', $line, 2);
            $key   = trim($key);
            $value = trim($value);
            // Only set if not already defined by phpunit.xml
            if (!array_key_exists($key, $_ENV)) {
                $_ENV[$key] = $value;
                putenv("$key=$value");
            }
        }
    }
}

// ─── Force testing environment ────────────────────────────────────────────────
$_ENV['APP_ENV'] = 'testing';
putenv('APP_ENV=testing');
