name: Build

on:
  push:
    branches:
      - main
      - master
    tags:
      - 'v*'
  pull_request:
  workflow_dispatch:

permissions:
  contents: write

jobs:
  linux:
    name: Linux ${{ matrix.goarch }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        goarch:
          - amd64
          - arm64

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

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm
          cache-dependency-path: |
            frontend/package-lock.json
            docs/package-lock.json

      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.24.5'
          cache-dependency-path: backend/go.sum

      - name: Install frontend dependencies
        run: npm ci --prefix frontend

      - name: Install docs dependencies
        run: npm ci --prefix docs

      - name: Build docs
        run: npm run build --prefix docs

      - name: Set version from tag
        run: |
          if [[ "$GITHUB_REF" == refs/tags/v* ]]; then
            echo "CLICD_VERSION=${GITHUB_REF#refs/tags/v}" >> "$GITHUB_ENV"
          else
            echo "CLICD_VERSION=dev" >> "$GITHUB_ENV"
          fi

      - name: Build CLICD
        env:
          CLICD_GOARCH: ${{ matrix.goarch }}
        run: bash build.sh

      - name: Package CLICD
        env:
          CLICD_GOARCH: ${{ matrix.goarch }}
        run: |
          asset_dir="clicd-linux-${CLICD_GOARCH}"
          mkdir -p "dist" "package/${asset_dir}"
          cp build/clicd "package/${asset_dir}/clicd"
          cp build/install.sh "package/${asset_dir}/install.sh"
          chmod +x "package/${asset_dir}/clicd" "package/${asset_dir}/install.sh"
          tar -C package -czf "dist/${asset_dir}.tar.gz" "${asset_dir}"
          cp build/clicd "dist/${asset_dir}"

      - name: Package Mofang module
        run: |
          if [ ! -f Mofang/clicd.php ]; then
            echo "Mofang module not present; skipping package."
            exit 0
          fi

          if ! command -v zip >/dev/null 2>&1; then
            sudo apt-get update
            sudo apt-get install -y zip
          fi

          cd Mofang
          zip -r ../dist/clicd-mofang.zip clicd.php handlers templates -x '*.DS_Store' -x '*/.DS_Store'

      - name: Generate checksums
        run: sha256sum dist/* > dist/SHA256SUMS

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: clicd-linux-${{ matrix.goarch }}
          path: dist/*

  release:
    name: Publish GitHub Release
    needs: linux
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')

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

      - name: Download artifacts
        uses: actions/download-artifact@v4
        with:
          path: dist-artifacts

      - name: Prepare release assets
        run: |
          mkdir -p dist
          find dist-artifacts -maxdepth 2 -type f ! -name SHA256SUMS -print -exec cp -f {} dist/ \;
          sha256sum dist/* > dist/SHA256SUMS

      - name: Publish GitHub Release
        env:
          GH_TOKEN: ${{ github.token }}
          GH_REPO: ${{ github.repository }}
        run: |
          gh release create "$GITHUB_REF_NAME" dist/* --generate-notes || \
          gh release upload "$GITHUB_REF_NAME" dist/* --clobber
