name: Windows Release

on:
  workflow_dispatch:
  push:
    tags:
      - "v*"

permissions:
  contents: write

jobs:
  build-windows-release:
    runs-on: windows-latest

    steps:
      - name: Check out repository
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Cache Rust build outputs
        uses: Swatinem/rust-cache@v2

      - name: Cache voice model assets
        uses: actions/cache@v4
        with:
          path: .hematite/assets/voice
          key: kokoro-voice-assets-v1.0

      - name: Download voice model assets
        shell: powershell
        run: |
          $assetsDir = ".hematite\assets\voice"
          New-Item -ItemType Directory -Force -Path $assetsDir | Out-Null
          $base = "https://github.com/thewh1teagle/kokoro-onnx/releases/download/model-files-v1.0"
          if (-not (Test-Path "$assetsDir\kokoro-v1.0.onnx")) {
            Invoke-WebRequest -Uri "$base/kokoro-v1.0.onnx" -OutFile "$assetsDir\kokoro-v1.0.onnx" -TimeoutSec 300
          }
          if (-not (Test-Path "$assetsDir\voices.bin")) {
            Invoke-WebRequest -Uri "$base/voices-v1.0.bin" -OutFile "$assetsDir\voices.bin" -TimeoutSec 120
          }

      - name: Install Inno Setup
        shell: powershell
        run: choco install innosetup -y --no-progress

      - name: Build portable bundle and installer
        shell: powershell
        run: powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\package-windows.ps1 -Installer

      - name: Upload packaged artifacts
        uses: actions/upload-artifact@v4
        with:
          name: hematite-windows
          path: |
            dist/windows/*.zip
            dist/windows/*-Setup.exe

      - name: Publish GitHub release assets
        if: startsWith(github.ref, 'refs/tags/')
        uses: softprops/action-gh-release@v2
        with:
          files: |
            dist/windows/*.zip
            dist/windows/*-Setup.exe
