name: Release

on:
  push:
    tags:
      - "v[0-9]*"

jobs:
  prepare-ui:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

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

      - name: Build WebUI
        run: |
          npm --prefix=./ui ci
          npm --prefix=./ui run build

      - name: Upload UI build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: ui-build
          path: ./ui/dist
          retention-days: 1

  build-linux:
    needs: prepare-ui
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version-file: "go.mod"

      - name: Download UI build artifacts
        uses: actions/download-artifact@v4
        with:
          name: ui-build
          path: ./ui/dist

      - name: Build Linux binaries
        env:
          CGO_ENABLED: 0
          GOOS: linux
        run: |
          mkdir -p dist/linux
          for ARCH in amd64 arm64 armv7; do
            if [ "$ARCH" == "armv7" ]; then
              export GOARM=7
            fi
            go build -ldflags="-s -w -X github.com/usual2970/certimate.Version=${GITHUB_REF#refs/tags/}" -o dist/linux/certimate_${GITHUB_REF#refs/tags/}_linux_$ARCH
          done

      - name: Upload Linux binaries
        uses: actions/upload-artifact@v4
        with:
          name: linux-binaries
          path: dist/linux/
          retention-days: 1

  build-macos:
    needs: prepare-ui
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version-file: "go.mod"

      - name: Download UI build artifacts
        uses: actions/download-artifact@v4
        with:
          name: ui-build
          path: ./ui/dist

      - name: Build macOS binaries
        env:
          CGO_ENABLED: 0
          GOOS: darwin
        run: |
          mkdir -p dist/darwin
          for ARCH in amd64 arm64; do
            go build -ldflags="-s -w -X github.com/usual2970/certimate.Version=${GITHUB_REF#refs/tags/}" -o dist/darwin/certimate_${GITHUB_REF#refs/tags/}_darwin_$ARCH
          done

      - name: Upload macOS binaries
        uses: actions/upload-artifact@v4
        with:
          name: macos-binaries
          path: dist/darwin/
          retention-days: 1

  build-windows:
    needs: prepare-ui
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version-file: "go.mod"

      - name: Download UI build artifacts
        uses: actions/download-artifact@v4
        with:
          name: ui-build
          path: ./ui/dist

      - name: Build Windows binaries
        env:
          CGO_ENABLED: 0
          GOOS: windows
        run: |
          mkdir -p dist/windows
          for ARCH in amd64 arm64; do
            go build -ldflags="-s -w -X github.com/usual2970/certimate.Version=${GITHUB_REF#refs/tags/}" -o dist/windows/certimate_${GITHUB_REF#refs/tags/}_windows_$ARCH.exe
          done

      - name: Upload Windows binaries
        uses: actions/upload-artifact@v4
        with:
          name: windows-binaries
          path: dist/windows/
          retention-days: 1

  create-release:
    needs: [build-linux, build-macos, build-windows]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Download all binaries
        uses: actions/download-artifact@v4
        with:
          path: ./artifacts

      - name: Prepare release assets
        run: |
          mkdir -p dist
          cp -r artifacts/linux-binaries/* dist/
          cp -r artifacts/macos-binaries/* dist/
          cp -r artifacts/windows-binaries/* dist/

          find dist -type f -not -name "*.exe" -exec chmod +x {} \;

          # 为每个二进制文件创建 zip 包
          cd dist
          for bin in certimate_*; do
            if [[ "$bin" == *".exe" ]]; then
              entrypoint="certimate.exe"
            else
              entrypoint="certimate"
            fi

            tmpdir=$(mktemp -d)
            cp "$bin" "${tmpdir}/${entrypoint}"
            cp ../README.md ../LICENSE.md ../CHANGELOG.md "$tmpdir"

            if [[ "$bin" == *".exe" ]]; then
              zip -j "${bin%.exe}.zip" "$tmpdir"/*
            else
              zip -j -X "${bin}.zip" "$tmpdir"/*
            fi

            rm -rf "$tmpdir"
          done

          # 创建校验和文件
          sha256sum *.zip > checksums.txt

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            dist/*.zip
            dist/checksums.txt
          draft: true
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}
