# ============================================================
# 🌀 FilterCair Server – Deploy Script
# ------------------------------------------------------------
# Builds the project for Linux, zips it, and deploys to Azure
# ------------------------------------------------------------
# Marc Wieland - 2025-11-04
# ============================================================

# Stop on first error
$ErrorActionPreference = "Stop"

# Variables
$projectPath = "C:\DEVQPDC\Masterarbeit\filtercair_dev\Masterarbeit_New\FilterCair.Server"
$publishFolder = "$projectPath\bin\Release\net9.0\linux-x64\publish"
$zipFile = "$projectPath\publish.zip"
$resourceGroup = "FilterCair-RG"
$appName = "filtercair-api"

Write-Host "Starting deploy for $appName..." -ForegroundColor Cyan

# 1️⃣ Build the project
Write-Host "Publishing project for Linux (Release mode)..."
dotnet publish $projectPath -c Release -r linux-x64 --self-contained false

# 2️⃣ Create ZIP
if (Test-Path $zipFile) {
    Remove-Item $zipFile -Force
}
Write-Host "Compressing published output..."
Compress-Archive -Path "$publishFolder\*" -DestinationPath $zipFile -Force

# 3️⃣ Deploy to Azure
Write-Host "Deploying to Azure WebApp ($appName)..."
az webapp deploy `
    --resource-group $resourceGroup `
    --name $appName `
    --src-path $zipFile `
    --type zip

if ($LASTEXITCODE -eq 0) {
    Write-Host "Deployment successful! Opening site..." -ForegroundColor Green
    Start-Process "https://$appName.azurewebsites.net/swagger"
} else {
    Write-Host "Deployment failed. Check Azure Portal logs for details." -ForegroundColor Red
}

Write-Host "Done!"
