name: Release

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:

permissions:
  contents: write

jobs:
  build-linux-x86_64:
    name: Build Linux x86_64
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

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

      - name: Cache cargo registry
        uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-x86_64-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-x86_64-cargo-

      - name: Build release binary
        run: cargo build --release

      - name: Rename binary
        run: |
          mv target/release/mc-minder mc-minder-x86_64-linux
          chmod +x mc-minder-x86_64-linux

      - name: Generate SHA256 checksum
        run: sha256sum mc-minder-x86_64-linux > mc-minder-x86_64-linux.sha256

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: mc-minder-x86_64-linux
          path: |
            mc-minder-x86_64-linux
            mc-minder-x86_64-linux.sha256

  build-termux-aarch64:
    name: Build Termux ARM64
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: aarch64-linux-android

      - name: Cache cargo registry
        uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-aarch64-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-aarch64-cargo-

      - name: Install cross via binstall
        run: |
          curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash
          cargo binstall -y cross

      - name: Build for Android ARM64
        run: cross build --target aarch64-linux-android --release

      - name: Rename binary
        run: |
          mv target/aarch64-linux-android/release/mc-minder mc-minder-termux-aarch64
          chmod +x mc-minder-termux-aarch64

      - name: Generate SHA256 checksum
        run: sha256sum mc-minder-termux-aarch64 > mc-minder-termux-aarch64.sha256

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: mc-minder-termux-aarch64
          path: |
            mc-minder-termux-aarch64
            mc-minder-termux-aarch64.sha256

  release:
    needs: [build-linux-x86_64, build-termux-aarch64]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

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

      - name: Get version from tag
        id: get_version
        run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT

      - name: Generate Changelog
        id: changelog
        run: |
          # 获取上一个 tag（如果有）
          PREVIOUS_TAG=$(git tag --sort=-version:refname | grep -v "${{ steps.get_version.outputs.VERSION }}" | head -1)
          
          if [ -n "$PREVIOUS_TAG" ]; then
            echo "Generating changelog from ${PREVIOUS_TAG} to ${{ steps.get_version.outputs.VERSION }}"
            
            # 生成 changelog
            CHANGELOG=$(git log ${PREVIOUS_TAG}..${{ steps.get_version.outputs.VERSION }} \
              --pretty=format:"- %s (%h)" \
              --no-merges)
          else
            echo "No previous tag found, generating full changelog"
            
            # 第一个 release，显示所有 commit
            CHANGELOG=$(git log \
              --pretty=format:"- %s (%h)" \
              --no-merges)
          fi
          
          # 如果没有 commit，使用默认消息
          if [ -z "$CHANGELOG" ]; then
            CHANGELOG="- Initial release"
          fi
          
          # 将 changelog 写入输出（转义特殊字符）
          echo "changelog<<EOF" >> $GITHUB_OUTPUT
          echo "$CHANGELOG" >> $GITHUB_OUTPUT
          echo "EOF" >> $GITHUB_OUTPUT

      - name: Create Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ steps.get_version.outputs.VERSION }}
          name: MC-Minder ${{ steps.get_version.outputs.VERSION }}
          body: |
            ## MC-Minder ${{ steps.get_version.outputs.VERSION }}
            
            ### 更新内容
            
            ${{ steps.changelog.outputs.changelog }}
            
            ---
            
            ### 下载
            
            | 平台 | 文件 | 说明 |
            |------|------|------|
            | Linux x64 | `mc-minder-x86_64-linux` | 适用于桌面 Linux / WSL |
            | Termux/Android ARM64 | `mc-minder-termux-aarch64` | 适用于手机 Termux 环境 |
            
            ### 安装
            
            ```bash
            curl -fsSL https://raw.githubusercontent.com/SharkMI-0x7E/mc-minder/main/install.sh | bash
            ```
            
            ### 功能特性
            
            - 日志监控：使用 notify 库实时监控服务器日志，事件驱动，低 CPU 占用
            - AI 聊天机器人：支持 OpenAI API 和 Ollama，玩家使用 `!` 前缀触发
            - RCON 通信：异步 RCON 协议实现，自动重连
            - 上下文记忆：按玩家保存对话历史
            - HTTP API：RESTful API 用于状态查询和命令执行
            - TUI 启动脚本：基于 dialog 的图形化管理界面
            - JVM 配置支持：自定义 GC 算法和 JVM 参数
            - Telegram 通知：服务器事件实时推送
            - 一键安装：交互式初始化配置
            - 自动更新：内置 self-update 命令
          files: |
            binaries/mc-minder-x86_64-linux/mc-minder-x86_64-linux
            binaries/mc-minder-x86_64-linux/mc-minder-x86_64-linux.sha256
            binaries/mc-minder-termux-aarch64/mc-minder-termux-aarch64
            binaries/mc-minder-termux-aarch64/mc-minder-termux-aarch64.sha256
            install.sh
          draft: false
          prerelease: false
