name: Build and Deploy Nightly

on:
  push:
    branches:
      - nightly

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 . 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 ---
  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\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 ---
  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"
          srcinfo: true
      
      - name: Upload Artifact
        uses: actions/upload-artifact@v7.0.1
        with:
          name: packages-arch
          path: packages/linux/nightly/****.pkg.tar.zst

  # --- 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 avaiable 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 port, you will to build the port yourself, see the README for more info. \n
            > Flatpak now in developpement ! Soon there will be a flatpak repo which will be show here and on the readme on how to enabled it..\n 
            \n
            Automatic Nighty (daily) binary build powered by GtiHub Actions. \n
            The nighy build are experimental nuitka compilation from the nighly branch \n
            Now includes installers (Note : Windows installer may be moved to each own release soon)!"
          files: |
            ./artifacts/****.exe
            ./artifacts/****.zip
            ./artifacts/****.pkg.tar.zst
            ./artifacts/****.tar.gz