
[CmdletBinding()]
param(
    [string]$Version,
    [switch]$SkipFlutterBuild
)

$ErrorActionPreference = 'Stop'

$ProjectRoot = Split-Path -Parent $PSScriptRoot
Set-Location $ProjectRoot
. (Join-Path $PSScriptRoot 'version-common.ps1')
Write-Host "==> Project root: $ProjectRoot" -ForegroundColor Cyan

if (-not $Version) {
    $Version = Get-SmPlayerCurrentVersion -Platform windows -ProjectRoot $ProjectRoot
}
Write-Host "==> Version: $Version" -ForegroundColor Cyan

$IsccCandidates = @(
    "$Env:ProgramFiles\Inno Setup 6\ISCC.exe",
    "${Env:ProgramFiles(x86)}\Inno Setup 6\ISCC.exe",
    "$Env:LOCALAPPDATA\Programs\Inno Setup 6\ISCC.exe",
    "$Env:ProgramFiles\Inno Setup 5\ISCC.exe",
    "${Env:ProgramFiles(x86)}\Inno Setup 5\ISCC.exe",
    "$Env:LOCALAPPDATA\Programs\Inno Setup 5\ISCC.exe"
)
$Iscc = $IsccCandidates | Where-Object { Test-Path $_ } | Select-Object -First 1
if (-not $Iscc) {
    $cmd = Get-Command iscc.exe -ErrorAction SilentlyContinue
    if ($cmd) { $Iscc = $cmd.Source }
}
if (-not $Iscc) {
    Write-Host ""
    Write-Host "[X] ISCC.exe not found." -ForegroundColor Red
    Write-Host "    Install: https://jrsoftware.org/isdl.php" -ForegroundColor Yellow
    Write-Host "    Or:      winget install -e --id JRSoftware.InnoSetup" -ForegroundColor Yellow
    exit 1
}
Write-Host "==> ISCC: $Iscc" -ForegroundColor Cyan

if (-not $SkipFlutterBuild) {
    Write-Host '==> flutter pub get' -ForegroundColor Cyan
    & flutter pub get
    if ($LASTEXITCODE -ne 0) { throw "flutter pub get failed (exit $LASTEXITCODE)" }

    $prepareFvpScript = Join-Path $PSScriptRoot 'prepare-fvp-windows-deps.ps1'
    & powershell -ExecutionPolicy Bypass -File $prepareFvpScript
    if ($LASTEXITCODE -ne 0) {
        throw "Failed to prepare pinned fvp Windows dependencies (exit $LASTEXITCODE)"
    }

    $flutterBuildArgs = @('build', 'windows', '--release', '--build-name', $Version)
    $flutterBuildArgs = Add-SmPlayerVersionDartDefine -Arguments $flutterBuildArgs -Version $Version

    Write-Host "==> flutter $($flutterBuildArgs -join ' ')" -ForegroundColor Cyan
    $Env:FVP_DEPS_LATEST = $null
    & flutter @flutterBuildArgs
    if ($LASTEXITCODE -ne 0) { throw "flutter build failed (exit $LASTEXITCODE)" }
} else {
    Write-Host "==> Skipping flutter build (--SkipFlutterBuild)" -ForegroundColor Yellow
}

$ReleaseDir = Join-Path $ProjectRoot "build\windows\x64\runner\Release"
$Exe        = Join-Path $ReleaseDir "smplayer.exe"
if (-not (Test-Path $Exe)) {
    throw "Build artifact not found: $Exe (re-run without -SkipFlutterBuild)"
}

$Iss        = Join-Path $ProjectRoot "installer\sm_emby.iss"
$OutputDir  = Join-Path $ProjectRoot "build\installer"
New-Item -ItemType Directory -Force -Path $OutputDir | Out-Null

Write-Host "==> Building installer" -ForegroundColor Cyan
& $Iscc "/DAppVersion=$Version" $Iss
if ($LASTEXITCODE -ne 0) { throw "ISCC failed (exit $LASTEXITCODE)" }

$Setup = Join-Path $OutputDir "smplayer-setup-$Version-x64.exe"
if (Test-Path $Setup) {
    $size = (Get-Item $Setup).Length / 1MB
    Write-Host ""
    Write-Host "[OK] Installer generated:" -ForegroundColor Green
    Write-Host "     $Setup" -ForegroundColor Green
    Write-Host ("     Size: {0:N2} MB" -f $size) -ForegroundColor Green
} else {
    Write-Host "[!] Expected artifact missing, check $OutputDir" -ForegroundColor Yellow
}
