<#
  release.ps1 — one-shot Windows release for YT Player.

  Builds the Tauri (WebView2) app on Windows, copies the installers into
  .\releases, then commits and pushes through WSL git (as requested).

  Run from a *native Windows* PowerShell (not WSL) inside the repo:

      pwsh -File scripts\release.ps1
      # or:  powershell -ExecutionPolicy Bypass -File scripts\release.ps1

  Prereqs (one time): Rust (MSVC), Microsoft C++ Build Tools, Node.js,
  WebView2 runtime (preinstalled on Win11).
#>

$ErrorActionPreference = "Stop"
$root = Split-Path -Parent $PSScriptRoot
Set-Location $root

function Step($m) { Write-Host "==> $m" -ForegroundColor Cyan }

Step "Installing npm deps + Tauri CLI"
npm install

Step "Fetching yt-dlp.exe into .\bin"
npm run setup

if (-not (Test-Path "src-tauri\icons\icon.ico")) {
  Step "Generating app icons"
  npm run make-icon
  npm run tauri icon .\appicon.png
}

Step "Building Windows app (npm run tauri:build)"
npm run tauri:build

Step "Collecting artifacts into .\releases"
New-Item -ItemType Directory -Force -Path releases | Out-Null
$bundle = "src-tauri\target\release\bundle"
Get-ChildItem "$bundle\nsis\*.exe" -ErrorAction SilentlyContinue | Copy-Item -Destination releases -Force
Get-ChildItem "$bundle\msi\*.msi"  -ErrorAction SilentlyContinue | Copy-Item -Destination releases -Force
Copy-Item "src-tauri\target\release\ytplayer.exe" releases -Force -ErrorAction SilentlyContinue
Get-ChildItem releases | Format-Table Name, Length -AutoSize

Step "Committing + pushing via WSL git"
$msg = "Offline cache + Save/Add-to-playlist + Settings page; refresh Windows release"
# Resolve the repo's path inside WSL and run git there.
$wslPath = (wsl wslpath -a "$($root -replace '\\','/')") 2>$null
if (-not $wslPath) { $wslPath = "~/development/personal/ytplayer" }
wsl bash -lc "cd '$wslPath' && git add -A && git commit -m '$msg' && git push"

Write-Host "Done — installers are in .\releases and changes are pushed." -ForegroundColor Green
