name: Build and Push Docker Image

on:
  release:
    types:
      - published  # 当通过 GitHub Releases 页面创建 release 时触发

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.release.tag_name }}  # 使用 release 对应的 tag
      
      - name: Extract version from release
        id: extract_version
        run: |
          # 从 release tag 中提取版本号（例如 v1.0.0 -> 1.0.0）
          TAG_NAME="${{ github.event.release.tag_name }}"
          if [ -z "$TAG_NAME" ]; then
            TAG_NAME=${GITHUB_REF#refs/tags/}
          fi
          
          # 验证版本号格式：v数字.数字.数字[-后缀]（例如 v1.0.0, v2.10.102, v1.0.0-beta）
          if [[ ! "$TAG_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.-]+)?$ ]]; then
            echo "错误: 版本号格式不正确，应为 v数字.数字.数字 或 v数字.数字.数字-后缀 (例如: v1.0.0, v1.0.0-beta)"
            exit 1
          fi
          
          VERSION=${TAG_NAME#v}  # 移除 v 前缀
          echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
          echo "TAG=$TAG_NAME" >> $GITHUB_OUTPUT
          echo "Extracted version: $VERSION"
          echo "Full tag: $TAG_NAME"
      
      - name: Send Telegram notification (build started)
        env:
          TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
          TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
        run: |
          # 检查必要的环境变量
          if [ -z "$TELEGRAM_BOT_TOKEN" ] || [ -z "$TELEGRAM_CHAT_ID" ]; then
            echo "⚠️  Telegram Bot Token 或 Chat ID 未配置，跳过通知"
            exit 0
          fi
          
          # 获取构建信息
          VERSION="${{ steps.extract_version.outputs.VERSION }}"
          TAG="${{ steps.extract_version.outputs.TAG }}"
          RELEASE_URL="${{ github.event.release.html_url }}"
          
          # 构建消息内容（仅包含关键信息）
          MESSAGE="🔨 <b>Docker 镜像构建中</b>"$'\n'$'\n'"📦 <b>版本:</b> ${VERSION}"$'\n'"🏷️ <b>Tag:</b> <code>${TAG}</code>"$'\n'"🔗 <a href=\"${RELEASE_URL}\">查看 Release</a>"
          
          # 发送 Telegram 消息（使用 jq 转义 JSON）
          curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
            -H "Content-Type: application/json" \
            -d "$(jq -n \
              --arg chat_id "$TELEGRAM_CHAT_ID" \
              --arg text "$MESSAGE" \
              '{chat_id: $chat_id, text: $text, parse_mode: "HTML", disable_web_page_preview: false}')" > /tmp/telegram_response.json
          
          # 检查发送结果
          if [ $? -eq 0 ]; then
            RESPONSE=$(cat /tmp/telegram_response.json)
            if echo "$RESPONSE" | grep -q '"ok":true'; then
              echo "✅ Telegram 通知发送成功"
            else
              echo "❌ Telegram 通知发送失败: $RESPONSE"
              # 通知失败不应该导致整个 job 失败
              exit 0
            fi
          else
            echo "❌ 发送 Telegram 消息时发生错误"
            # 通知失败不应该导致整个 job 失败
            exit 0
          fi
      
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
        with:
          # 启用多架构构建支持
          platforms: linux/amd64,linux/arm64
      
      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      
      - name: Build and push Docker image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: ./Dockerfile
          push: true
          # 多架构构建：支持 amd64 和 arm64
          platforms: linux/amd64,linux/arm64
          tags: |
            wrbug/polyhermes:${{ steps.extract_version.outputs.TAG }}
            wrbug/polyhermes:latest
          build-args: |
            VERSION=${{ steps.extract_version.outputs.VERSION }}
            GIT_TAG=${{ steps.extract_version.outputs.TAG }}
            GITHUB_REPO_URL=https://github.com/WrBug/PolyHermes
          cache-from: type=registry,ref=wrbug/polyhermes:latest
          cache-to: type=inline

      - name: Send Telegram notification
        env:
          TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
          TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
        run: |
          # 检查必要的环境变量
          if [ -z "$TELEGRAM_BOT_TOKEN" ] || [ -z "$TELEGRAM_CHAT_ID" ]; then
            echo "⚠️  Telegram Bot Token 或 Chat ID 未配置，跳过通知"
            exit 0
          fi
          
          # 获取构建信息
          VERSION="${{ steps.extract_version.outputs.VERSION }}"
          TAG="${{ steps.extract_version.outputs.TAG }}"
          RELEASE_NAME="${{ github.event.release.name }}"
          RELEASE_URL="${{ github.event.release.html_url }}"
          REPO_NAME="${{ github.repository }}"
          
          # 构建消息内容（仅包含关键信息）
          DEPLOY_DOC_URL="https://github.com/WrBug/PolyHermes/blob/main/docs/zh/DEPLOYMENT.md"
          MESSAGE="✅ <b>Docker 镜像构建成功</b>"$'\n'$'\n'"📦 <b>版本:</b> ${VERSION}"$'\n'"🏷️ <b>Tag:</b> <code>${TAG}</code>"$'\n'"🔗 <a href=\"${RELEASE_URL}\">查看 Release</a>"$'\n'"📚 <a href=\"${DEPLOY_DOC_URL}\">Docker 部署文档</a>"
          
          # 发送 Telegram 消息（使用 jq 转义 JSON）
          curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
            -H "Content-Type: application/json" \
            -d "$(jq -n \
              --arg chat_id "$TELEGRAM_CHAT_ID" \
              --arg text "$MESSAGE" \
              '{chat_id: $chat_id, text: $text, parse_mode: "HTML", disable_web_page_preview: false}')" > /tmp/telegram_response.json
          
          # 检查发送结果
          if [ $? -eq 0 ]; then
            RESPONSE=$(cat /tmp/telegram_response.json)
            if echo "$RESPONSE" | grep -q '"ok":true'; then
              echo "✅ Telegram 通知发送成功"
            else
              echo "❌ Telegram 通知发送失败: $RESPONSE"
              # 构建成功，通知失败不应该导致整个 job 失败
              exit 0
            fi
          else
            echo "❌ 发送 Telegram 消息时发生错误"
            # 构建成功，通知失败不应该导致整个 job 失败
            exit 0
          fi