name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  build-windows:
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v4

      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.25'

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install Wails CLI
        run: go install github.com/wailsapp/wails/v3/cmd/wails3@latest

      - name: Install frontend dependencies
        run: npm install
        working-directory: frontend

      - name: Build frontend
        run: npm run generate
        working-directory: frontend

      - name: Generate bindings
        run: wails3 generate bindings

      - name: Generate syso
        working-directory: build
        run: wails3 generate syso -arch amd64 -icon windows/icon.ico -manifest windows/wails.exe.manifest -info windows/info.json -out ../wails_windows_amd64.syso

      - name: Build
        env:
          CGO_ENABLED: 1
          GOOS: windows
          GOARCH: amd64
        run: go build -tags production -trimpath -buildvcs=false -ldflags="-w -s -H windowsgui" -o bin/gitpilot.exe

      - name: Copy config.yaml
        run: Copy-Item config.yaml -Destination bin/config.yaml

      - name: Remove syso
        run: Remove-Item *.syso

      - name: Install NSIS
        run: choco install nsis -y

      - name: Generate WebView2 Bootstrapper
        run: wails3 generate webview2bootstrapper -dir build/windows/nsis

      - name: Create NSIS Installer
        run: |
          & "C:\Program Files (x86)\NSIS\makensis.exe" -DARG_WAILS_AMD64_BINARY="${{ github.workspace }}\bin\gitpilot.exe" "${{ github.workspace }}\build\windows\nsis\project.nsi"

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: bin/gitpilot-amd64-installer.exe
