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:
      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 . nuitka
      - 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 ---
  build_windows_installer:
    name: Build Inno Setup
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v6.0.2
      - name: Compile .ISS
        uses: Minionguyjpro/Inno-Setup-Action@v1.2.8
        with:
          path: packages\windows\nigthly.iss
          # HACK : temporaly removed the filename to see if the installer workflow work

      - 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 ---
  build_arch_package:
      name : "Build Arch Packages"
      runs-on: ubuntu-latest
      container:
        image: archlinux
        options: --privileged
      steps: 
      - uses: actions/checkout@v6.0.2
      - name: Build Arch Linux package
        uses: FFY00/build-arch-package@v1
        with:
          PKGBUILD: ${{ github.workspace }}packages/linux/nigthly/PKGBUILD # HACK : test to see if it will work

      - name: Upload Artifact
        uses: actions/upload-artifact@v7.0.1
        with:
          name: packages-arch
          path: |
            $HOME/arch-packages/lce-qt-launcher-git-*.r?.*-?-any.pkg.tar.zst
  
  # --- JOB 4 : Making AppImage ---
  build_appimage:
    name: Build GNU/Linx AppImage
    runs-on: ubuntu-latest
    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 pyproject-appimage
          pip install . 
          python -m pyproject-appimage 

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

  # --- JOB 5 : 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 dev branch \n
            Now includes installers (NOTE : currently, Automatic installer workflow is broken)."
          files: |
            ./artifacts/*.exe
            ./artifacts/*.zip
            ./artifacts/*.AppImage
            ./artifacts/*.pkg.tar.zst
            ./artifacts/*.tar.gz
