name: CI Build

on:
  push:
    branches: [ '**' ]  # 所有分支的推送都会触发
  pull_request:
    branches: [ main, develop, master ]
  workflow_dispatch:  # 允许手动触发
    inputs:
      create_release:
        description: 'Create a release after build'
        required: false
        default: 'false'
        type: choice
        options:
          - 'true'
          - 'false'
      release_tag:
        description: 'Release tag (only if creating release)'
        required: false
        default: 'ci-latest'

permissions:
  contents: write  # 允许创建 release
  packages: write  # 允许上传包
  actions: read    # 允许读取 actions

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      with:
        submodules: 'recursive'
    
    - name: Set up JDK 17
      uses: actions/setup-java@v4
      with:
        distribution: 'temurin'
        java-version: '17'
    
    - name: Cache Gradle dependencies
      uses: actions/cache@v4
      with:
        path: |
          ~/.gradle/caches
          ~/.gradle/wrapper
        key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
        restore-keys: |
          ${{ runner.os }}-gradle-
    
    - name: Grant execute permission for gradlew
      run: chmod +x gradlew
    
    - name: Setup Android SDK
      uses: android-actions/setup-android@v3
      
    - name: Install NDK and CMake
      run: |
        echo "y" | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager "ndk;25.2.9519653"
        echo "y" | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager "cmake;3.22.1"
    
    - name: Build ConfigApp
      run: |
        cd configapp
        ../gradlew assembleDebug
        cd ..
        
    - name: Build Module
      run: |
        cd module
        ../gradlew assembleRelease
        cd ..
    
    - name: Package Module for Testing
      run: |
        # 创建临时目录
        TEMP_DIR="build/magisk_module"
        rm -rf $TEMP_DIR
        mkdir -p $TEMP_DIR
        
        # 创建 module.prop
        cat > $TEMP_DIR/module.prop << EOF
        id=zygisk-myinjector
        name=Zygisk MyInjector
        version=dev-${{ github.sha }}
        versionCode=9999
        author=jiqiu2022
        description=A Zygisk module for dynamic library injection with ConfigApp (CI Build)
        EOF
        
        # 复制文件
        cp module/service.sh $TEMP_DIR/
        chmod 755 $TEMP_DIR/service.sh
        
        # 创建 zygisk 目录并复制 so 文件
        mkdir -p $TEMP_DIR/zygisk
        for arch in armeabi-v7a arm64-v8a x86 x86_64; do
          SO_PATH="module/build/intermediates/stripped_native_libs/release/out/lib/$arch/libmyinjector.so"
          if [ -f "$SO_PATH" ]; then
            cp "$SO_PATH" "$TEMP_DIR/zygisk/$arch.so"
          fi
        done
        
        # 复制 ConfigApp APK
        cp configapp/build/outputs/apk/debug/configapp-debug.apk $TEMP_DIR/configapp.apk
        
        # 创建 META-INF 目录
        mkdir -p $TEMP_DIR/META-INF/com/google/android
        touch $TEMP_DIR/META-INF/com/google/android/update-binary
        touch $TEMP_DIR/META-INF/com/google/android/updater-script
        
        # 打包
        cd $TEMP_DIR
        zip -r ../../zygisk-myinjector-ci.zip *
        cd ../..
        
        # 列出文件内容
        echo "Module contents:"
        unzip -l zygisk-myinjector-ci.zip
    
    - name: Upload Module Artifact
      uses: actions/upload-artifact@v4
      with:
        name: module-ci-${{ github.sha }}
        path: zygisk-myinjector-ci.zip
        retention-days: 7
        
    - name: Upload ConfigApp Artifact
      uses: actions/upload-artifact@v4
      with:
        name: configapp-ci-${{ github.sha }}
        path: configapp/build/outputs/apk/debug/configapp-debug.apk
        retention-days: 7
    
    - name: Prepare Release Files
      if: github.event_name == 'workflow_dispatch' && github.event.inputs.create_release == 'true'
      run: |
        # 准备发布文件
        mkdir -p release-files
        cp zygisk-myinjector-ci.zip release-files/zygisk-myinjector-${{ github.event.inputs.release_tag }}.zip
        cp configapp/build/outputs/apk/debug/configapp-debug.apk release-files/ConfigApp-${{ github.event.inputs.release_tag }}.apk
        
        echo "Release files:"
        ls -la release-files/
    
    - name: Create Release
      if: github.event_name == 'workflow_dispatch' && github.event.inputs.create_release == 'true'
      uses: softprops/action-gh-release@v2
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        tag_name: ${{ github.event.inputs.release_tag }}
        name: CI Release ${{ github.event.inputs.release_tag }}
        body: |
          ## CI Build Release
          
          - **Build Type**: Manual CI Release
          - **Commit**: ${{ github.sha }}
          - **Branch**: ${{ github.ref_name }}
          - **Run ID**: ${{ github.run_id }}
          
          ### 下载
          - 模块文件: `zygisk-myinjector-${{ github.event.inputs.release_tag }}.zip`
          - 配置应用: `ConfigApp-${{ github.event.inputs.release_tag }}.apk`
          
          ### 安装说明
          1. 在 Magisk Manager 中安装模块 ZIP
          2. 重启设备
          3. ConfigApp 会自动安装
          
          ---
          *这是一个 CI 构建版本，可能不稳定*
        draft: false
        prerelease: true
        files: |
          release-files/zygisk-myinjector-*.zip
          release-files/ConfigApp-*.apk