name: Build Android (APK + AAB)

on:
  push:
    branches: [master]
    paths:
      - 'app/**'
      - 'mobile/**'
      - 'scripts/sync-html.mjs'
      - '.forgejo/workflows/build-android.yml'
  workflow_dispatch: {}  # botão manual no painel Forgejo

jobs:
  build-android:
    runs-on: ubuntu-latest
    permissions:
      contents: write  # pra upload de release

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

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

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

      - name: Setup Android SDK
        uses: android-actions/setup-android@v3
        with:
          packages: 'platforms;android-34 build-tools;34.0.0 platform-tools'

      - name: Install root deps (sync script)
        run: |
          cd "${{ github.workspace }}"
          # Sem package.json no root, script é puro Node ESM
          node --version

      - name: Sync HTML (1 fonte → server + mobile)
        run: node scripts/sync-html.mjs

      - name: Install Capacitor deps
        working-directory: mobile
        run: npm ci --ignore-scripts

      - name: Capacitor sync
        working-directory: mobile
        run: npx cap sync android

      - name: Decode keystore from secrets
        run: |
          echo "${{ secrets.SHIVAO_KEYSTORE_BASE64 }}" | base64 --decode > mobile/android/app/shivao-release.keystore
          ls -lh mobile/android/app/shivao-release.keystore

      - name: Build APK + AAB (release, signed)
        working-directory: mobile/android
        env:
          SHIVAO_KEYSTORE_PWD: ${{ secrets.SHIVAO_KEYSTORE_PWD }}
        run: |
          chmod +x ./gradlew
          ./gradlew bundleRelease assembleRelease --no-daemon --stacktrace

      - name: Rename artifacts with version
        run: |
          VERSION=$(grep "versionName" mobile/android/app/build.gradle | head -1 | grep -oE '"[^"]+"' | tr -d '"')
          echo "Version: $VERSION"
          cp mobile/android/app/build/outputs/apk/release/app-release.apk Shivao-v${VERSION}.apk
          cp mobile/android/app/build/outputs/bundle/release/app-release.aab Shivao-v${VERSION}.aab
          echo "VERSION=$VERSION" >> $GITHUB_ENV

      - name: Upload APK + AAB as artifacts (CI)
        uses: actions/upload-artifact@v3
        with:
          name: shivao-android-${{ env.VERSION }}
          path: |
            Shivao-v${{ env.VERSION }}.apk
            Shivao-v${{ env.VERSION }}.aab

      - name: Create Forgejo release
        if: github.event_name == 'workflow_dispatch'
        uses: actions/forgejo-release@v2
        with:
          direction: upload
          tag: v${{ env.VERSION }}-ci
          title: "Shivao v${{ env.VERSION }} (CI build)"
          files: |
            Shivao-v${{ env.VERSION }}.apk
            Shivao-v${{ env.VERSION }}.aab
          token: ${{ secrets.FORGEJO_TOKEN }}
          url: https://git.pontualtech.work
          repo: karlao/shivao-projeto
