name: Windows Setup

on:
  release:
    types: [published]
  workflow_dispatch:

jobs:
  build:
    runs-on: windows-latest
    permissions:
      contents: write
    env:
      VERSION: ${{ github.event.release.tag_name || '0.0' }}

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Copy files to temporary directory
        run: |
          $tempPath = "$env:RUNNER_TEMP\Temp"
          mkdir "$tempPath" | Out-Null

          & "C:\Program Files\7-Zip\7z.exe" x .\.github\build\Windows\PySide6\Python_3.10.11.7z -o"$env:RUNNER_TEMP\Temp"
          xcopy ".\FlashGBX" "$tempPath\Python_3.10.11\Lib\site-packages\FlashGBX\" /s /i /y

          $filesToCopy = @("CHANGES.md", "README.md", "LICENSE", "Third Party Notices.md")
          foreach ($file in $filesToCopy) {
              Copy-Item "$env:GITHUB_WORKSPACE\$file" "$env:RUNNER_TEMP\Temp\" -Force
          }

          if (Test-Path "$tempPath\Python_3.10.11\Lib\site-packages\FlashGBX\config") {
              Remove-Item "$tempPath\Python_3.10.11\Lib\site-packages\FlashGBX\config\*" -Recurse -Force
          }

      - name: Configure Setup
        run: |
          $myAppVersion = "${{ env.VERSION }}"
          $myFilesDir = "$env:RUNNER_TEMP\Temp"
          $myCH341Dir = "$env:GITHUB_WORKSPACE\.github\build\Windows\Drivers\CH341"
          $myOutputDir = "$env:RUNNER_TEMP\Setup"
          mkdir $myOutputDir | Out-Null

          $issFilePath = "$env:GITHUB_WORKSPACE\.github\build\Windows\PySide6\setup.iss"
          (Get-Content $issFilePath) `
            -replace "<APP_VERSION>", "$myAppVersion" `
            -replace "<FILES_DIR>", "$myFilesDir" `
            -replace "<CH341_DIR>", "$myCH341Dir" `
            -replace "<OUTPUT_DIR>", "$myOutputDir" | Set-Content $issFilePath
          $filesToCopy = @("CHANGES.md", "README.md", "LICENSE", "Third Party Notices.md")
          foreach ($file in $filesToCopy) {
              Copy-Item "$env:GITHUB_WORKSPACE\$file" "$myOutputDir" -Force
          }
          Rename-Item -Path "$myFilesDir\FlashGBX.exe" -NewName "FlashGBX-app.exe" -Force

      - name: Install Inno Setup
        run: |
          $innoSetupUrl = "https://www.jrsoftware.org/download.php/is.exe"
          $installerPath = "$env:RUNNER_TEMP\InnoSetupInstaller.exe"
          Invoke-WebRequest -Uri $innoSetupUrl -OutFile $installerPath
          Start-Process -FilePath $installerPath -ArgumentList "/SILENT" -Wait

      - name: Run Inno Setup
        run: |
          $issFilePath = "$env:GITHUB_WORKSPACE\.github\build\Windows\PySide6\setup.iss"
          & "C:\Program Files (x86)\Inno Setup 6\ISCC.exe" $issFilePath

      - name: Create zip archive
        run: |
          $zipFileName = "FlashGBX_${{ env.VERSION }}_Windows_Setup.zip"
          & "C:\Program Files\7-Zip\7z.exe" a -tzip -mx=9 "$env:RUNNER_TEMP\$zipFileName" "$env:RUNNER_TEMP\Setup\*"
          echo "Created zip file: $zipFileName"

      - name: Create artifact
        if: env.VERSION == '0.0'
        uses: actions/upload-artifact@v4
        with:
          name: FlashGBX_Windows_Setup
          path: "${{ runner.temp }}\\FlashGBX_${{ env.VERSION }}_Windows_Setup.zip"

      - name: Upload release asset
        if: env.VERSION != '0.0'
        uses: softprops/action-gh-release@v2.0.8
        with:
          tag_name: ${{ github.event.release.tag_name }}
          files: "${{ runner.temp }}\\FlashGBX_${{ env.VERSION }}_Windows_Setup.zip"
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
