name: Build and Deploy Nightly

on: 
  push:
    branches:
      - dev
  workflow_dispatch :
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

      - name: Upload Artifact
        uses: actions/upload-artifact@v7.0.1
        with:
          name: installer-windows
          path: packages\windows\Output\*.exe 

  build_arch_package:
    name: "Build Arch Packages"
    runs-on: ubuntu-latest
    container:
     image: archlinux:latest
     options: --privileged
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Setup build environment
      run: |
        echo "Starting System Update and installation of build tools"
        pacman -Syu --noconfirm base-devel sudo
        echo "Preparation of builduser"
        useradd -m builduser
        passwd -d builduser
        echo "builduser ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
        #FIXME :  chown: cannot access '/home/runner/work/LCE-Qt-Launcher/LCE-Qt-Launcher': No such file or directorY, Error: Process completed with exit code 1.
        chown -R builduser:builduser ${{ github.workspace }}

    - name: Build Arch Packages
      run: |
        cd ${{ github.workspace }}/packages/linux/nigthly
        sudo -u builduser makepkg -s --noconfirm

    - name: Upload Artifact
      uses: actions/upload-artifact@v7.0.1
      with:
        name: packages-arch
        path: |
          ${{ github.workspace }}/packages/linux/nigthly/lce-qt-launcher-git-*.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.12"
          cache: 'pip'

      - name: Install dependencies
        run: |
         sudo apt update
         sudo apt install -y imagemagick

      - name: Build  AppImage
        run: |
          #FIXME : OSError: [Errno 8] Exec format error: '/tmp/tmpp_9dcsrv/magick'
          python -m pip install --upgrade pip
          pip install . 
          pip install pyproject-appimage
          pyproject-appimage --no-fuse || python -m pyproject_appimage --no-fuse
          
      - 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 installer for Windows."
          files: |
            ./artifacts/*.exe
            ./artifacts/*.zip
            ./artifacts/*.AppImage
            ./artifacts/*.pkg.tar.zst
            ./artifacts/*.tar.gz
