# ============================================
#  Build Release Single-File EXE for Analog System Monitor
#  Output directory: ./release/
# ============================================

Write-Host "=== Analog System Monitor Release Build ===" -ForegroundColor Cyan

# Ensure script runs from project directory
$project = "analog_system_monitor.csproj"
if (!(Test-Path $project)) {
    Write-Host "Error: Telemetry.csproj not found in this directory." -ForegroundColor Red
    exit 1
}

# Clean previous builds
Write-Host "Cleaning previous build artifacts..."
dotnet clean -c Release

# Publish using settings from the .csproj
Write-Host "Publishing Release build..."
dotnet publish -c Release

# Determine publish folder
$publishDir = Join-Path "bin" "Release\net10.0-windows\win-x64\publish"

if (!(Test-Path $publishDir)) {
    Write-Host "Error: Publish directory not found." -ForegroundColor Red
    exit 1
}

# Custom output directory
$releaseDir = Join-Path (Get-Location) "release"

# Create directory if missing
if (!(Test-Path $releaseDir)) {
    Write-Host "Creating release directory..."
    New-Item -ItemType Directory -Path $releaseDir | Out-Null
}

# Copy all published files to ./release/
Write-Host "Copying published files to ./release/ ..."
Copy-Item -Path "$publishDir\*" -Destination $releaseDir -Recurse -Force

Write-Host "Build completed successfully!" -ForegroundColor Green
Write-Host "Release output: $releaseDir"

# List produced files
Write-Host "`nRelease files:"
Get-ChildItem $releaseDir | Format-Table Name, Length

# Open folder in Explorer
Write-Host "`nOpening release folder..."
Start-Process explorer.exe $releaseDir
