//! NSIS installer script template.
//!
//! Contains the handlebars template used to generate NSIS installer scripts
//! with Modern UI, version info, and installation/uninstallation logic.

/// NSI script template using Handlebars syntax.
///
/// This template generates a complete NSIS installer script with:
/// - Modern UI wizard interface
/// - Configurable compression algorithms
/// - Multiple architecture support
/// - Per-user or per-machine installation modes
/// - Start Menu shortcuts
/// - Windows registry integration
/// - Full uninstaller support
pub const NSI_TEMPLATE: &str = r#"
; NSIS Installer Script
; Generated by kodegen_release

!define PRODUCT_NAME "{{product_name}}"
!define PRODUCT_VERSION "{{version}}"
!define PRODUCT_PUBLISHER "{{publisher}}"
!define INSTALL_DIR "{{install_dir}}"
!define BINARY_NAME "{{binary_name}}"
!define ARCH "{{arch}}"

; Compression
!if "{{compression}}" == "none"
  SetCompress off
!else
  SetCompressor /SOLID "{{compression}}"
!endif

; Modern UI
!include "MUI2.nsh"
!include "x64.nsh"
!include "FileFunc.nsh"

; Custom Branding Images
{{#if header_image}}
!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_BITMAP "{{header_image}}"
{{/if}}

{{#if sidebar_image}}
!define MUI_WELCOMEFINISHPAGE_BITMAP "{{sidebar_image}}"
{{/if}}

{{#if installer_icon}}
!define MUI_ICON "{{installer_icon}}"
{{/if}}

; General Settings
Name "${PRODUCT_NAME}"
OutFile "${OUTPUT_FILE}"
InstallDir "${INSTALL_DIR}"

; Request admin for perMachine, user for currentUser
!if "{{install_mode}}" == "perMachine"
  RequestExecutionLevel admin
!else if "{{install_mode}}" == "currentUser"
  RequestExecutionLevel user
!else
  RequestExecutionLevel admin
!endif

; Interface Settings
!define MUI_ABORTWARNING
{{#unless installer_icon}}
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\modern-install.ico"
{{/unless}}
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico"

; Pages
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

; Uninstaller Pages
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES

; Languages
!insertmacro MUI_LANGUAGE "English"

; Version Info
VIProductVersion "{{version_nsis}}"
VIAddVersionKey "ProductName" "${PRODUCT_NAME}"
VIAddVersionKey "ProductVersion" "${PRODUCT_VERSION}"
VIAddVersionKey "CompanyName" "${PRODUCT_PUBLISHER}"
VIAddVersionKey "FileDescription" "${PRODUCT_NAME} Installer"
VIAddVersionKey "FileVersion" "${PRODUCT_VERSION}"

; Installation Section
Section "MainSection" SEC01
  SetOutPath "$INSTDIR"
  
  ; Copy all binaries
  {{#each binary_files}}
  File "{{this}}"
  {{/each}}
  
  ; Create uninstaller
  WriteUninstaller "$INSTDIR\Uninstall.exe"
  
  ; Create Start Menu shortcuts
  CreateDirectory "$SMPROGRAMS\${PRODUCT_NAME}"
  CreateShortcut "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk" \
    "$INSTDIR\${BINARY_NAME}.exe"
  CreateShortcut "$SMPROGRAMS\${PRODUCT_NAME}\Uninstall.lnk" \
    "$INSTDIR\Uninstall.exe"
  
  ; Write registry uninstall information
  !if "{{install_mode}}" == "perMachine"
    !define UNINST_ROOT "HKLM"
  !else
    !define UNINST_ROOT "HKCU"
  !endif
  
  WriteRegStr ${UNINST_ROOT} \
    "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" \
    "DisplayName" "${PRODUCT_NAME}"
  WriteRegStr ${UNINST_ROOT} \
    "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" \
    "DisplayVersion" "${PRODUCT_VERSION}"
  WriteRegStr ${UNINST_ROOT} \
    "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" \
    "Publisher" "${PRODUCT_PUBLISHER}"
  WriteRegStr ${UNINST_ROOT} \
    "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" \
    "UninstallString" "$INSTDIR\Uninstall.exe"
  WriteRegStr ${UNINST_ROOT} \
    "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" \
    "InstallLocation" "$INSTDIR"
  
  ; Estimated size in KB
  ${GetSize} "$INSTDIR" "/S=0K" $0 $1 $2
  IntFmt $0 "0x%08X" $0
  WriteRegDWORD ${UNINST_ROOT} \
    "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" \
    "EstimatedSize" "$0"
SectionEnd

; Uninstaller Section
Section "Uninstall"
  ; Remove uninstaller
  Delete "$INSTDIR\Uninstall.exe"

  ; Remove all installed binaries
  Delete "$INSTDIR\${BINARY_NAME}.exe"
  
  ; Remove Start Menu shortcuts
  Delete "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk"
  Delete "$SMPROGRAMS\${PRODUCT_NAME}\Uninstall.lnk"
  RMDir "$SMPROGRAMS\${PRODUCT_NAME}"
  
  ; Remove registry keys
  !if "{{install_mode}}" == "perMachine"
    DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
  !else
    DeleteRegKey HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
  !endif
  
  ; Remove installation directory
  RMDir "$INSTDIR"
SectionEnd
"#;
