# Full release: prepare (build Win/Linux, copy) then publish (validate + upload).
# Run: release-all.cmd  (default: -AfterMac, version already set, Mac files in release folder)
#      release-all-bump.cmd  (bump version first, Mac later)

param(
  [string]$Version = '',
  [switch]$Bump,
  [switch]$AfterMac,
  [switch]$Minor,
  [switch]$SkipGit,
  [switch]$SkipLinux,
  [switch]$NoBump,
  [switch]$CheckOnlyPublish,
  [switch]$SkipMac
)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

$ToolDir = $PSScriptRoot
$PrepareScript = Join-Path $ToolDir 'prepare-release.ps1'
$PublishScript = Join-Path $ToolDir 'publish.ps1'

if (-not (Test-Path -LiteralPath $PrepareScript)) {
  throw "Missing prepare-release.ps1: $PrepareScript"
}
if (-not (Test-Path -LiteralPath $PublishScript)) {
  throw "Missing publish.ps1: $PublishScript"
}

$useAfterMac = $AfterMac
if ($Bump -and $AfterMac) {
  throw 'Use either -Bump or -AfterMac, not both'
}
if (-not $Bump -and -not $PSBoundParameters.ContainsKey('AfterMac')) {
  $useAfterMac = $true
}

$prepareArgs = @()
if ($Version) { $prepareArgs += '-Version', $Version }
if ($Bump) { $prepareArgs += '-Bump' }
if ($useAfterMac) { $prepareArgs += '-AfterMac' }
if ($Minor) { $prepareArgs += '-Minor' }
if ($SkipGit) { $prepareArgs += '-SkipGit' }
if ($SkipLinux) { $prepareArgs += '-SkipLinux' }
if ($NoBump) { $prepareArgs += '-NoBump' }

$publishArgs = @()
if ($CheckOnlyPublish) { $publishArgs += '-CheckOnly' }
if ($SkipMac) { $publishArgs += '-SkipMac' }

Write-Host '=== TTRPG Full Release ===' -ForegroundColor Cyan
Write-Host ''

Write-Host '>>> Phase 1/2: Prepare release' -ForegroundColor Yellow
& powershell -NoProfile -ExecutionPolicy Bypass -File $PrepareScript @prepareArgs
if ($LASTEXITCODE -ne 0) {
  throw "Prepare release failed (exit $LASTEXITCODE). Publish not started."
}

Write-Host ''
Write-Host '>>> Phase 2/2: Publish to server' -ForegroundColor Yellow
& powershell -NoProfile -ExecutionPolicy Bypass -File $PublishScript @publishArgs
if ($LASTEXITCODE -ne 0) {
  throw "Publish failed (exit $LASTEXITCODE)"
}

Write-Host ''
Write-Host '=== Full release completed ===' -ForegroundColor Green
exit 0
