# summary_helper.ps1 - Handles build timing and summary reporting cleanly
param(
    [string]$Action
)

if ($Action -eq "Start") {
    if (-not (Test-Path "Intermediate")) { New-Item -ItemType Directory -Path "Intermediate" | Out-Null }
    [System.IO.File]::WriteAllText("Intermediate\build_start.tmp", [System.DateTime]::Now.Ticks.ToString())
}
elseif ($Action -eq "Finish") {
    $elapsedStr = "00:00:00.000"
    if (Test-Path "Intermediate\build_start.tmp") {
        $startStr = Get-Content "Intermediate\build_start.tmp" -ErrorAction SilentlyContinue
        if ($startStr) {
            $startTicks = [long]$startStr
            $elapsed = New-Object System.TimeSpan ([System.DateTime]::Now.Ticks - $startTicks)
            $elapsedStr = "{0:D2}:{1:D2}:{2:D2}.{3:D3}" -f $elapsed.Hours, $elapsed.Minutes, $elapsed.Seconds, $elapsed.Milliseconds
        }
        Remove-Item "Intermediate\build_start.tmp" -ErrorAction SilentlyContinue
    }
    Write-Host "Total Duration : $elapsedStr"
    Write-Host "Output Binaries:"
    if (Test-Path "Intermediate\built_outputs.tmp") {
        $files = Get-Content "Intermediate\built_outputs.tmp" -ErrorAction SilentlyContinue | Where-Object { Test-Path $_ }
        if ($files) {
            foreach ($f in $files) {
                $item = Get-Item $f
                $size = "{0:N2} MB" -f ($item.Length / 1MB)
                $relPath = $item.FullName.Replace((Get-Location).Path + "\", "")
                Write-Host "  - $relPath ($size)"
            }
        } else {
            Write-Host "  - None built in this session (all requested targets up-to-date)"
        }
        Remove-Item "Intermediate\built_outputs.tmp" -ErrorAction SilentlyContinue
    } else {
        Write-Host "  - None built in this session (all requested targets up-to-date)"
    }
}
