name: Windows (x86_64) Nightly Build

on:
  workflow_dispatch:
    inputs:
      release_upload_url:
        description: 'Release upload URL'
        required: true

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build Windows Executable
    runs-on: windows-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 'lts/*'
          cache: 'npm'
          
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        
      - name: Install WebView2
        run: |
          $WebView2InstallPath = "$env:TEMP\MicrosoftEdgeWebview2Setup.exe"
          Invoke-WebRequest "https://go.microsoft.com/fwlink/p/?LinkId=2124703" -OutFile $WebView2InstallPath
          Start-Process -FilePath $WebView2InstallPath -Args "/silent /install" -Wait
          
      - name: Install Dependencies
        run: |
          npm install
          
      - name: Build Frontend
        run: npm run build
        
      - name: Build Windows Executable
        shell: bash  # Force using bash shell for consistent environment variable setting
        run: |
          echo "Building Windows executable..."
          npm run tauri build
          echo "WIN_EXE_PATH=src-tauri/target/release/NeoHtop.exe" >> $GITHUB_ENV
          
      - name: Get version from package.json
        id: version
        shell: bash  # Force using bash shell
        run: echo "version=$(node -p "require('./package.json').version")" >> $GITHUB_OUTPUT
          
      - name: Upload to Release
        if: github.event.inputs.release_upload_url != ''
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
        with:
          upload_url: ${{ github.event.inputs.release_upload_url }}
          asset_path: ${{ env.WIN_EXE_PATH }}
          asset_name: NeoHtop_${{ steps.version.outputs.version }}_x64.exe
          asset_content_type: application/vnd.microsoft.portable-executable