name: Build and Deploy Nightly

on:
  push:
    branches:
      - dev

permissions:
  contents: write
 
jobs:
  # --- JOB 1 : NUITKA COMPILATION (Windows/Linux) ---
  build:
    name: Build Nuitka Binaries
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v6.0.2
      - uses: actions/setup-python@v6.2.0
        with:
          python-version: "3.12"
          cache: 'pip'
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install . hatch zstandard
      - name: Build with Nuitka
        run: hatch run python -m nuitka --standalone --assume-yes-for-download --output-dir=dist src/lce_qt_launcher/main.py
      
      - name: Package Windows
        if: matrix.os == 'windows-latest'
        shell: pwsh
        run: Compress-Archive -Path dist/main.dist/* -DestinationPath LCE-Qt-Launcher-Windows.zip

      - name: Package Linux
        if: matrix.os == 'ubuntu-latest'
        run: tar -czf LCE-Qt-Launcher-Linux.tar.gz -C dist/main.dist .

      - name: Upload Artifact
        uses: actions/upload-artifact@v7.0.1
        with:
          name: binaries-${{ matrix.os }}
          path: |
            *.zip
            *.tar.gz

  # # --- JOB 2 : Building Windows Installer ---
  # # Disabled due to not working
  # build_windows_installer:
  #   name: Build Inno Setup
  #   runs-on: windows-latest
  #   steps:
  #     - uses: actions/checkout@v4 # test to see if version 4 work, verfify if update coudld be fixed or needed
  #     - name: Compile .ISS
  #       uses: Minionguyjpro/Inno-Setup-Action@v1.2.8
  #       with:
  #         path: packages\windows\setup-nigthly.iss
  #         options: '/O+ /F"LCE_Qt_Launcher_Windows_Nigthly_Setup"'
      
  #     - name: Upload Artifact
  #       uses: actions/upload-artifact@v7.0.1
  #       with:
  #         name: installer-windows
  #         path: packages\windows\Output\*.exe 

  # # --- JOB 3 : Making Arch Linux Packages ---
  # # Disabled due to not working
  # build_arch_installer:
  #   name: Build Arch Package
  #   runs-on: ubuntu-latest
  #   steps:
  #     - uses: actions/checkout@v6.0.2
  #     - name: Validate and Build package
  #       uses: heyhusen/archlinux-package-action@v3.0.0
  #       with:
  #         namcap: true
  #         updpkgsums : true
  #         path : "packages/linux/nigthly"
      
  #     - name: Upload Artifact
  #       uses: actions/upload-artifact@v7.0.1
  #       with:
  #         name: packages-arch
  #         path: |
  #           packages/linux/nigthly/*
  #           packages/linux/nigthly/lce-qt-launcher-git-*.r?.*-?-any.pkg.tar.zst
  #           !packages/linux/nigthly/pkg/
  #           !packages/linux/nigthly/src/
  #           !packages/linux/nigthly/lce-qt-launcher-git/

  # --- JOB 4 : Publish the Nightly Release ---
  release:
    name: Create Nightly Release
    needs: [build]
    runs-on: ubuntu-latest
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v8.0.1
        with:
          path: ./artifacts
          merge-multiple: true

      - name: Upload Nightly
        uses: andelf/nightly-release@main
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: nightly
          name: Nightly Release
          body: "
            > [!NOTE]\n
            > The nightly prebuild is only available for GNU/Linux and Windows. \n
            > For NixOS/Nixpkg you need to build it yourself by downloading the source files 
            and then run `./nix-helper install` \n
            > For FreeBSD, check [my FreeBSD Port Overlay collection](https://github.com/xgui4/freebsd-ports/).
            Note that the Nightly build is not available as a port right now.\n
            \n
            Automatic Nighty (daily) binary build powered by GtiHub Actions. \n
            The nigthly build are experimental nuitka compilation from the nighly branch \n
            Now includes installers (NOTE : currently, Automatic installer workflow is broken)."
          files: |
            ./artifacts/****.exe
            ./artifacts/****.zip
            ./artifacts/****.pkg.tar.zst
            ./artifacts/****.tar.gz
