name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  # Phase 1: Build in parallel and output artifacts
  build:
    name: Build ${{ matrix.platform }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: macos-latest
            platform: darwin/amd64
            os_name: MacOS
            arch_name: Amd64
            build_name: gonavi-build-darwin-amd64
            wails_tags: ""
            artifact_suffix: ""
            build_optional_agents: true
            linux_webkit: ""
          - os: macos-latest
            platform: darwin/arm64
            os_name: MacOS
            arch_name: Arm64
            build_name: gonavi-build-darwin-arm64
            wails_tags: ""
            artifact_suffix: ""
            build_optional_agents: true
            linux_webkit: ""
          - os: windows-latest
            platform: windows/amd64
            os_name: Windows
            arch_name: Amd64
            build_name: gonavi-build-windows-amd64
            wails_tags: ""
            artifact_suffix: ""
            build_optional_agents: true
            linux_webkit: ""
          - os: windows-latest
            platform: windows/arm64
            os_name: Windows
            arch_name: Arm64
            build_name: gonavi-build-windows-arm64
            wails_tags: ""
            artifact_suffix: ""
            build_optional_agents: true
            linux_webkit: ""
          - os: ubuntu-22.04
            platform: linux/amd64
            os_name: Linux
            arch_name: Amd64
            build_name: gonavi-build-linux-amd64
            wails_tags: ""
            artifact_suffix: ""
            build_optional_agents: true
            linux_webkit: "4.0"
          # Debian 13 (trixie) 默认仓库已切到 WebKitGTK 4.1：单独提供 4.1 变体产物
          - os: ubuntu-24.04
            platform: linux/amd64
            os_name: Linux
            arch_name: Amd64
            build_name: gonavi-build-linux-amd64-webkit41
            wails_tags: "webkit2_41"
            artifact_suffix: "-WebKit41"
            build_optional_agents: false
            linux_webkit: "4.1"

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.24'
          check-latest: true

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      # Linux Dependencies (GTK3, WebKit2GTK required by Wails)
      - name: Install Linux Dependencies
        if: contains(matrix.platform, 'linux')
        run: |
          sudo apt-get update
          sudo apt-get install -y libgtk-3-dev

          # WebKitGTK 4.1 需要 libsoup3；4.0 使用 libsoup2（通常由 webkit2gtk dev 包拉起）
          if [ "${{ matrix.linux_webkit }}" = "4.1" ]; then
            sudo apt-get install -y libwebkit2gtk-4.1-dev libsoup-3.0-dev
          else
            sudo apt-get install -y libwebkit2gtk-4.0-dev
          fi

          # AppImage 运行/打包可能需要 FUSE2。不同发行版/版本包名不同，做兼容兜底。
          sudo apt-get install -y libfuse2 || sudo apt-get install -y libfuse2t64 || true

          # Download linuxdeploy tools for AppImage packaging
          LINUXDEPLOY_URL="https://github.com/linuxdeploy/linuxdeploy/releases/download/continuous/linuxdeploy-x86_64.AppImage"
          PLUGIN_URL="https://github.com/linuxdeploy/linuxdeploy-plugin-gtk/releases/download/continuous/linuxdeploy-plugin-gtk-x86_64.AppImage"

          echo "📥 下载 linuxdeploy..."
          wget --retry-connrefused --waitretry=1 --read-timeout=20 --timeout=15 --tries=3 \
            -O /tmp/linuxdeploy "$LINUXDEPLOY_URL" || {
            echo "⚠️ linuxdeploy 下载失败，AppImage 打包将跳过"
            touch /tmp/skip-appimage
          }

          echo "📥 下载 linuxdeploy-plugin-gtk..."
          wget --retry-connrefused --waitretry=1 --read-timeout=20 --timeout=15 --tries=3 \
            -O /tmp/linuxdeploy-plugin-gtk "$PLUGIN_URL" || {
            echo "⚠️ linuxdeploy-plugin-gtk 下载失败，AppImage 打包将跳过"
            touch /tmp/skip-appimage
          }

          if [ ! -f /tmp/skip-appimage ]; then
            chmod +x /tmp/linuxdeploy /tmp/linuxdeploy-plugin-gtk
            echo "✅ AppImage 工具准备完成"
          fi

      - name: Install Wails
        run: go install -v github.com/wailsapp/wails/v2/cmd/wails@latest

      - name: Build
        shell: bash
        run: |
          set -euo pipefail
          TAG_ARGS=()
          if [ -n "${{ matrix.wails_tags }}" ]; then
            TAG_ARGS+=(-tags "${{ matrix.wails_tags }}")
          fi
          wails build -platform ${{ matrix.platform }} -clean -o ${{ matrix.build_name }} "${TAG_ARGS[@]}" -ldflags "-s -w -X GoNavi-Wails/internal/app.AppVersion=${{ github.ref_name }}"

      - name: Build Optional Driver Agents
        if: ${{ matrix.build_optional_agents }}
        shell: bash
        run: |
          set -euo pipefail
          TARGET_PLATFORM="${{ matrix.platform }}"
          GOOS="${TARGET_PLATFORM%%/*}"
          GOARCH="${TARGET_PLATFORM##*/}"
          DRIVERS=(mariadb diros sphinx sqlserver sqlite duckdb dameng kingbase highgo vastbase mongodb tdengine)
          OUTDIR="drivers/${{ matrix.os_name }}"
          mkdir -p "$OUTDIR"

          for DRIVER in "${DRIVERS[@]}"; do
            TAG="gonavi_${DRIVER}_driver"
            OUTPUT="${DRIVER}-driver-agent-${GOOS}-${GOARCH}"
            if [ "$GOOS" = "windows" ]; then
              OUTPUT="${OUTPUT}.exe"
            fi
            OUTPUT_PATH="${OUTDIR}/${OUTPUT}"
            echo "🔧 构建 ${OUTPUT_PATH} (tag=${TAG})"
            if [ "$DRIVER" = "duckdb" ]; then
              set +e
              CGO_ENABLED=1 GOOS="$GOOS" GOARCH="$GOARCH" go build \
                -tags "${TAG}" \
                -trimpath \
                -ldflags "-s -w" \
                -o "${OUTPUT_PATH}" \
                ./cmd/optional-driver-agent
              DUCKDB_RC=$?
              set -e
              if [ "${DUCKDB_RC}" -ne 0 ]; then
                echo "⚠️ DuckDB 代理构建失败（平台 ${GOOS}/${GOARCH}），跳过该资产，不阻断发布"
                rm -f "${OUTPUT_PATH}"
                continue
              fi
            else
              CGO_ENABLED=0 GOOS="$GOOS" GOARCH="$GOARCH" go build \
                -tags "${TAG}" \
                -trimpath \
                -ldflags "-s -w" \
                -o "${OUTPUT_PATH}" \
                ./cmd/optional-driver-agent
            fi
          done

      # macOS Packaging
      - name: Package macOS DMG
        if: contains(matrix.platform, 'darwin')
        run: |
          brew install create-dmg
          VERSION="${{ github.ref_name }}"
          VERSION="${VERSION#v}"
          cd build/bin
          
          APP_PATH=$(find . -maxdepth 1 -name "*.app" | head -n 1)
          if [ -z "$APP_PATH" ]; then
            echo "❌ 未找到 .app 应用包!"
            exit 1
          fi
          APP_NAME=$(basename "$APP_PATH")
          
          echo "🔏 正在进行 Ad-hoc 签名..."
          codesign --force --options runtime --deep --sign - "$APP_NAME"
          
          DMG_NAME="${{ matrix.build_name }}.dmg"
          FINAL_NAME="GoNavi-$VERSION-${{ matrix.os_name }}-${{ matrix.arch_name }}${{ matrix.artifact_suffix }}.dmg"
          echo "📦 正在生成 DMG: $DMG_NAME..."
          
          create-dmg \
            --volname "GoNavi Installer" \
            --window-pos 200 120 \
            --window-size 800 400 \
            --icon-size 100 \
            --icon "$APP_NAME" 200 190 \
            --hide-extension "$APP_NAME" \
            --app-drop-link 600 185 \
            "$DMG_NAME" \
            "$APP_NAME"
            
          mv "$DMG_NAME" "../../$FINAL_NAME"

      # Windows Packaging
      - name: Package Windows Portable Zip
        if: contains(matrix.platform, 'windows')
        shell: pwsh
        run: |
          Set-Location build/bin
          $version = "${{ github.ref_name }}"
          if ($version.StartsWith("v")) {
              $version = $version.Substring(1)
          }
          $target = "${{ matrix.build_name }}"
          $finalExeName = "GoNavi-$version-${{ matrix.os_name }}-${{ matrix.arch_name }}${{ matrix.artifact_suffix }}.exe"
          $finalZipName = "GoNavi-$version-${{ matrix.os_name }}-${{ matrix.arch_name }}${{ matrix.artifact_suffix }}.zip"

          if (Test-Path "$target.exe") {
              $finalExe = "$target.exe"
          } elseif (Test-Path "$target") {
              Rename-Item -Path "$target" -NewName "$target.exe"
              $finalExe = "$target.exe"
          } else {
              Write-Error "❌ 未找到构建产物 '$target'!"
              exit 1
          }

          Write-Host "📦 生成 Windows 可执行文件 $finalExeName..."
          Copy-Item -LiteralPath $finalExe -Destination "..\\..\\$finalExeName" -Force

          Write-Host "📦 生成 Windows 压缩包 $finalZipName..."
          Compress-Archive -LiteralPath $finalExe -DestinationPath "..\\..\\$finalZipName" -Force

      # Linux Packaging (tar.gz and AppImage)
      - name: Package Linux
        if: contains(matrix.platform, 'linux')
        run: |
          VERSION="${{ github.ref_name }}"
          VERSION="${VERSION#v}"
          cd build/bin
          TARGET="${{ matrix.build_name }}"
          TAR_NAME="GoNavi-$VERSION-${{ matrix.os_name }}-${{ matrix.arch_name }}${{ matrix.artifact_suffix }}.tar.gz"
          APPIMAGE_NAME="GoNavi-$VERSION-${{ matrix.os_name }}-${{ matrix.arch_name }}${{ matrix.artifact_suffix }}.AppImage"

          if [ ! -f "$TARGET" ]; then
              echo "❌ 未找到构建产物 '$TARGET'!"
              exit 1
          fi

          chmod +x "$TARGET"

          # 1. Create tar.gz
          echo "📦 正在打包 $TAR_NAME..."
          tar -czvf "$TAR_NAME" "$TARGET"
          mv "$TAR_NAME" ../../

          # 2. Create AppImage (skip for ARM64 or if tools unavailable)
          if [ -f /tmp/skip-appimage ]; then
              echo "⚠️ 跳过 AppImage 打包"
              exit 0
          fi

          echo "📦 正在生成 AppImage..."

          # Create AppDir structure
          mkdir -p AppDir/usr/bin
          mkdir -p AppDir/usr/share/applications
          mkdir -p AppDir/usr/share/icons/hicolor/256x256/apps

          cp "$TARGET" AppDir/usr/bin/gonavi

          # Create desktop file
          printf '%s\n' \
            '[Desktop Entry]' \
            'Name=GoNavi' \
            'Exec=gonavi' \
            'Icon=gonavi' \
            'Type=Application' \
            'Categories=Development;Database;' \
            'Comment=Database Management Tool' \
            > AppDir/usr/share/applications/gonavi.desktop

          cp AppDir/usr/share/applications/gonavi.desktop AppDir/gonavi.desktop

          # Create a simple icon (or use existing if available)
          if [ -f "../../build/appicon.png" ]; then
              cp "../../build/appicon.png" AppDir/usr/share/icons/hicolor/256x256/apps/gonavi.png
              cp "../../build/appicon.png" AppDir/gonavi.png
          else
              # Create a placeholder icon
              convert -size 256x256 xc:#336791 -fill white -gravity center -pointsize 48 -annotate 0 "GoNavi" AppDir/gonavi.png || \
              wget -q "https://via.placeholder.com/256/336791/FFFFFF?text=GoNavi" -O AppDir/gonavi.png || \
              touch AppDir/gonavi.png
              cp AppDir/gonavi.png AppDir/usr/share/icons/hicolor/256x256/apps/gonavi.png
          fi

          # Build AppImage
          export DEPLOY_GTK_VERSION=3
          /tmp/linuxdeploy --appdir AppDir --plugin gtk --output appimage || {
              echo "⚠️ AppImage 生成失败，但 tar.gz 已成功生成"
              exit 0
          }

          # Rename output
          mv GoNavi*.AppImage "$APPIMAGE_NAME" 2>/dev/null || {
              echo "⚠️ AppImage 重命名失败"
              exit 0
          }

          if [ -f "$APPIMAGE_NAME" ]; then
              mv "$APPIMAGE_NAME" ../../
              echo "✅ AppImage 生成成功"
          fi

      # Upload to Actions Artifacts (Temporary Storage)
      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: build-artifacts-${{ strategy.job-index }} # Unique name per job
          path: |
            GoNavi-*.dmg
            GoNavi-*.exe
            GoNavi-*.zip
            GoNavi-*.tar.gz
            GoNavi-*.AppImage
            drivers/**
          retention-days: 1

  # Phase 2: Collect all artifacts and Publish Release (Single Job)
  release:
    name: Publish Release
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Download All Artifacts
        uses: actions/download-artifact@v4
        with:
          path: release-assets
          pattern: build-artifacts-*
          merge-multiple: true

      - name: List Assets
        run: ls -R release-assets

      - name: Package Driver Agents Bundle
        shell: bash
        run: |
          set -euo pipefail
          cd release-assets
          if [ ! -d drivers ]; then
            echo "⚠️ 未找到 drivers 目录，跳过驱动总包打包"
            exit 0
          fi
          if [ -z "$(find drivers -type f 2>/dev/null | head -n 1)" ]; then
            echo "⚠️ drivers 目录为空，跳过驱动总包打包"
            rm -rf drivers
            exit 0
          fi

          echo "📦 打包驱动总包：GoNavi-DriverAgents.zip"
          python3 - <<'PY'
          import json
          import os
          import zipfile
          from pathlib import Path

          out_name = "GoNavi-DriverAgents.zip"
          index_name = "GoNavi-DriverAgents-Index.json"
          base = Path("drivers")
          out_path = Path(out_name)
          index_path = Path(index_name)
          if out_path.exists():
              out_path.unlink()
          if index_path.exists():
              index_path.unlink()

          size_index = {}
          with zipfile.ZipFile(out_path, "w", compression=zipfile.ZIP_DEFLATED) as zf:
              for p in base.rglob("*"):
                  if not p.is_file():
                      continue
                  arcname = p.relative_to(base).as_posix()
                  zf.write(p, arcname)
                  size_index[p.name] = p.stat().st_size

          index_path.write_text(
              json.dumps({"assets": size_index}, ensure_ascii=False, indent=2),
              encoding="utf-8",
          )

          print(f"created {out_name} size={out_path.stat().st_size} bytes")
          print(f"created {index_name} entries={len(size_index)}")
          PY

          # Release 只发布一个驱动总包，避免大量平铺资产污染 Release 页面
          rm -rf drivers

      - name: Generate SHA256SUMS
        shell: bash
        run: |
          cd release-assets
          FILES=()
          while IFS= read -r file; do
            if [ -n "$file" ]; then
              FILES+=("$file")
            fi
          done < <(find . -maxdepth 1 -type f ! -name SHA256SUMS -exec basename {} \; | sort)
          if [ ${#FILES[@]} -eq 0 ]; then
            echo "⚠️ 未找到可签名资产，生成空 SHA256SUMS"
            : > SHA256SUMS
          else
            sha256sum "${FILES[@]}" > SHA256SUMS
          fi

      - name: Create Release
        uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/')
        with:
          files: release-assets/*
          draft: true
          make_latest: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
