<#
.SYNOPSIS
    Builds a Flutter Windows installer with versioning support.

.DESCRIPTION
    This script builds a Flutter Windows application and creates an installer using Inno Setup.

.PARAMETER buildType
    The build type (release or debug). Default is "release".

.PARAMETER buildName
    The build name (version). Default is "0.0.0".

.EXAMPLE
    .\build-windows-installer.ps1 -buildType debug -buildName 1.0.0
    Builds a debug Windows application with version 1.0.0.
#>

[CmdletBinding()]
param (
    [string]$buildType = "release",  # Default value is "release"
    [string]$buildName = "0.0.0"    # Default value is "0.0.0"
)

$buildNumber = "$(Get-Date -Format 'yyyyMMddHHmmss')"

# Define the file path and the new version value
$issTplFile = "./busylight-buddy-windows-installer-builder.iss.tpl"
$issFile = "./busylight-buddy-windows-installer-builder.iss"

cd $PSScriptRoot

# Build an array for arguments
$flutterArgs = @(
    "--$buildType",
    "--build-name=$buildName",
    "--build-number=$buildNumber"
)

Write-Output "Building Windows application with arguments: $($flutterArgs -join ' ')"

# Build the Windows application using Flutter
flutter build windows @flutterArgs

# Build the Windows installer using Inno Setup Compiler (ISCC.exe)
# Read the content of Inno Setup template file
$content = Get-Content -Path $issTplFile -Raw

# Replace the placeholder with the new version value
$updatedContent = $content -replace '%%MyAppVersion%%', $buildName

# Write the updated content back to Inno Setup file
$updatedContent | Set-Content -Path $issFile

ISCC.exe $issFile