name: Build & Publish All-in-One

on:
  push:
    branches: [main]
    tags: [v*]

env:
  GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  BIN_NAME: spage
  BIN_OUT: build

concurrency:
  group: 'build-aio'
  cancel-in-progress: true

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    strategy:
      matrix:
        goos: [linux, darwin, windows]
        goarch: [amd64, arm64]
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Go
        uses: actions/setup-go@v5

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22

      - name: Build frontend
        run: |
          cd web-src
          npm install pnpm -g
          pnpm install
          pnpm build
          cp -r out/* ../static/dist/

      - name: Build Go binary
        env:
          GOOS: ${{ matrix.goos }}
          GOARCH: ${{ matrix.goarch }}
        run: |
          mkdir -p ${{ env.BIN_OUT }}/${{ matrix.goos }}-${{ matrix.goarch }}
          if [ "${{ matrix.goos }}" = "windows" ]; then
            BIN_NAME="${{ env.BIN_NAME }}.exe"
          else
            BIN_NAME="${{ env.BIN_NAME }}"
          fi
          go build -o ${{ env.BIN_OUT }}/${{ matrix.goos }}-${{ matrix.goarch }}/$BIN_NAME ./cmd/server

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.BIN_NAME }}-${{ matrix.goos }}-${{ matrix.goarch }}
          path: ${{ env.BIN_OUT }}/${{ matrix.goos }}-${{ matrix.goarch }}/${{ env.BIN_NAME }}*

  publish:
    name: Publish Artifacts & Images
    runs-on: ubuntu-latest
    needs: build
    steps:
      # 下载所有产物用于 Release
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Download all artifacts for release
        uses: actions/download-artifact@v4
        with:
          pattern: ${{ env.BIN_NAME }}-*
          path: ${{ env.BIN_OUT }}

      # 只下载 linux 产物用于镜像构建
      - name: Download linux artifacts for image
        uses: actions/download-artifact@v4
        with:
          pattern: ${{ env.BIN_NAME }}-linux-*
          path: ${{ env.BIN_OUT }}

      - name: Prepare binaries for Docker build
        run: |
          mkdir -p build/linux-amd64 build/linux-arm64
          mv build/spage-linux-amd64/${{ env.BIN_NAME }} build/linux-amd64/${{ env.BIN_NAME }}
          mv build/spage-linux-arm64/${{ env.BIN_NAME }} build/linux-arm64/${{ env.BIN_NAME }}

      - name: Set version/tag
        id: version
        run: |
          if [[ "${GITHUB_REF}" == refs/tags/v* ]]; then
            VERSION="${GITHUB_REF#refs/tags/}"
            TAG="${VERSION}"
            NIGHTLY="false"
          else
            VERSION="${GITHUB_SHA}"
            TAG="nightly"
            NIGHTLY="true"
          fi
          echo "version=${VERSION}" >> $GITHUB_OUTPUT
          echo "tag=${TAG}" >> $GITHUB_OUTPUT
          echo "nightly=${NIGHTLY}" >> $GITHUB_OUTPUT

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

      - name: Login to GHCR
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ secrets.GHCR_USER }}
          password: ${{ secrets.GHCR_TOKEN }}

      - name: Login to DockerHub
        uses: docker/login-action@v3
        with:
          registry: docker.io
          username: ${{ secrets.DOCKERHUB_USER }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Login to Harbor
        uses: docker/login-action@v3
        with:
          registry: reg.liteyuki.org
          username: ${{ secrets.LITEYUKIREG_USER }}
          password: ${{ secrets.LITEYUKIREG_TOKEN }}

      - name: Build & Push to GHCR
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: ghcr.io/liteyukistudio/spage:${{ steps.version.outputs.tag }}
          platforms: linux/amd64,linux/arm64

      - name: Build & Push to DockerHub
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: docker.io/liteyukistudio/spage:${{ steps.version.outputs.tag }}
          platforms: linux/amd64,linux/arm64

      - name: Build & Push to Harbor (amd64 only)
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: reg.liteyuki.org/spage/spage:${{ steps.version.outputs.tag }}
          platforms: linux/amd64,linux/arm64

      - name: Publish GitHub Release
        if: ${{ steps.version.outputs.nightly == 'false' }}
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ steps.version.outputs.tag }}
          name: Release ${{ steps.version.outputs.tag }}
          body: |
            Release ${{ steps.version.outputs.tag }}
            Built from tag ${{ steps.version.outputs.tag }}
          files: |
            ${{ env.BIN_OUT }}/**/${{ env.BIN_NAME }}*