# Load key=value pairs from repo .env (no external dependency)
function Get-SovereignEnvMap {
    param([string]$RepoRoot = (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent))
    $envFile = Join-Path $RepoRoot '.env'
    $map = @{}
    if (-not (Test-Path $envFile)) { return $map }
    Get-Content $envFile | ForEach-Object {
        $line = $_.Trim()
        if ($line -match '^\s*#' -or [string]::IsNullOrWhiteSpace($line)) { return }
        if ($line -match '^\s*([^=]+)=(.*)$') {
            $map[$matches[1].Trim()] = $matches[2].Trim()
        }
    }
    return $map
}

function Get-SovereignRepositoryConnectionString {
    param([string]$Override)
    if ($Override) { return $Override }
    $map = Get-SovereignEnvMap
    if ($map['SOVEREIGN_DB_CONNECTION']) { return $map['SOVEREIGN_DB_CONNECTION'] }
    return $null
}
