name: Nightly Build

on:
  schedule:
    - cron: '0 */8 * * *' # Every 8 hours
  workflow_dispatch: # Manual trigger for the rizz

permissions:
  contents: write

jobs:
  tag:
    name: Create Nightly Tag
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    steps:
      - uses: actions/checkout@v4
        with:
          ref: main
          fetch-depth: 0
      - name: Update Nightly Tag
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git tag -f nightly
          git push -f origin nightly

  dist-build:
    name: Build Binaries
    needs: tag
    if: github.ref == 'refs/heads/main'
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: x86_64-pc-windows-msvc
            os: windows-latest
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      
      - name: Build Workspace
        shell: bash
        run: cargo build --release --workspace

      - name: Fetch Micro HBA
        shell: bash
        run: |
          HBA_MICRO_LATEST_URL="${{ vars.HBA_MICRO_LATEST_URL }}" \
            bash .github/scripts/fetch_micro_hba.sh

      - name: Prepare Release Bundle
        shell: bash
        run: |
          mkdir -p staging/dats
          mkdir -p staging/scripts
          EXT=""
          if [[ "${{ matrix.os }}" == "windows-latest" ]]; then EXT=".exe"; fi
          cp target/release/tui$EXT staging/holtburger-cli$EXT
          cp target/release/dat2hba$EXT staging/dat2hba$EXT
          cp target/release/dat-tool$EXT staging/dat-tool$EXT || true
          cp dats/assets.hba staging/dats/assets.hba
          cp scripts/*.js scripts/*.data.json scripts/*.data.bin staging/scripts/
          ARCHIVE_NAME="holtburger-nightly-${{ matrix.target }}"
          if [[ "${{ matrix.os }}" == "windows-latest" ]]; then
            7z a "$ARCHIVE_NAME.zip" ./staging/*
            echo "ASSET_PATH=$ARCHIVE_NAME.zip" >> $GITHUB_ENV
          else
            tar -czf "$ARCHIVE_NAME.tar.gz" -C staging .
            echo "ASSET_PATH=$ARCHIVE_NAME.tar.gz" >> $GITHUB_ENV
          fi

      - name: Upload Build Artifact
        uses: actions/upload-artifact@v4
        with:
          name: binary-${{ matrix.target }}
          path: ${{ env.ASSET_PATH }}

  flatpak-build:
    name: Build Flatpak
    needs: tag
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
      - name: Install Flatpak dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y flatpak flatpak-builder
          sudo flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo
          sudo flatpak install -y flathub org.freedesktop.Platform/x86_64/24.08 org.freedesktop.Sdk/x86_64/24.08
      - name: Build TUI Client
        run: |
          cargo build --release --package holtburger-cli
          mv target/release/tui target/release/holtburger-cli
      - name: Fetch Micro HBA
        shell: bash
        run: |
          HBA_MICRO_LATEST_URL="${{ vars.HBA_MICRO_LATEST_URL }}" \
            bash .github/scripts/fetch_micro_hba.sh
      - uses: flatpak/flatpak-github-actions/flatpak-builder@v6
        with:
          bundle: holtburger-cli.flatpak
          manifest-path: apps/holtburger-cli/dist/io.github.merklejerk.holtburger-cli.yaml
          cache: true
      - name: Upload Flatpak Artifact
        uses: actions/upload-artifact@v4
        with:
          name: flatpak-bundle
          path: holtburger-cli.flatpak

  micro-hba-assets:
    name: Publish Micro HBA Assets
    needs: tag
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Fetch Micro HBA Bundle
        shell: bash
        run: |
          HBA_MICRO_LATEST_URL="${{ vars.HBA_MICRO_LATEST_URL }}" \
          HBA_MICRO_KEEP_ARCHIVE=1 \
            bash .github/scripts/fetch_micro_hba.sh
      - name: Upload Micro HBA Release Assets
        uses: actions/upload-artifact@v4
        with:
          name: micro-hba-assets
          path: |
            micro-hba.zip
            dats/assets.hba

  publish:
    name: Publish Nightly Release
    needs: [dist-build, flatpak-build, micro-hba-assets]
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    steps:
      - uses: actions/checkout@v4
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts
          merge-multiple: true # This puts all files into the 'artifacts' dir

      - name: Upload to Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: nightly
          files: artifacts/*
          prerelease: true
          name: "Nightly Build (Live on the edge 🍔)"
          body: |
            This is an automated nightly build of the Holtburger TUI client.
            
            **Warning:** These builds are highly experimental!
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
