$ErrorActionPreference = 'Stop'

$projectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$envFile = Join-Path $projectRoot '.env.local'

if (-not (Test-Path $envFile)) {
    throw "Missing .env.local at $envFile"
}

Get-Content $envFile | ForEach-Object {
    $line = $_.Trim()
    if (-not $line -or $line.StartsWith('#')) { return }

    $parts = $line -split '=', 2
    if ($parts.Count -ne 2) { return }

    $name = $parts[0].Trim()
    $value = $parts[1]
    [System.Environment]::SetEnvironmentVariable($name, $value, 'Process')
}

Write-Host "Starting QC Text Builder on $env:HTTP_ADDR" -ForegroundColor Green
Set-Location $projectRoot

go run ./cmd/qctextbuilder
