name: Build and Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: "true"

jobs:
  release-mac-arm64:
    runs-on: macos-14

    steps:
      - name: Check out git repository
        uses: actions/checkout@v5
        with:
          fetch-depth: 0

      - name: Install Node.js
        uses: actions/setup-node@v5
        with:
          node-version: 24
          cache: "npm"

      - name: Install Dependencies
        run: npm install

      - name: Sync version with tag
        shell: bash
        run: |
          VERSION=${GITHUB_REF_NAME#v}
          echo "Syncing package.json version to $VERSION"
          npm version $VERSION --no-git-tag-version --allow-same-version

      - name: Build Frontend & Type Check
        run: |
          npx tsc
          npx vite build

      - name: Package and Publish macOS arm64 (unsigned DMG)
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          CSC_IDENTITY_AUTO_DISCOVERY: "false"
        run: |
          npx electron-builder --mac dmg --arm64 --publish always

  release-linux:
    runs-on: ubuntu-latest

    steps:
      - name: Check out git repository
        uses: actions/checkout@v5
        with:
          fetch-depth: 0

      - name: Install Node.js
        uses: actions/setup-node@v5
        with:
          node-version: 24
          cache: "npm"

      - name: Install Dependencies
        run: npm install

      - name: Sync version with tag
        shell: bash
        run: |
          VERSION=${GITHUB_REF_NAME#v}
          echo "Syncing package.json version to $VERSION"
          npm version $VERSION --no-git-tag-version --allow-same-version

      - name: Build Frontend & Type Check
        run: |
          npx tsc
          npx vite build

      - name: Package and Publish Linux
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          npx electron-builder --linux --publish always

  release:
    runs-on: windows-latest

    steps:
      - name: Check out git repository
        uses: actions/checkout@v5
        with:
          fetch-depth: 0

      - name: Install Node.js
        uses: actions/setup-node@v5
        with:
          node-version: 24
          cache: 'npm'

      - name: Install Dependencies
        run: npm install

      - name: Sync version with tag
        shell: bash
        run: |
          VERSION=${GITHUB_REF_NAME#v}
          echo "Syncing package.json version to $VERSION"
          npm version $VERSION --no-git-tag-version --allow-same-version

      - name: Build Frontend & Type Check
        run: |
          npx tsc
          npx vite build

      - name: Package and Publish
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          npx electron-builder --publish always

  release-windows-arm64:
    runs-on: windows-latest

    steps:
      - name: Check out git repository
        uses: actions/checkout@v5
        with:
          fetch-depth: 0

      - name: Install Node.js
        uses: actions/setup-node@v5
        with:
          node-version: 24
          cache: 'npm'

      - name: Install Dependencies
        run: npm install

      - name: Sync version with tag
        shell: bash
        run: |
          VERSION=${GITHUB_REF_NAME#v}
          echo "Syncing package.json version to $VERSION"
          npm version $VERSION --no-git-tag-version --allow-same-version

      - name: Build Frontend & Type Check
        run: |
          npx tsc
          npx vite build

      - name: Package and Publish Windows arm64
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          npx electron-builder --win nsis --arm64 --publish always '--config.artifactName=${productName}-${version}-arm64-Setup.${ext}'

  update-release-notes:
    runs-on: ubuntu-latest
    needs:
      - release-mac-arm64
      - release-linux
      - release
      - release-windows-arm64

    steps:
      - name: Generate release notes with platform download links
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        shell: bash
        run: |
          set -euo pipefail

          TAG="$GITHUB_REF_NAME"
          REPO="$GITHUB_REPOSITORY"
          RELEASE_PAGE="https://github.com/$REPO/releases/tag/$TAG"

          ASSETS_JSON="$(gh release view "$TAG" --repo "$REPO" --json assets)"

          pick_asset() {
            local pattern="$1"
            echo "$ASSETS_JSON" | jq -r --arg p "$pattern" '[.assets[].name | select(test($p))][0] // ""'
          }

          WINDOWS_ASSET="$(echo "$ASSETS_JSON" | jq -r '[.assets[].name | select(test("\\.exe$")) | select(test("arm64") | not)][0] // ""')"
          WINDOWS_ARM64_ASSET="$(echo "$ASSETS_JSON" | jq -r '[.assets[].name | select(test("arm64.*\\.exe$"))][0] // ""')"
          MAC_ASSET="$(pick_asset "\\.dmg$")"
          LINUX_DEB_ASSET="$(pick_asset "\\.deb$")"
          LINUX_TAR_ASSET="$(pick_asset "\\.tar\\.gz$")"
          LINUX_APPIMAGE_ASSET="$(pick_asset "\\.AppImage$")"

          build_link() {
            local name="$1"
            if [ -n "$name" ]; then
              echo "https://github.com/$REPO/releases/download/$TAG/$name"
            fi
          }

          WINDOWS_URL="$(build_link "$WINDOWS_ASSET")"
          WINDOWS_ARM64_URL="$(build_link "$WINDOWS_ARM64_ASSET")"
          MAC_URL="$(build_link "$MAC_ASSET")"
          LINUX_DEB_URL="$(build_link "$LINUX_DEB_ASSET")"
          LINUX_TAR_URL="$(build_link "$LINUX_TAR_ASSET")"
          LINUX_APPIMAGE_URL="$(build_link "$LINUX_APPIMAGE_ASSET")"

          cat > release_notes.md <<EOF
          ## 更新日志
          修复了一些已知问题

          ## 查看更多日志/获取最新动态
          [点击加入 Telegram 频道](https://t.me/weflow_cc)

          ## 下载
          - Windows x64（Win10+）: ${WINDOWS_URL:-$RELEASE_PAGE}
          - Windows arm64: ${WINDOWS_ARM64_URL:-$RELEASE_PAGE}
          - macOS（M系列芯片）: ${MAC_URL:-$RELEASE_PAGE}
          - Linux (.deb) (即将废弃): ${LINUX_DEB_URL:-$RELEASE_PAGE}
          - Linux (.tar.gz): ${LINUX_TAR_URL:-$RELEASE_PAGE}
          - linux (.AppImage): ${LINUX_APPIMAGE_URL:-$RELEASE_PAGE}

          > 如果某个平台链接暂时未生成，可进入完整发布页查看全部资源：$RELEASE_PAGE
          EOF

          gh release edit "$TAG" --repo "$REPO" --notes-file release_notes.md
