name: Android APK 构建

# 触发条件：
# 1. 每次推送代码到 main 分支：自动构建并更新名为 "latest" 的 Release (Rolling Release)
# 2. 推送 v* 格式的 tag（如 v1.0）：自动构建并发布正式 Release
# 3. 手动触发（workflow_dispatch）：手动指定 tag 发布
on:
  push:
    branches: [ main ]
    tags: [ 'v*' ]
  workflow_dispatch:
    inputs:
      release_tag:
        description: '上传到已有标签 (如 v1.0)'
        required: true
        default: 'v1.0'

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

permissions:
  contents: write

jobs:
  # ── 中文版 APK 构建 ──────────────────────────────────────────────────────────
  build-android-zh:
    name: Build EmbyX-zh APK
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Setup JDK 21
        uses: actions/setup-java@v4
        with:
          java-version: '21'
          distribution: 'temurin'

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: 22

      # 动态生成中文版 capacitor.config.json（webDir 指向 zh/ 目录）
      - name: Create capacitor.config.json (zh)
        run: |
          cat > capacitor.config.json << 'EOF'
          {
            "appId": "juneix.embyx",
            "appName": "EmbyX",
            "webDir": "zh",
            "backgroundColor": "#000000",
            "server": {
              "androidScheme": "http",
              "cleartext": true
            },
            "plugins": {
              "SplashScreen": {
                "launchAutoHide": true,
                "backgroundColor": "#000000",
                "androidScaleType": "CENTER",
                "showSpinner": false
              }
            }
          }
          EOF

      - name: Install Capacitor dependencies
        run: npm install

      - name: Add & Sync Android Platform
        run: |
          npx cap add android
          npx cap sync android

      # 运行补丁脚本：替换图标、注册屏保、添加全屏模式
      - name: Patch Android Project (zh)
        run: python .github/patch_android.py --lang zh

      - name: Build Debug APK
        run: |
          cd android
          chmod +x gradlew
          ./gradlew assembleDebug

      - name: Rename to EmbyX-zh.apk
        run: mv android/app/build/outputs/apk/debug/app-debug.apk EmbyX-zh.apk

      - name: Upload Artifact
        uses: actions/upload-artifact@v7
        with:
          name: embyx-zh
          path: EmbyX-zh.apk

  # ── 英文版 APK 构建 ──────────────────────────────────────────────────────────
  build-android-en:
    name: Build EmbyX-en APK
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Setup JDK 21
        uses: actions/setup-java@v4
        with:
          java-version: '21'
          distribution: 'temurin'

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: 22

      # 动态生成英文版 capacitor.config.json（webDir 指向 en/ 目录）
      - name: Create capacitor.config.json (en)
        run: |
          cat > capacitor.config.json << 'EOF'
          {
            "appId": "juneix.embyx",
            "appName": "EmbyX",
            "webDir": "en",
            "backgroundColor": "#000000",
            "server": {
              "androidScheme": "http",
              "cleartext": true
            },
            "plugins": {
              "SplashScreen": {
                "launchAutoHide": true,
                "backgroundColor": "#000000",
                "androidScaleType": "CENTER",
                "showSpinner": false
              }
            }
          }
          EOF

      - name: Install Capacitor dependencies
        run: npm install

      - name: Add & Sync Android Platform
        run: |
          npx cap add android
          npx cap sync android

      # 运行补丁脚本：替换图标、注册屏保、添加全屏模式
      - name: Patch Android Project (en)
        run: python .github/patch_android.py --lang en

      - name: Build Debug APK
        run: |
          cd android
          chmod +x gradlew
          ./gradlew assembleDebug

      - name: Rename to EmbyX-en.apk
        run: mv android/app/build/outputs/apk/debug/app-debug.apk EmbyX-en.apk

      - name: Upload Artifact
        uses: actions/upload-artifact@v7
        with:
          name: embyx-en
          path: EmbyX-en.apk

  # ── 发布 Release ─────────────────────────────────────────────────────────────
  # 当推送到 main 分支、打 v* 标签或手动触发时执行
  release:
    name: Create GitHub Release
    needs: [build-android-zh, build-android-en]
    runs-on: ubuntu-latest
    if: github.event_name == 'workflow_dispatch' || startsWith(github.ref, 'refs/tags/') || github.ref == 'refs/heads/main'
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v7

      - name: Verify APK files exist
        run: |
          ls -R
          test -f "embyx-zh/EmbyX-zh.apk"
          test -f "embyx-en/EmbyX-en.apk"

      - name: Create Release
        uses: softprops/action-gh-release@v3
        with:
          # 如果是 main 分支，固定使用 preview 标签以便滚动更新
          tag_name: ${{ github.event_name == 'workflow_dispatch' && inputs.release_tag || (github.ref_name == 'main' && 'preview' || github.ref_name) }}
          name: ${{ github.event_name == 'workflow_dispatch' && inputs.release_tag || (github.ref_name == 'main' && 'EmbyX Preview' || github.ref_name) }}
          prerelease: ${{ github.event_name == 'push' && github.ref_name == 'main' }}
          draft: false
          fail_on_unmatched_files: true
          files: |
            embyx-zh/EmbyX-zh.apk
            embyx-en/EmbyX-en.apk
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
