name: Build Zenyx ⚡
on:
  push:
  pull_request:

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        target:
          - x86_64-unknown-linux-gnu
          - aarch64-unknown-linux-gnu
          - x86_64-pc-windows-gnu
          - aarch64-apple-darwin
        include:
          - target: x86_64-unknown-linux-gnu
            binary_name: zenyx-x86_64-linux
            ext: ""
          - target: aarch64-unknown-linux-gnu
            binary_name: zenyx-aarch64-linux
            ext: ""
          - target: x86_64-pc-windows-gnu
            binary_name: zenyx-x86_64-windows.exe
            ext: ".exe"
          - target: aarch64-apple-darwin
            binary_name: zenyx-aarch64-apple
            ext: ""

    steps:
      - name: 📥 Checkout source
        uses: actions/checkout@v4

      - name: 🐳 Setup Docker-in-Docker
        run: |
          # Install Docker using the convenience script
          curl -fsSL https://get.docker.com -o get-docker.sh
          sh get-docker.sh
          
          # Start Docker daemon directly (instead of using service)
          sudo dockerd &
          
          # Give the Docker daemon a moment to initialize
          sleep 10
          
          # Verify Docker is working
          docker info || true
          docker --version

      - name: 🛠️ Install Rust toolchain
        uses: https://github.com/actions-rs/toolchain@v1
        with:
          toolchain: stable
          target: ${{ matrix.target }}
          override: true

      - name: 🧰 Install Cross
        run: cargo install cross --locked

      - name: 🚀 Build with cross
        uses: https://github.com/actions-rs/cargo@v1
        env:
          CROSS_CONTAINER_IN_CONTAINER: true
        with:
          use-cross: true
          command: build
          args: --target ${{ matrix.target }}

      - name: 📦 Package artifact
        run: |
          mkdir -p artifacts
          cp target/${{ matrix.target }}/debug/zenyx${{ matrix.ext }} artifacts/${{ matrix.binary_name }}
          chmod +x artifacts/${{ matrix.binary_name }}

      - name: ⬆️ Upload artifact
        uses: https://code.forgejo.org/forgejo/upload-artifact@v4
        with:
          name: ${{ matrix.binary_name }}
          path: artifacts/${{ matrix.binary_name }}