#!/bin/bash
# Build script for Windows NSIS installer from WSL2
# Pure Rust app - no Python dependencies!

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
BUILD_DIR="$PROJECT_ROOT/build"
INSTALLER_DIR="$BUILD_DIR/installer"

echo "========================================"
echo "Chronara Windows NSIS Build Script"
echo "(Pure Rust Edition)"
echo "========================================"
echo ""

# Clean previous build
echo "[1/5] Cleaning previous build..."
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR"
mkdir -p "$INSTALLER_DIR"

# Create model download info (models downloaded on first run)
echo "[2/5] Creating model info for first-run download..."
mkdir -p "$PROJECT_ROOT/src-tauri/resources/models"

cat > "$PROJECT_ROOT/src-tauri/resources/models/download-info.json" << 'MODELINFO'
{
    "llama": {
        "filename": "Llama-3.2-3B-Instruct-Q4_K_M.gguf",
        "url": "https://huggingface.co/bartowski/Llama-3.2-3B-Instruct-GGUF/resolve/main/Llama-3.2-3B-Instruct-Q4_K_M.gguf",
        "size_bytes": 2019377152,
        "description": "Llama 3.2 3B Instruct (Q4_K_M quantization)"
    },
    "whisper": {
        "filename": "ggml-base.en.bin",
        "url": "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base.en.bin",
        "size_bytes": 147964211,
        "description": "Whisper Base English model"
    }
}
MODELINFO
echo "  Created download-info.json"

# Build Tauri app for Windows
echo "[3/5] Building Tauri app for Windows..."
cd "$PROJECT_ROOT"

# Build frontend
echo "  Building frontend..."
pnpm install
pnpm build

# Build Tauri with cargo-xwin for Windows MSVC target
echo "  Building Tauri executable (this may take a while)..."
cd "$PROJECT_ROOT/src-tauri"
cargo xwin build --release --target x86_64-pc-windows-msvc

# Create NSIS installer
echo "[4/5] Creating NSIS installer..."

# Prepare installer staging directory
STAGE_DIR="$INSTALLER_DIR/stage"
mkdir -p "$STAGE_DIR"
mkdir -p "$STAGE_DIR/models"

# Copy the executable
cp "$PROJECT_ROOT/src-tauri/target/x86_64-pc-windows-msvc/release/chronara.exe" "$STAGE_DIR/Chronara.exe"

# Copy model download info
cp "$PROJECT_ROOT/src-tauri/resources/models/download-info.json" "$STAGE_DIR/models/"

# Create NSIS script
cat > "$INSTALLER_DIR/installer.nsi" << 'NSIS_EOF'
!include "MUI2.nsh"
!include "FileFunc.nsh"

SetCompressor /SOLID lzma

Name "Chronara"
OutFile "Chronara-Setup.exe"
InstallDir "$PROGRAMFILES64\Chronara"
InstallDirRegKey HKLM "Software\Chronara" "InstallDir"
RequestExecutionLevel admin

!define MUI_ABORTWARNING
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\modern-install.ico"
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico"

!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES

!insertmacro MUI_LANGUAGE "English"

Section "Install"
    SetOutPath "$INSTDIR"

    ; Install main executable
    File "stage\Chronara.exe"

    ; Install models directory (contains download-info.json)
    SetOutPath "$INSTDIR\models"
    File /r "stage\models\*.*"

    ; Create start menu shortcuts
    CreateDirectory "$SMPROGRAMS\Chronara"
    CreateShortcut "$SMPROGRAMS\Chronara\Chronara.lnk" "$INSTDIR\Chronara.exe"
    CreateShortcut "$SMPROGRAMS\Chronara\Uninstall.lnk" "$INSTDIR\Uninstall.exe"

    ; Create desktop shortcut
    CreateShortcut "$DESKTOP\Chronara.lnk" "$INSTDIR\Chronara.exe"

    ; Write registry keys
    WriteRegStr HKLM "Software\Chronara" "InstallDir" "$INSTDIR"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Chronara" "DisplayName" "Chronara"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Chronara" "UninstallString" "$INSTDIR\Uninstall.exe"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Chronara" "Publisher" "NHCarrigan"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Chronara" "DisplayVersion" "0.1.0"

    ; Get installed size
    ${GetSize} "$INSTDIR" "/S=0K" $0 $1 $2
    IntFmt $0 "0x%08X" $0
    WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Chronara" "EstimatedSize" "$0"

    ; Create uninstaller
    WriteUninstaller "$INSTDIR\Uninstall.exe"
SectionEnd

Section "Uninstall"
    ; Remove files
    RMDir /r "$INSTDIR\models"
    Delete "$INSTDIR\Chronara.exe"
    Delete "$INSTDIR\Uninstall.exe"
    RMDir "$INSTDIR"

    ; Remove shortcuts
    Delete "$SMPROGRAMS\Chronara\Chronara.lnk"
    Delete "$SMPROGRAMS\Chronara\Uninstall.lnk"
    RMDir "$SMPROGRAMS\Chronara"
    Delete "$DESKTOP\Chronara.lnk"

    ; Remove registry keys
    DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Chronara"
    DeleteRegKey HKLM "Software\Chronara"
SectionEnd
NSIS_EOF

# Run NSIS to create installer
echo "[5/5] Running NSIS..."
cd "$INSTALLER_DIR"
makensis installer.nsi

# Move installer to output location
mv "$INSTALLER_DIR/Chronara-Setup.exe" "$BUILD_DIR/"

echo ""
echo "========================================"
echo "Build complete!"
echo "========================================"
echo ""
echo "Output:"
echo "  Installer: $BUILD_DIR/Chronara-Setup.exe"
echo ""
echo "The installer is much smaller now (~15-30MB vs ~500MB+)"
echo "since we're not bundling Python!"
echo ""
echo "First-run behavior:"
echo "  1. User downloads LLaMA model (~2GB) on first use"
echo "  2. User downloads Whisper model (~150MB) on first use"
echo ""
