name: Android Build

on:
  push:
    branches: [ main ]
    paths:
      - 'apps/web/**'
      - 'packages/**'
      - 'apps/api/**'
      - 'package.json'
      - 'pnpm-workspace.yaml'
      - 'pnpm-lock.yaml'
      - '.github/workflows/android-build.yml'
  pull_request:
    branches: [ main ]
    paths:
      - 'apps/web/**'
      - 'packages/**'
      - 'apps/api/**'
      - 'package.json'
      - 'pnpm-workspace.yaml'
      - 'pnpm-lock.yaml'
      - '.github/workflows/android-build.yml'
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: android-build-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build-android:
    name: Assemble Android APK (debug)
    runs-on: ubuntu-latest
    env:
      CAPACITOR_SKIP_UPDATE_CHECK: 'true'
      NODE_ENV: production

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

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          run_install: false
          package_json_path: package.json

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: pnpm

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

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Cache turbo
        uses: actions/cache@v4
        with:
          path: |
            .turbo
            node_modules/.cache/turbo
          key: ${{ runner.os }}-turbo-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-turbo-

      - name: Build web assets
        run: pnpm --filter=@my-roadtrip/web build

      - name: Validate build outputs
        run: |
          set -euo pipefail
          test -d apps/web/dist || (echo "apps/web/dist is missing" && exit 1)
          test -f apps/web/dist/manifest.webmanifest || (echo "manifest.webmanifest is missing" && exit 1)

      - name: Add Android platform (Capacitor)
        run: pnpm cap add android

      - name: Sync web assets to Android
        run: pnpm cap sync android

      - name: Cache Gradle
        uses: actions/cache@v4
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-${{ hashFiles('android/**/gradle-wrapper.properties') }}
          restore-keys: |
            ${{ runner.os }}-gradle-

      - name: Build Android APK (debug)
        run: |
          cd android
          ./gradlew assembleDebug

      - name: Collect Android artifact
        id: collect_android
        run: |
          set -euo pipefail
          SHORT_SHA=${GITHUB_SHA::7}
          APK_PATH="${GITHUB_WORKSPACE}/android/app/build/outputs/apk/debug/app-debug.apk"
          ARTIFACT_DIR="${GITHUB_WORKSPACE}/build-artifacts/android"
          mkdir -p "$ARTIFACT_DIR"
          ARTIFACT_BASENAME="roadtripper-android-${SHORT_SHA}"
          TARGET="$ARTIFACT_DIR/${ARTIFACT_BASENAME}.apk"
          cp "$APK_PATH" "$TARGET"
          sha256sum "$TARGET" > "$ARTIFACT_DIR/${ARTIFACT_BASENAME}.sha256"
          echo "apk_path=$TARGET" >> "$GITHUB_OUTPUT"
          echo "checksum_path=$ARTIFACT_DIR/${ARTIFACT_BASENAME}.sha256" >> "$GITHUB_OUTPUT"
          echo "artifact_name=$ARTIFACT_BASENAME" >> "$GITHUB_OUTPUT"

      - name: Upload Android artifact
        uses: actions/upload-artifact@v4
        with:
          name: android-apk
          path: |
            ${{ steps.collect_android.outputs.apk_path }}
            ${{ steps.collect_android.outputs.checksum_path }}

      - name: Publish summary
        if: always()
        run: |
          TARGET="${{ steps.collect_android.outputs.apk_path }}"
          CHECKSUM_FILE="${{ steps.collect_android.outputs.checksum_path }}"
          SIZE=$(stat -c%s "$TARGET")
          echo "## ✅ Android APK (debug)" >> $GITHUB_STEP_SUMMARY
          echo "- Artifact: \`${{ steps.collect_android.outputs.artifact_name }}.apk\`" >> $GITHUB_STEP_SUMMARY
          echo "- Size: ${SIZE} bytes" >> $GITHUB_STEP_SUMMARY
          echo "- Checksum: $(cat "$CHECKSUM_FILE" | cut -d' ' -f1)" >> $GITHUB_STEP_SUMMARY
          echo "- Commit: ${GITHUB_SHA}" >> $GITHUB_STEP_SUMMARY
