name: Alpha Build

on:
  # 因为 alpha 不再负责频繁构建，且需要相对于 autobuild 更稳定使用环境
  # 所以不再使用 workflow_dispatch 触发
  # 应当通过 git tag 来触发构建
  # TODO 手动控制版本号
  workflow_dispatch:
    # inputs:
    #   tag_name:
    #     description: "Alpha tag name (e.g. v1.2.3-alpha.1)"
    #     required: true
    #     type: string

  # push:
  #   # 应当限制在 dev 分支上触发发布。
  #   branches:
  #     - dev
  #   # 应当限制 v*.*.*-alpha* 的 tag 来触发发布。
  #   tags:
  #     - "v*.*.*-alpha*"
permissions: write-all
env:
  TAG_NAME: alpha
  TAG_CHANNEL: Alpha
  CARGO_INCREMENTAL: 0
  RUST_BACKTRACE: short
concurrency:
  group: "${{ github.workflow }} - ${{ github.head_ref || github.ref }}"

jobs:
  check_alpha_tag:
    name: Check Alpha Tag package.json Version Consistency
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Check tag and package.json version
        id: check_tag
        run: |
          TAG_REF="${GITHUB_REF##*/}"
          echo "Current tag: $TAG_REF"
          if [[ ! "$TAG_REF" =~ -alpha ]]; then
            echo "Current tag is not an alpha tag."
            exit 1
          fi
          PKG_VERSION=$(jq -r .version package.json)
          echo "package.json version: $PKG_VERSION"
          if [[ "$PKG_VERSION" != *alpha* ]]; then
            echo "package.json version is not an alpha version."
            exit 1
          fi
          if [[ "$TAG_REF" != "v$PKG_VERSION" ]]; then
            echo "Tag ($TAG_REF) does not match package.json version (v$PKG_VERSION)."
            exit 1
          fi
          echo "Alpha tag and package.json version are consistent."

  delete_old_assets:
    name: Delete Old Alpha Release Assets and Tags
    needs: check_alpha_tag
    runs-on: ubuntu-latest
    steps:
      - name: Delete Old Alpha Tags Except Latest
        uses: actions/github-script@v7
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const tagPattern = /-alpha.*/; // 匹配带有 -alpha 的 tag
            const owner = context.repo.owner;
            const repo = context.repo.repo;

            try {
              // 获取所有 tag
              const { data: tags } = await github.rest.repos.listTags({
                owner,
                repo,
                per_page: 100 // 调整 per_page 以获取更多 tag
              });

              // 过滤出包含 -alpha 的 tag
              const alphaTags = (await Promise.all(
                tags
                  .filter(tag => tagPattern.test(tag.name))
                  .map(async tag => {
                    // 获取每个 tag 的 commit 信息以获得日期
                    const { data: commit } = await github.rest.repos.getCommit({
                      owner,
                      repo,
                      ref: tag.commit.sha
                    });
                    return {
                      ...tag,
                      commitDate: commit.committer && commit.committer.date ? commit.committer.date : commit.commit.author.date
                    };
                  })
              )).sort((a, b) => {
                // 按 commit 日期降序排序（最新的在前面）
                return new Date(b.commitDate) - new Date(a.commitDate);
              });

              console.log(`Found ${alphaTags.length} alpha tags`);

              if (alphaTags.length === 0) {
                console.log('No alpha tags found');
                return;
              }

              // 保留最新的 tag
              const latestTag = alphaTags[0];
              console.log(`Keeping latest alpha tag: ${latestTag.name}`);

              // 处理其他旧的 alpha tag
              for (const tag of alphaTags.slice(1)) {
                console.log(`Processing tag: ${tag.name}`);

                // 获取与 tag 关联的 release
                try {
                  const { data: release } = await github.rest.repos.getReleaseByTag({
                    owner,
                    repo,
                    tag: tag.name
                  });

                  // 删除 release 下的所有资产
                  if (release.assets && release.assets.length > 0) {
                    console.log(`Deleting ${release.assets.length} assets for release ${tag.name}`);
                    for (const asset of release.assets) {
                      console.log(`Deleting asset: ${asset.name} (${asset.id})`);
                      await github.rest.repos.deleteReleaseAsset({
                        owner,
                        repo,
                        asset_id: asset.id
                      });
                    }
                  }

                  // 删除 release
                  console.log(`Deleting release for tag: ${tag.name}`);
                  await github.rest.repos.deleteRelease({
                    owner,
                    repo,
                    release_id: release.id
                  });

                  // 删除 tag
                  console.log(`Deleting tag: ${tag.name}`);
                  await github.rest.git.deleteRef({
                    owner,
                    repo,
                    ref: `tags/${tag.name}`
                  });

                } catch (error) {
                  if (error.status === 404) {
                    console.log(`No release found for tag ${tag.name}, deleting tag directly`);
                    await github.rest.git.deleteRef({
                      owner,
                      repo,
                      ref: `tags/${tag.name}`
                    });
                  } else {
                    console.error(`Error processing tag ${tag.name}:`, error);
                    throw error;
                  }
                }
              }

              console.log('Old alpha tags and releases deleted successfully');
            } catch (error) {
              console.error('Error:', error);
              throw error;
            }

  update_tag:
    name: Update tag
    runs-on: ubuntu-latest
    needs: delete_old_assets
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Fetch UPDATE logs
        id: fetch_update_logs
        run: |
          if [ -f "UPDATELOG.md" ]; then
            UPDATE_LOGS=$(awk '/^## v/{if(flag) exit; flag=1} flag' UPDATELOG.md)
            if [ -n "$UPDATE_LOGS" ]; then
              echo "Found update logs"
              echo "UPDATE_LOGS<<EOF" >> $GITHUB_ENV
              echo "$UPDATE_LOGS" >> $GITHUB_ENV
              echo "EOF" >> $GITHUB_ENV
            else
              echo "No update sections found in UPDATELOG.md"
            fi
          else
            echo "UPDATELOG.md file not found"
          fi
        shell: bash

      - name: Set Env
        run: |
          echo "BUILDTIME=$(TZ=Asia/Shanghai date)" >> $GITHUB_ENV
        shell: bash

      - run: |
          if [ -z "$UPDATE_LOGS" ]; then
            echo "No update logs found, using default message"
            UPDATE_LOGS="More new features are now supported. Check for detailed changelog soon."
          else
            echo "Using found update logs"
          fi

          cat > release.txt << EOF
          $UPDATE_LOGS

          ## 我应该下载哪个版本？

          ### MacOS
          - MacOS intel芯片: x64.dmg
          - MacOS apple M芯片: aarch64.dmg

          ### Linux
          - Linux 64位: amd64.deb/amd64.rpm
          - Linux arm64 architecture: arm64.deb/aarch64.rpm
          - Linux armv7架构: armhf.deb/armhfp.rpm

          ### Windows (不再支持Win7)
          #### 正常版本(推荐)
          - 64位: x64-setup.exe
          - arm64架构: arm64-setup.exe
          #### 便携版问题很多不再提供
          #### 内置Webview2版(体积较大，仅在企业版系统或无法安装webview2时使用)
          - 64位: x64_fixed_webview2-setup.exe
          - arm64架构: arm64_fixed_webview2-setup.exe

          ### FAQ
          - [常见问题](https://clash-verge-rev.github.io/faq/windows.html)

          ### 稳定机场VPN推荐
          - [狗狗加速](https://verge.dginv.click/#/register?code=oaxsAGo6)

          Created at ${{ env.BUILDTIME }}.
          EOF

      - name: Upload Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ env.TAG_NAME }}
          name: "Clash Verge Rev ${{ env.TAG_CHANNEL }}"
          body_path: release.txt
          prerelease: true
          token: ${{ secrets.GITHUB_TOKEN }}
          generate_release_notes: true

  alpha-x86-windows-macos-linux:
    name: Alpha x86 Windows, MacOS and Linux
    needs: update_tag
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: windows-latest
            target: x86_64-pc-windows-msvc
          - os: windows-latest
            target: aarch64-pc-windows-msvc
          - os: macos-latest
            target: aarch64-apple-darwin
          - os: macos-latest
            target: x86_64-apple-darwin
          - os: ubuntu-22.04
            target: x86_64-unknown-linux-gnu
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Install Rust Stable
        uses: dtolnay/rust-toolchain@stable

      - name: Add Rust Target
        run: rustup target add ${{ matrix.target }}

      - name: Rust Cache
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: src-tauri
          save-if: false

      - name: Install dependencies (ubuntu only)
        if: matrix.os == 'ubuntu-22.04'
        run: |
          sudo apt-get update
          sudo apt-get install -y libxslt1.1 libwebkit2gtk-4.1-dev libayatana-appindicator3-dev librsvg2-dev patchelf

      - name: Install Node
        uses: actions/setup-node@v4
        with:
          node-version: "22"

      - uses: pnpm/action-setup@v4
        name: Install pnpm
        with:
          run_install: false

      - name: Pnpm install and check
        run: |
          pnpm i
          pnpm run prebuild ${{ matrix.target }}

      # - name: Release ${{ env.TAG_CHANNEL }} Version
      #   run: pnpm release-version ${{ env.TAG_NAME }}

      - name: Tauri build
        uses: tauri-apps/tauri-action@v0
        env:
          NODE_OPTIONS: "--max_old_space_size=4096"
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }}
          TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }}
          APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }}
          APPLE_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_CERTIFICATE_PASSWORD }}
          APPLE_SIGNING_IDENTITY: ${{ secrets.APPLE_SIGNING_IDENTITY }}
          APPLE_ID: ${{ secrets.APPLE_ID }}
          APPLE_PASSWORD: ${{ secrets.APPLE_PASSWORD }}
          APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}
        with:
          tagName: ${{ env.TAG_NAME }}
          releaseName: "Clash Verge Rev ${{ env.TAG_CHANNEL }}"
          releaseBody: "More new features are now supported."
          releaseDraft: false
          prerelease: true
          tauriScript: pnpm
          args: --target ${{ matrix.target }}

  alpha-arm-linux:
    name: Alpha ARM Linux
    needs: update_tag
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-22.04
            target: aarch64-unknown-linux-gnu
            arch: arm64
          - os: ubuntu-22.04
            target: armv7-unknown-linux-gnueabihf
            arch: armhf
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Install Rust Stable
        uses: dtolnay/rust-toolchain@stable

      - name: Add Rust Target
        run: rustup target add ${{ matrix.target }}

      - name: Rust Cache
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: src-tauri
          save-if: false

      - name: Install Node
        uses: actions/setup-node@v4
        with:
          node-version: "22"

      - name: Install pnpm
        uses: pnpm/action-setup@v4
        with:
          run_install: false

      - name: Pnpm install and check
        run: |
          pnpm i
          pnpm run prebuild ${{ matrix.target }}

      # - name: Release ${{ env.TAG_CHANNEL }} Version
      #   run: pnpm release-version ${{ env.TAG_NAME }}

      - name: Setup for linux
        run: |
          sudo ls -lR /etc/apt/

          cat > /tmp/sources.list << EOF
          deb [arch=amd64,i386] http://archive.ubuntu.com/ubuntu jammy main multiverse universe restricted
          deb [arch=amd64,i386] http://archive.ubuntu.com/ubuntu jammy-security main multiverse universe restricted
          deb [arch=amd64,i386] http://archive.ubuntu.com/ubuntu jammy-updates main multiverse universe restricted
          deb [arch=amd64,i386] http://archive.ubuntu.com/ubuntu jammy-backports main multiverse universe restricted

          deb [arch=armhf,arm64] http://ports.ubuntu.com/ubuntu-ports jammy main multiverse universe restricted
          deb [arch=armhf,arm64] http://ports.ubuntu.com/ubuntu-ports jammy-security main multiverse universe restricted
          deb [arch=armhf,arm64] http://ports.ubuntu.com/ubuntu-ports jammy-updates main multiverse universe restricted
          deb [arch=armhf,arm64] http://ports.ubuntu.com/ubuntu-ports jammy-backports main multiverse universe restricted
          EOF

          sudo mv /etc/apt/sources.list /etc/apt/sources.list.default
          sudo mv /tmp/sources.list /etc/apt/sources.list

          sudo dpkg --add-architecture ${{ matrix.arch }}
          sudo apt-get update -y
          sudo apt-get -f install -y

          sudo apt-get install -y \
            linux-libc-dev:${{ matrix.arch }} \
            libc6-dev:${{ matrix.arch }}

          sudo apt-get install -y \
            libxslt1.1:${{ matrix.arch }} \
            libwebkit2gtk-4.1-dev:${{ matrix.arch }} \
            libayatana-appindicator3-dev:${{ matrix.arch }} \
            libssl-dev:${{ matrix.arch }} \
            patchelf:${{ matrix.arch }} \
            librsvg2-dev:${{ matrix.arch }}

      - name: Install aarch64 tools
        if: matrix.target == 'aarch64-unknown-linux-gnu'
        run: |
          sudo apt install -y \
            gcc-aarch64-linux-gnu \
            g++-aarch64-linux-gnu

      - name: Install armv7 tools
        if: matrix.target == 'armv7-unknown-linux-gnueabihf'
        run: |
          sudo apt install -y \
            gcc-arm-linux-gnueabihf \
            g++-arm-linux-gnueabihf

      - name: Build for Linux
        run: |
          export PKG_CONFIG_ALLOW_CROSS=1
          if [ "${{ matrix.target }}" == "aarch64-unknown-linux-gnu" ]; then
            export PKG_CONFIG_PATH=/usr/lib/aarch64-linux-gnu/pkgconfig/:$PKG_CONFIG_PATH
            export PKG_CONFIG_SYSROOT_DIR=/usr/aarch64-linux-gnu/
          elif [ "${{ matrix.target }}" == "armv7-unknown-linux-gnueabihf" ]; then
            export PKG_CONFIG_PATH=/usr/lib/arm-linux-gnueabihf/pkgconfig/:$PKG_CONFIG_PATH
            export PKG_CONFIG_SYSROOT_DIR=/usr/arm-linux-gnueabihf/
          fi
          pnpm build --target ${{ matrix.target }}
        env:
          NODE_OPTIONS: "--max_old_space_size=4096"
          TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }}
          TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }}

      - name: Get Version
        run: |
          sudo apt-get update
          sudo apt-get install jq
          echo "VERSION=$(cat package.json | jq '.version' | tr -d '"')" >> $GITHUB_ENV
          echo "BUILDTIME=$(TZ=Asia/Shanghai date)" >> $GITHUB_ENV

      - name: Upload Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ env.TAG_NAME }}
          name: "Clash Verge Rev ${{ env.TAG_CHANNEL }}"
          prerelease: true
          token: ${{ secrets.GITHUB_TOKEN }}
          files: |
            src-tauri/target/${{ matrix.target }}/release/bundle/deb/*.deb
            src-tauri/target/${{ matrix.target }}/release/bundle/rpm/*.rpm

  alpha-x86-arm-windows_webview2:
    name: Alpha x86 and ARM Windows with WebView2
    needs: update_tag
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            arch: x64
          - os: windows-latest
            target: aarch64-pc-windows-msvc
            arch: arm64
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Add Rust Target
        run: rustup target add ${{ matrix.target }}

      - name: Rust Cache
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: src-tauri
          save-if: false

      - name: Install Node
        uses: actions/setup-node@v4
        with:
          node-version: "22"

      - uses: pnpm/action-setup@v4
        name: Install pnpm
        with:
          run_install: false

      - name: Pnpm install and check
        run: |
          pnpm i
          pnpm run prebuild ${{ matrix.target }}

      # - name: Release ${{ env.TAG_CHANNEL }} Version
      #   run: pnpm release-version ${{ env.TAG_NAME }}

      - name: Download WebView2 Runtime
        run: |
          invoke-webrequest -uri https://github.com/westinyang/WebView2RuntimeArchive/releases/download/109.0.1518.78/Microsoft.WebView2.FixedVersionRuntime.109.0.1518.78.${{ matrix.arch }}.cab -outfile Microsoft.WebView2.FixedVersionRuntime.109.0.1518.78.${{ matrix.arch }}.cab
          Expand .\Microsoft.WebView2.FixedVersionRuntime.109.0.1518.78.${{ matrix.arch }}.cab -F:* ./src-tauri
          Remove-Item .\src-tauri\tauri.windows.conf.json
          Rename-Item .\src-tauri\webview2.${{ matrix.arch }}.json tauri.windows.conf.json

      - name: Tauri build
        id: build
        uses: tauri-apps/tauri-action@v0
        env:
          NODE_OPTIONS: "--max_old_space_size=4096"
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }}
          TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }}
        with:
          tauriScript: pnpm
          args: --target ${{ matrix.target }}

      - name: Rename
        run: |
          $files = Get-ChildItem ".\src-tauri\target\${{ matrix.target }}\release\bundle\nsis\*-setup.exe"
          foreach ($file in $files) {
            $newName = $file.Name -replace "-setup\.exe$", "_fixed_webview2-setup.exe"
            Rename-Item $file.FullName $newName
          }

          $files = Get-ChildItem ".\src-tauri\target\${{ matrix.target }}\release\bundle\nsis\*.nsis.zip"
          foreach ($file in $files) {
            $newName = $file.Name -replace "-setup\.nsis\.zip$", "_fixed_webview2-setup.nsis.zip"
            Rename-Item $file.FullName $newName
          }

          $files = Get-ChildItem ".\src-tauri\target\${{ matrix.target }}\release\bundle\nsis\*-setup.exe.sig"
          foreach ($file in $files) {
            $newName = $file.Name -replace "-setup\.exe\.sig$", "_fixed_webview2-setup.exe.sig"
            Rename-Item $file.FullName $newName
          }

      - name: Upload Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ env.TAG_NAME }}
          name: "Clash Verge Rev ${{ env.TAG_CHANNEL }}"
          prerelease: true
          token: ${{ secrets.GITHUB_TOKEN }}
          files: src-tauri/target/${{ matrix.target }}/release/bundle/nsis/*setup*

      - name: Portable Bundle
        run: pnpm portable-fixed-webview2 ${{ matrix.target }} --${{ env.TAG_NAME }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
