name: Build Quasar SPA and Go Backend for memberApp

on:
  push:
    tags:
      - '*'

jobs:
  build-spa:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install dependencies
        run: npm install

      - name: Install Quasar CLI
        run: |
          npm cache clean --force
          npm install -g @quasar/cli --force

      - name: Build Quasar SPA
        run: quasar build

      - name: Upload SPA artifact
        uses: actions/upload-artifact@v3
        with:
          name: quasar-dist
          path: ./dist/spa

  build-backend:
    needs: build-spa
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - goos: windows
            arch: amd64
            ext: .exe
          - goos: linux
            arch: amd64
            ext: ""
          - goos: linux
            arch: arm64
            ext: ""
          - goos: linux
            arch: arm
            arm_version: 6
            ext: ""
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Download SPA artifact
        uses: actions/download-artifact@v3
        with:
          name: quasar-dist
          path: ./dist/spa

      - name: Ensure latest Go is installed in /data/go
        run: |
          export GOROOT=/data/go/go
          export PATH=$GOROOT/bin:$PATH
          export GOCACHE=/data/gocache
          export GOMODCACHE=/data/gomodcache
          mkdir -p $GOCACHE $GOMODCACHE

          if [ ! -x "$GOROOT/bin/go" ]; then
            echo "Go not found in $GOROOT, downloading latest stable..."
            GO_VERSION=$(curl -s https://go.dev/VERSION?m=text | head -n1)
            echo "Latest version is $GO_VERSION"
            mkdir -p /data/go
            curl -sSL "https://go.dev/dl/${GO_VERSION}.linux-amd64.tar.gz" -o /tmp/go.tar.gz
            tar -C /data/go -xzf /tmp/go.tar.gz
          else
            echo "Using cached Go from $GOROOT"
          fi

          go version

      - name: Go Mod Tidy & Download
        working-directory: ./backend
        run: |
          export GOROOT=/data/go/go
          export PATH=$GOROOT/bin:$PATH
          export GOCACHE=/data/gocache
          export GOMODCACHE=/data/gomodcache
          mkdir -p $GOCACHE $GOMODCACHE
          go mod tidy -v
          go mod download

      - name: Build Go backend binary
        working-directory: ./backend
        run: |
          export GOROOT=/data/go/go
          export PATH=$GOROOT/bin:$PATH
          export GOCACHE=/data/gocache
          export GOMODCACHE=/data/gomodcache
          mkdir -p $GOCACHE $GOMODCACHE

          OUTPUT="../memberApp-${{ matrix.goos }}-${{ matrix.arch }}${{ matrix.ext }}"
          if [ "${{ matrix.arch }}" = "arm" ]; then
            GOARM=${{ matrix.arm_version }}
          fi

          GOOS=${{ matrix.goos }} GOARCH=${{ matrix.arch }} go build -ldflags="-s -w" -trimpath -o "$OUTPUT" main.go

      - name: Upload combined package
        uses: actions/upload-artifact@v3
        with:
          name: memberApp-${{ matrix.goos }}-${{ matrix.arch }}
          path: |
            ./dist/spa
            memberApp-${{ matrix.goos }}-${{ matrix.arch }}${{ matrix.ext }}
