name: release

on:
  push:
    tags:
      - "*.*.*"
  workflow_dispatch:

jobs:
  check-runner:
    runs-on: ubuntu-latest
    outputs:
      runner-label: ${{ steps.set-runner.outputs.runner-label }}
    steps:
      - name: Set runner
        id: set-runner
        run: |
          runners=$(curl -s -H "Accept: application/vnd.github+json" \
            -H "Authorization: token ${{ secrets.REPO_ACCESS_TOKEN }}" \
            "https://api.github.com/repos/${{ github.repository }}/actions/runners")
          available=$(echo "$runners" | jq '.runners[] | select(.status == "online" and .busy == false and .labels[] .name == "self-hosted")')
          if [ -n "$available" ]; then
            echo "runner-label=self-hosted" >> "$GITHUB_OUTPUT"
          else
            echo "runner-label=ubuntu-latest" >> "$GITHUB_OUTPUT"
          fi

  frontend:
    needs: check-runner
    runs-on: ${{ needs.check-runner.outputs.runner-label }}
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 1

      - uses: pnpm/action-setup@v4
        with:
          version: latest

      - name: Set up Node.js
        uses: actions/setup-node@v6
        with:
          node-version: latest
          cache: pnpm
          cache-dependency-path: frontend/pnpm-lock.yaml

      - name: Cache frontend build
        uses: actions/cache@v5
        with:
          path: |
            frontend/node_modules
            frontend/.svelte-kit
          key: ${{ runner.os }}-pnpm-${{ hashFiles('frontend/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-

      - name: Build frontend
        run: |
          pnpm --prefix=./frontend install --frozen-lockfile --prefer-offline
          PUBLIC_VERSION=${GITHUB_REF##*/} pnpm --prefix=./frontend run build
          tar -czf frontend-dist.tar.gz -C frontend/build .

      - name: Upload frontend artifact
        uses: actions/upload-artifact@v6
        with:
          name: frontend-dist
          path: frontend-dist.tar.gz

  goreleaser:
    needs:
      - check-runner
      - frontend
    runs-on: ${{ needs.check-runner.outputs.runner-label }}
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Download frontend artifact
        uses: actions/download-artifact@v8
        with:
          name: frontend-dist

      - name: Extract frontend into backend
        run: |
          mkdir -p backend/pb_public
          tar -xzf frontend-dist.tar.gz -C backend/pb_public
          git reset --hard
          git clean -fd

      - name: Set up Go
        uses: actions/setup-go@v6
        with:
          go-version-file: backend/go.mod
          cache-dependency-path: backend/go.sum

      - name: Cache Go build
        uses: actions/cache@v5
        with:
          path: |
            ~/.cache/go-build
            ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('backend/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-

      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v7
        with:
          distribution: goreleaser
          version: "~> v2"
          args: release --clean
          workdir: backend
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          AUR_KEY: ${{ secrets.AUR_KEY }}

      - name: Upload backend artifact
        uses: actions/upload-artifact@v6
        with:
          name: backend-dist
          path: backend/dist

  docker:
    needs:
      - check-runner
      - frontend
      - goreleaser
    runs-on: ${{ needs.check-runner.outputs.runner-label }}
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Download backend artifact
        uses: actions/download-artifact@v8
        with:
          name: backend-dist
          path: backend/dist

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v6
        with:
          images: |
            ghcr.io/seriousm4x/UpSnap
            seriousm4x/UpSnap
          tags: |
            type=ref,event=tag
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}
            type=semver,pattern={{major}}.{{minor}}

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v4

      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v4

      - name: Log in to GHCR
        uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Log in to Docker Hub
        uses: docker/login-action@v4
        with:
          username: ${{ vars.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: .
          platforms: linux/amd64,linux/arm64,linux/arm/v6,linux/arm/v7
          builder: ${{ steps.buildx.outputs.name }}
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
          build-args: |
            VERSION=${{ github.ref_name }}
