name: MoviePilot Windows Builder
on:
  workflow_dispatch:
  push:
    branches:
      - main
    paths:
      - version.py

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

      - name: Release Version
        id: release_version
        run: |
          $app_version = Select-String -Path "version.py" -Pattern "APP_VERSION\s=\s'v(.*)'" | ForEach-Object { $_.Matches.Groups[1].Value }
          $env:GITHUB_ENV += "app_version=$app_version"

      - name: Init Python 3.11.4
        uses: actions/setup-python@v4
        with:
          python-version: '3.11.4'

      - name: Install Dependent Packages
        run: |
          python -m pip install --upgrade pip
          pip install wheel pyinstaller
          pip install -r requirements.txt
        shell: pwsh

      - name: Pyinstaller
        run: |
          pyinstaller windows.spec
        shell: pwsh

      - name: Upload Windows File
        uses: actions/upload-artifact@v3
        with:
          name: windows
          path: dist/MoviePilot.exe

      - name: Generate Release
        id: generate_release
        uses: actions/create-release@latest
        with:
          tag_name: v${{ env.app_version }}
          release_name: v${{ env.app_version }}
          body: ${{ github.event.commits[0].message }}
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload Release Asset
        uses: dwenegar/upload-release-assets@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          release_id: ${{ steps.generate_release.outputs.id }}
          assets_path: |
            dist/MoviePilot.exe
