name: Build and Upload Canary
on:
  push:
    tags:
      - "v*.*.*"
  workflow_dispatch:
env:
  TWITCH_CLIENT_ID: ${{ env.TWITCH_CLIENT_ID }}

jobs:
  build:
    runs-on: docker
    container:
      image: ghcr.io/catthehacker/ubuntu:js-22.04
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          persist-credentials: "true" # Optional; should be the default

      - name: Checkout lfs
        run: |
          git lfs install --local
          AUTH=$(git config --local http.${{ forge.server_url }}/.extraheader)
          git config --local --unset http.${{ forge.server_url }}/.extraheader
          git config --local http.${{ forge.server_url }}/${{ forge.repository }}.git/info/lfs/objects/batch.extraheader "$AUTH"
          git lfs pull

        # 1. FIX: Install system dependencies BEFORE setup-bun
      - name: Install system dependencies
        run: |
          apt-get update
          apt-get install -y unzip libstdc++6 libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 libxkbcommon0 libxcomposite1 libxdamage1 libxrandr2 libgbm1 libasound2

      - name: Manual Bun Install
        run: |
          # Download and install via official script
          curl -fsSL https://bun.sh/install | bash

          # Manually add Bun to the PATH for subsequent steps
          echo "BUN_INSTALL=$HOME/.bun" >> $GITHUB_ENV
          echo "$HOME/.bun/bin" >> $GITHUB_PATH

          # Force execution permissions just in case
          chmod +x $HOME/.bun/bin/bun

          # Verify it works immediately
          $HOME/.bun/bin/bun --version

      - name: Install dependencies
        run: bun install --frozen-lockfile

      - name: Install 7zip (minimal)
        if: runner.os == 'Linux'
        run: sudo apt-get update && sudo apt-get install -y p7zip-full

      - name: Build Canary
        run: bun run package:Linux
        env:
          TWITCH_CLIENT_ID: ${{ secrets.TWITCH_CLIENT_ID }}

      - name: Upload Artifact
        uses: actions/upload-artifact@v3
        with:
          name: canary-build
          path: build/linux
          retention-days: 7
